File: ASCStringHelpers.h

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (485 lines) | stat: -rw-r--r-- 11,743 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#ifndef __ASC_STRING_HELPERS_H_INCLUDED__
#define __ASC_STRING_HELPERS_H_INCLUDED__

#include <cstdlib>
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <memory>
#include <string>
#include <wchar.h>
#include <stdio.h>
#include <ctype.h>

using std::auto_ptr;
using std::string;

#ifndef _UNICODE_BROKEN_

    using std::wstring;

#else

    #undef _UNICODE

#endif // _UNICODE_BROKEN_

/*! \class ASCCharTString ASCStringHelpers.h

    \brief \e ASCCharTString represents the STL parent's string class of ASCString.

    \li If the preprocessor definition _UNICODE is defined, \e ASCCharTString is an
    STL's \e std::wstring to use \e wchar_t type.
    \li If _UNICODE is not defined, \e ASCCharTString is an STL's \e std::string
    to use \e char type.

    \par
    That way, \e ASCCharTString will addapt itself to the right character type needed for its internal
    string representation depending on the _UNICODE preprocessor definition.


    \e ASCCharTString is defined through a typedef :

    \code

    #ifdef _UNICODE

        typedef wstring      ASCCharTString;

    #else

        typedef string       ASCCharTString;

    #endif

    \endcode

    \par
    See STL's documentation for more information on \e std::string and \e std::wstring.

    \sa ASCAdaptatorString

*/

/*! \class ASCAdaptatorString ASCStringHelpers.h

    \brief \e ASCAdaptatorString represents the complementary class to ASCCharTString.

    Complementary to ASCCharTString means that if ASCCharTString is using \e char types
    for its internal string representation, \e ASCAdaptatorString will use \e wchar_t types.
    On the other hand, if ASCCharTString is using \e wchar_t, \e ASCAdaptatorString will use \e char.

    \li If the preprocessor definition _UNICODE is defined, \e ASCAdaptatorString is an
    STL's \e std::string to use \e char type.
    \li If _UNICODE is not defined, \e ASCAdaptatorString is an STL's \e std::wstring to use \e wchar_t type.

    \e ASCAdaptatorString is defined through a typedef :

    \code

    #ifdef _UNICODE

        typedef string       ASCAdaptatorString;

    #else

        typedef wstring      ASCAdaptatorString;

    #endif

    \endcode

    \par
    See STL's documentation for more information on \e std::string and \e std::wstring.

    \sa ASCCharTString
*/

#ifndef DOXYGEN_SHOULD_SKIP_THIS

    #ifdef ASC_UNICODE

        typedef wstring     ASCCharTString;
        typedef string      ASCAdaptatorString;

        #ifndef _T
            #define  _T( text )   L ## text
        #else
            #error "_T macro already defined."
        #endif

    #else // ASC_UNICODE

        typedef string       ASCCharTString;

        #ifndef _UNICODE_BROKEN_

            typedef wstring      ASCAdaptatorString;

        #endif // _UNICODE_BROKEN_

        #ifndef _T
            #define  _T( text )   text
        #else
            // #error "_T macro already defined."
        #endif

    #endif // _UNICODE

#endif // DOXYGEN_SHOULD_SKIP_THIS

/*!
    \class ASCStringHelpers ASCStringHelpers.h

    \brief The \e ASCStringHelpers class provides helper's functions used by ASCString.

    \par
    \e ASCStringHelpers provides standard interfaces to utility functions across platforms and char types
    ( char or wchar_t ).

    \sa ASCCharTString, ASCAdaptatorString

*/
class ASCStringHelpers
{
public:

    /*!
        charT is an alias to ASCCharTString::value_type, the character type used to represent strings
        internally.

        \li If _UNICODE is defined, charT is a \e wchar_t.
        \li If _UNICODE is not defined, charT is a \e char.

        \sa ASCCharTString
    */
    typedef ASCCharTString::value_type  charT;

#ifndef _UNICODE_BROKEN_

