Advanced Strategic Command
ASCStringHelpers.h
Go to the documentation of this file.
1 #ifndef __ASC_STRING_HELPERS_H_INCLUDED__
2 #define __ASC_STRING_HELPERS_H_INCLUDED__
3 
4 #include <cstdlib>
5 #include <cassert>
6 #include <cstdarg>
7 #include <cstdio>
8 #include <cstring>
9 #include <memory>
10 #include <string>
11 #include <wchar.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 
15 #include <boost/scoped_array.hpp>
16 
17 using std::auto_ptr;
18 using std::string;
19 
20 #ifndef _UNICODE_BROKEN_
21 
22  using std::wstring;
23 
24 #else
25 
26  #undef _UNICODE
27 
28 #endif // _UNICODE_BROKEN_
29 
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102 
103  #ifdef ASC_UNICODE
104 
105  typedef wstring ASCCharTString;
106  typedef string ASCAdaptatorString;
107 
108  #ifndef _T
109  #define _T( text ) L ## text
110  #else
111  #error "_T macro already defined."
112  #endif
113 
114  #else // ASC_UNICODE
115 
116  typedef string ASCCharTString;
117 
118  #ifndef _UNICODE_BROKEN_
119 
120  typedef wstring ASCAdaptatorString;
121 
122  #endif // _UNICODE_BROKEN_
123 
124  #ifndef _T
125  #define _T( text ) text
126  #else
127  // #error "_T macro already defined."
128  #endif
129 
130  #endif // _UNICODE
131 
132 #endif // DOXYGEN_SHOULD_SKIP_THIS
133 
147 {
148 public:
149 
159  typedef ASCCharTString::value_type charT;
160 
161 #ifndef _UNICODE_BROKEN_
162 
172  typedef ASCAdaptatorString::value_type NoncharT;
173 
174 #endif // _UNICODE_BROKEN_
175 
176  static size_t _Strlen ( const charT* pS );
177  static charT* _Strcpy ( charT* pDest, const charT* pSrc );
178  static charT* _Strlwr ( charT* pS );
179  static charT* _Strupr ( charT* pS );
180  static int _Stricmp ( const charT* pS1, const charT* pS2 );
181  static int _Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr );
182  static int _Printf ( const charT* format, ... );
183 
184 #ifndef _UNICODE_BROKEN_
185 
186  static size_t _ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count );
187 
188 #endif // _UNICODE_BROKEN_
189 
190 };
191 
192 
206 inline size_t ASCStringHelpers::_Strlen ( const charT* pS )
207 {
208  assert ( pS != NULL );
209 
210  #ifdef ASC_UNICODE
211  return ::wcslen ( pS );
212  #else
213  return ::strlen ( pS );
214  #endif
215 }
216 
232 {
233  assert ( pSrc != NULL );
234  assert ( pDest != NULL );
235 
236  #ifdef ASC_UNICODE
237  return ::wcscpy ( pDest, pSrc );
238  #else
239  return ::strcpy ( pDest, pSrc );
240  #endif
241 }
242 
256 {
257  assert ( pS != NULL );
258 
259 #ifndef _UNIX_
260 
261  #ifdef ASC_UNICODE
262  return ::_wcslwr ( pS );
263  #else
264  return :: strlwr ( pS );
265  #endif
266 
267 #else // _UNIX_
268 
269  charT* pTemp = pS;
270 
271  while ( *pTemp != 0 )
272  {
273  #ifdef ASC_UNICODE
274  *pTemp = towlower ( *pTemp );
275  #else
276  *pTemp = tolower ( *pTemp );
277  #endif
278 
279  ++pTemp;
280  };
281  return pS;
282 
283 #endif // _UNIX_
284 
285 }
286 
300 {
301  assert ( pS != NULL );
302 
303 #ifndef _UNIX_
304 
305  #ifdef ASC_UNICODE
306  return ::_wcsupr ( pS );
307  #else
308  return :: strupr ( pS );
309  #endif
310 
311 #else // _UNIX_
312 
313  charT* pTemp = pS;
314 
315  while ( *pTemp != 0 )
316  {
317  #ifdef ASC_UNICODE
318  *pTemp = towupper ( *pTemp );
319  #else
320  *pTemp = toupper ( *pTemp );
321  #endif
322 
323  ++pTemp;
324  };
325  return pS;
326 
327 #endif // _UNIX_
328 
329 }
330 
350 inline int ASCStringHelpers::_Stricmp ( const charT* pS1, const charT* pS2 )
351 {
352  assert ( pS1 != NULL );
353  assert ( pS2 != NULL );
354 
355 #ifndef _UNIX_
356 
357  #ifdef ASC_UNICODE
358  return ::wcsicmp ( pS1, pS2 );
359  #else
360  return ::stricmp ( pS1, pS2 );
361  #endif
362 
363 #else // _UNIX_
364 
365  #ifdef ASC_UNICODE
366  // auto_ptr will release the memorey if an exception is raised
367  boost::scoped_array< charT > l_autopS1 ( new charT [ _Strlen ( pS1 ) + sizeof ( charT ) ] );
368  boost::scoped_array< charT > l_autopS2 ( new charT [ _Strlen ( pS2 ) + sizeof ( charT ) ] );
369 
370  charT* l_pS1 = l_autopS1.get();
371  charT* l_pS2 = l_autopS2.get();
372 
373  _Strcpy ( l_pS1, pS1 );
374  _Strcpy ( l_pS2, pS2 );
375 
376  _Strlwr ( l_pS1 );
377  _Strlwr ( l_pS2 );
378 
379  return ::wcscmp ( l_pS1, l_pS2 );
380  #else
381  return ::strcasecmp ( pS1, pS2 );
382  #endif
383 
384 #endif // _UNIX_
385 
386 }
387 
406 inline int ASCStringHelpers::_Vsnprintf ( charT* buffer, size_t count, const charT* format, std::va_list argptr )
407 {
408  assert ( buffer != NULL );
409  assert ( format != NULL );
410 
411 #ifdef _MSC_VER
412  #define vsnwprintf _vsnwprintf
413  #define vsnprintf _vsnprintf
414 #endif
415 
416  #ifdef ASC_UNICODE
417  return ::vsnwprintf ( buffer, count, format, argptr );
418  #else
419  return ::vsnprintf ( buffer, count, format, argptr );
420  #endif
421 }
422 
437 inline int ASCStringHelpers::_Printf ( const charT* format, ... )
438 {
439  assert ( format != NULL );
440 
441  std::va_list argptr;
442  va_start ( argptr, format );
443 
444  int nRes = 0;
445 
446  #ifdef ASC_UNICODE
447  nRes = ::vwprintf ( format, argptr );
448  #else
449  nRes = ::vprintf ( format, argptr );
450  #endif
451 
452  va_end ( argptr );
453  return nRes;
454 }
455 
456 #ifndef _UNICODE_BROKEN_
457 
472 inline size_t ASCStringHelpers::_ConvertToCharT ( charT* pDest, const NoncharT* pSrc, size_t count )
473 {
474  assert ( pDest != NULL );
475  assert ( pSrc != NULL );
476 
477  #ifdef ASC_UNICODE
478  return ::mbstowcs( pDest, pSrc, count );
479  #else
480  return std::wcstombs( pDest, pSrc, count );
481  #endif
482 }
483 
484 #endif // _UNICODE_BROKEN_
485 
486 #endif // __ASC_STRING_HELPERS_H_INCLUDED__
487 
#define stricmp
static charT * _Strcpy(charT *pDest, const charT *pSrc)
static charT * _Strlwr(charT *pS)
static int _Printf(const charT *format,...)
static int _Stricmp(const charT *pS1, const charT *pS2)
static size_t _ConvertToCharT(charT *pDest, const NoncharT *pSrc, size_t count)
char * strupr(const char *a)
Definition: misc.cpp:164
wstring ASCAdaptatorString
static int _Vsnprintf(charT *buffer, size_t count, const charT *format, std::va_list argptr)
static size_t _Strlen(const charT *pS)
ASCCharTString::value_type charT
The ASCStringHelpers class provides helper's functions used by ASCString.
static charT * _Strupr(charT *pS)
ASCAdaptatorString::value_type NoncharT
string ASCCharTString