00001 #ifndef __ASC_STRING_HELPERS_H_INCLUDED__
00002 #define __ASC_STRING_HELPERS_H_INCLUDED__
00003
00004 #include <cstdlib>
00005 #include <cassert>
00006 #include <cstdarg>
00007 #include <cstdio>
00008 #include <cstring>
00009 #include <memory>
00010 #include <string>
00011 #include <wchar.h>
00012 #include <stdio.h>
00013 #include <ctype.h>
00014
00015 using std::auto_ptr;
00016 using std::string;
00017
00018 #ifndef _UNICODE_BROKEN_
00019
00020 using std::wstring;
00021
00022 #else
00023
00024 #undef _UNICODE
00025
00026 #endif // _UNICODE_BROKEN_
00027
00099 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00100
00101 #ifdef ASC_UNICODE
00102
00103 typedef wstring ASCCharTString;
00104 typedef string ASCAdaptatorString;
00105
00106 #ifndef _T
00107 #define _T( text ) L ## text
00108 #else
00109 #error "_T macro already defined."
00110 #endif
00111
00112 #else // ASC_UNICODE
00113
00114 typedef string ASCCharTString;
00115
00116 #ifndef _UNICODE_BROKEN_
00117
00118 typedef wstring ASCAdaptatorString;
00119
00120 #endif // _UNICODE_BROKEN_
00121
00122 #ifndef _T
00123 #define _T( text ) text
00124 #else
00125
00126 #endif
00127
00128 #endif // _UNICODE
00129
00130 #endif // DOXYGEN_SHOULD_SKIP_THIS
00131
00144 class ASCStringHelpers
00145 {
00146 public:
00147
00157 typedef ASCCharTString::value_type charT;
00158
00159 #ifndef _UNICODE_BROKEN_
00160
00170 typedef ASCAdaptatorString::value_type NoncharT;
00171
00172 #endif // _UNICODE_BROKEN_
00173
00174 static size_t _Strlen ( const charT* pS );
00175 static charT* _Strcpy ( charT* pDest, const charT* pSrc );
00176 static charT* _Strlwr ( charT* pS );
00177 static charT* _Strupr ( charT* pS );
00178 static int _Stricmp ( const charT* pS1, const charT* pS2 );
00179 static int _Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr );
00180 static int _Printf ( const charT* format, ... );
00181
00182 #ifndef _UNICODE_BROKEN_
00183
00184 static size_t _ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count );
00185
00186 #endif // _UNICODE_BROKEN_
00187
00188 };
00189
00190
00204 inline size_t ASCStringHelpers::_Strlen ( const charT* pS )
00205 {
00206 assert ( pS != NULL );
00207
00208 #ifdef ASC_UNICODE
00209 return ::wcslen ( pS );
00210 #else
00211 return ::strlen ( pS );
00212 #endif
00213 }
00214
00229 inline ASCStringHelpers::charT* ASCStringHelpers::_Strcpy ( charT* pDest, const charT* pSrc )
00230 {
00231 assert ( pSrc != NULL );
00232 assert ( pDest != NULL );
00233
00234 #ifdef ASC_UNICODE
00235 return ::wcscpy ( pDest, pSrc );
00236 #else
00237 return ::strcpy ( pDest, pSrc );
00238 #endif
00239 }
00240
00253 inline ASCStringHelpers::charT* ASCStringHelpers::_Strlwr ( charT* pS )
00254 {
00255 assert ( pS != NULL );
00256
00257 #ifndef _UNIX_
00258
00259 #ifdef ASC_UNICODE
00260 return ::_wcslwr ( pS );
00261 #else
00262 return :: strlwr ( pS );
00263 #endif
00264
00265 #else // _UNIX_
00266
00267 charT* pTemp = pS;
00268
00269 while ( *pTemp != 0 )
00270 {
00271 #ifdef ASC_UNICODE
00272 *pTemp = towlower ( *pTemp );
00273 #else
00274 *pTemp = tolower ( *pTemp );
00275 #endif
00276
00277 ++pTemp;
00278 };
00279 return pS;
00280
00281 #endif // _UNIX_
00282
00283 }
00284
00297 inline ASCStringHelpers::charT* ASCStringHelpers::_Strupr ( charT* pS )
00298 {
00299 assert ( pS != NULL );
00300
00301 #ifndef _UNIX_
00302
00303 #ifdef ASC_UNICODE
00304 return ::_wcsupr ( pS );
00305 #else
00306 return :: strupr ( pS );
00307 #endif
00308
00309 #else // _UNIX_
00310
00311 charT* pTemp = pS;
00312
00313 while ( *pTemp != 0 )
00314 {
00315 #ifdef ASC_UNICODE
00316 *pTemp = towupper ( *pTemp );
00317 #else
00318 *pTemp = toupper ( *pTemp );
00319 #endif
00320
00321 ++pTemp;
00322 };
00323 return pS;
00324
00325 #endif // _UNIX_
00326
00327 }
00328
00348 inline int ASCStringHelpers::_Stricmp ( const charT* pS1, const charT* pS2 )
00349 {
00350 assert ( pS1 != NULL );
00351 assert ( pS2 != NULL );
00352
00353 #ifndef _UNIX_
00354
00355 #ifdef ASC_UNICODE
00356 return ::wcsicmp ( pS1, pS2 );
00357 #else
00358 return ::stricmp ( pS1, pS2 );
00359 #endif
00360
00361 #else // _UNIX_
00362
00363 #ifdef ASC_UNICODE
00364
00365 auto_ptr< charT > l_autopS1 ( new charT [ _Strlen ( pS1 ) + sizeof ( charT ) ] );
00366 auto_ptr< charT > l_autopS2 ( new charT [ _Strlen ( pS2 ) + sizeof ( charT ) ] );
00367
00368 charT* l_pS1 = l_autopS1.get();
00369 charT* l_pS2 = l_autopS2.get();
00370
00371 _Strcpy ( l_pS1, pS1 );
00372 _Strcpy ( l_pS2, pS2 );
00373
00374 _Strlwr ( l_pS1 );
00375 _Strlwr ( l_pS2 );
00376
00377 return ::wcscmp ( l_pS1, l_pS2 );
00378 #else
00379 return ::strcasecmp ( pS1, pS2 );
00380 #endif
00381
00382 #endif // _UNIX_
00383
00384 }
00385
00404 inline int ASCStringHelpers::_Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr )
00405 {
00406 assert ( buffer != NULL );
00407 assert ( format != NULL );
00408
00409 #ifdef _MSC_VER
00410 #define vsnwprintf _vsnwprintf
00411 #define vsnprintf _vsnprintf
00412 #endif
00413
00414 #ifdef ASC_UNICODE
00415 return ::vsnwprintf ( buffer, count, format, argptr );
00416 #else
00417 return ::vsnprintf ( buffer, count, format, argptr );
00418 #endif
00419 }
00420
00435 inline int ASCStringHelpers::_Printf ( const charT* format, ... )
00436 {
00437 assert ( format != NULL );
00438
00439 std::va_list argptr;
00440 va_start ( argptr, format );
00441
00442 int nRes = 0;
00443
00444 #ifdef ASC_UNICODE
00445 nRes = ::vwprintf ( format, argptr );
00446 #else
00447 nRes = ::vprintf ( format, argptr );
00448 #endif
00449
00450 va_end ( argptr );
00451 return nRes;
00452 }
00453
00454 #ifndef _UNICODE_BROKEN_
00455
00470 inline size_t ASCStringHelpers::_ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count )
00471 {
00472 assert ( pDest != NULL );
00473 assert ( pSrc != NULL );
00474
00475 #ifdef ASC_UNICODE
00476 return ::mbstowcs( pDest, pSrc, count );
00477 #else
00478 return std::wcstombs( pDest, pSrc, count );
00479 #endif
00480 }
00481
00482 #endif // _UNICODE_BROKEN_
00483
00484 #endif // __ASC_STRING_HELPERS_H_INCLUDED__
00485