    /*!
        NoncharT is an alias to ASCAdaptatorString::value_type, the character type used to represent
        complementary strings internally.

        \li If _UNICODE is defined, NoncharT is a \e char.
        \li If _UNICODE is not defined, NoncharT is a \e wchar_t.

        \sa ASCAdaptatorString
    */
    typedef ASCAdaptatorString::value_type  NoncharT;

#endif // _UNICODE_BROKEN_

    static size_t  _Strlen    ( const charT* pS );
    static charT*  _Strcpy    ( charT* pDest, const charT* pSrc );
    static charT*  _Strlwr    ( charT* pS );
    static charT*  _Strupr    ( charT* pS );
    static int     _Stricmp   ( const charT* pS1, const charT* pS2 );
    static int     _Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr );
    static int     _Printf    ( const charT* format, ... );

#ifndef _UNICODE_BROKEN_

    static size_t  _ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count );

#endif // _UNICODE_BROKEN_

};


/*!
    \fn size_t ASCStringHelpers::_Strlen ( const charT* pS )

    Get the length of a string.

    \param pS a pointer to a NULL-terminated string.

    \return returns the number of characters in string, excluding the terminal NULL.
    No return value is reserved to indicate an error.

    \remarks
    See standard system documentation for more information about \e strlen.
*/
inline size_t ASCStringHelpers::_Strlen ( const charT* pS )
{
    assert ( pS != NULL );

    #ifdef ASC_UNICODE
        return ::wcslen ( pS );
    #else
        return ::strlen ( pS );
    #endif
}

/*!
    \fn ASCStringHelpers::charT* ASCStringHelpers::_Strcpy ( charT* pDest, const charT* pSrc )

    Copy a string

    \param pDest a pointer to a NULL-terminated string which will receive a copy of \a pSrc.

    \param pSrc a pointer to a NULL-terminated string which will be copied.

    \return returns a pointer to the destination string. No return value is reserved to indicate an error.

    \remarks
    See standard system documentation for more information about \e strcpy.
*/
inline ASCStringHelpers::charT* ASCStringHelpers::_Strcpy ( charT* pDest, const charT* pSrc )
{
    assert ( pSrc  != NULL );
    assert ( pDest != NULL );

    #ifdef ASC_UNICODE
        return ::wcscpy ( pDest, pSrc );
    #else
        return ::strcpy ( pDest, pSrc );
    #endif
}

/*!
    \fn ASCStringHelpers::charT* ASCStringHelpers::_Strlwr ( charT* pS )

    Convert to lowercase.

    \param pS a pointer to a NULL-terminated string.

    \return returns a pointer to the destination string. No return value is reserved to indicate an error.

    \remarks
    See standard system documentation for more information about \e strlwr.
*/
inline ASCStringHelpers::charT* ASCStringHelpers::_Strlwr ( charT* pS )
{
    assert ( pS != NULL );

#ifndef _UNIX_

    #ifdef ASC_UNICODE
        return ::_wcslwr ( pS );
    #else
        return :: strlwr ( pS );
    #endif

#else // _UNIX_

    charT* pTemp = pS;

    while ( *pTemp != 0 )
    {
        #ifdef ASC_UNICODE
            *pTemp = towlower ( *pTemp );
        #else
            *pTemp = tolower  ( *pTemp );
        #endif

        ++pTemp;
    };
    return pS;

#endif // _UNIX_

}

/*!
    \fn ASCStringHelpers::charT* ASCStringHelpers::_Strupr ( charT* pS )

    Convert to uppercase.

    \param pS a pointer to a NULL-terminated string.

    \return returns a pointer to the destination string. No return value is reserved to indicate an error.

    \remarks
    See standard system documentation for more information about \e strupr.
*/
inline ASCStringHelpers::charT* ASCStringHelpers::_Strupr ( charT* pS )
{
    assert ( pS != NULL );

#ifndef _UNIX_

    #ifdef ASC_UNICODE
        return ::_wcsupr ( pS );
    #else
        return :: strupr ( pS );
    #endif

#else // _UNIX_

    charT* pTemp = pS;

    while ( *pTemp != 0 )
    {
        #ifdef ASC_UNICODE
            *pTemp = towupper ( *pTemp );
        #else
            *pTemp = toupper  ( *pTemp );
        #endif

        ++pTemp;
    };
    return pS;

#endif // _UNIX_

}

/*!
    \fn int ASCStringHelpers::_Stricmp ( const charT* pS1, const charT* pS2 )

    Perform a lowercase comparison of strings.

    \param pS1 a pointer to a NULL-terminated string.

    \param pS2 a pointer to a NULL-terminated string.

    \return returns a value indicating the relationship between the compared strings :

    < 0 pS1 less than pS2
      0 pS1 identical to pS2
    > 0 pS1 greater than pS2

    \remarks
    See standard system documentation for more information about \e stricmp.

*/
inline int ASCStringHelpers::_Stricmp ( const charT* pS1, const charT* pS2 )
{
    assert ( pS1 != NULL );
    assert ( pS2 != NULL );

#ifndef _UNIX_

    #ifdef ASC_UNICODE
        return ::wcsicmp ( pS1, pS2 );
    #else
        return ::stricmp ( pS1, pS2 );
    #endif

#else // _UNIX_

    #ifdef ASC_UNICODE
        // auto_ptr will release the memorey if an exception is raised
        auto_ptr< charT > l_autopS1 ( new charT [ _Strlen ( pS1 ) + sizeof ( charT ) ] );
        auto_ptr< charT > l_autopS2 ( new charT [ _Strlen ( pS2 ) + sizeof ( charT ) ] );

        charT* l_pS1 = l_autopS1.get();
        charT* l_pS2 = l_autopS2.get();

        _Strcpy ( l_pS1, pS1 );
        _Strcpy ( l_pS2, pS2 );

        _Strlwr ( l_pS1 );
        _Strlwr ( l_pS2 );

        return ::wcscmp ( l_pS1, l_pS2 );
    #else
        return ::strcasecmp ( pS1, pS2 );
    #endif

#endif // _UNIX_

}

/*!
    \fn int ASCStringHelpers::_Vsnprintf ( charT* buffer, size_t count, const charT* format, va_list argptr )

    Write formated output.

    \param buffer the storage location for output.

    \param count the maximum number of characters to write.

    \param format the format string.

    \param argptr pointer to a list of arguments.

    \return returns the number of characters written.

    \remarks
    See standard system documentation for more information about \e vsnprintf.
*/
inline int ASCStringHelpers::_Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr )
{
    assert ( buffer != NULL );
    assert ( format != NULL );

#ifdef _MSC_VER
    #define vsnwprintf  _vsnwprintf
    #define vsnprintf   _vsnprintf
#endif

    #ifdef ASC_UNICODE
        return ::vsnwprintf ( buffer, count, format, argptr );
    #else
        return ::vsnprintf ( buffer, count, format, argptr );
    #endif
}

/*!
    \fn int ASCStringHelpers::_Printf ( const charT* format, ... )

    Print formated string to standard output.

    \param format the format string.

    \param ... optional arguments.

    \return returns the number of characters printed.

    \remarks
    See standard system documentation for more information about \e printf.
*/
inline int ASCStringHelpers::_Printf ( const charT* format, ... )
{
    assert ( format != NULL );

    std::va_list argptr;
    va_start ( argptr, format );

    int nRes = 0;

    #ifdef ASC_UNICODE
        nRes = ::vwprintf ( format, argptr );
    #else
        nRes = ::vprintf ( format, argptr );
    #endif

    va_end ( argptr );
    return nRes;
}

#ifndef _UNICODE_BROKEN_

/*!
    \fn size_t ASCStringHelpers::_ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count )

    Convert a non-native string to a native one.

    \param pDest a pointer to a NULL-terminated string which will receive a converted \a pSrc.

    \param pSrc a pointer to a NULL-terminated string which will be converted.

    \param count the size of the destination buffer

    \return returns the number of characters written to \a pDest.

*/
inline size_t ASCStringHelpers::_ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count )
{
    assert ( pDest != NULL );
    assert ( pSrc  != NULL );

    #ifdef ASC_UNICODE
        return ::mbstowcs( pDest, pSrc, count );
    #else
        return std::wcstombs( pDest, pSrc, count );
    #endif
}

#endif // _UNICODE_BROKEN_

#endif // __ASC_STRING_HELPERS_H_INCLUDED__