1 #ifndef __STRUTILS_H__91CD1AE9_5C86_4261_AB5D_BDDAFBDA8DC5
2 #define __STRUTILS_H__91CD1AE9_5C86_4261_AB5D_BDDAFBDA8DC5
10 #ifndef STRUTILS_RETURN_TYPE
12 #define STRUTILS_RETURN_TYPE CFugue::MString
18 #pragma warning(disable:4996) // sprintf deprecated warning: disable;
24 #if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
32 typedef __int64 INT_PTR, *PINT_PTR;
33 typedef unsigned __int64 UINT_PTR, *PUINT_PTR;
35 typedef __int64 LONG_PTR, *PLONG_PTR;
36 typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
38 #define __int3264 __int64
41 typedef _W64
int INT_PTR, *PINT_PTR;
42 typedef _W64
unsigned int UINT_PTR, *PUINT_PTR;
44 typedef _W64
long LONG_PTR, *PLONG_PTR;
45 typedef _W64
unsigned long ULONG_PTR, *PULONG_PTR;
47 #define __int3264 __int32
51 typedef STRUTILS_RETURN_TYPE StrUtils_Return_Type;
53 #if !defined(_countof)
54 #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
57 #if defined __GNUC__ && defined __STRICT_ANSI__
58 #define PRINTNUM(BUF,SPEC,NUM) _stprintf(BUF,_countof(BUF),SPEC,NUM) // swprint is by default a secure version !!
59 #elif defined __GNUC__ && !defined __STRICT_ANSI__
60 #define PRINTNUM(BUF,SPEC,NUM) _stprintf(BUF,SPEC,NUM) // Gcc does not have a secure version of sprintf
61 #elif defined _MSC_VER
62 #define PRINTNUM(BUF,SPEC,NUM) _stprintf_s(BUF,_countof(BUF),SPEC,NUM) //MSVC has different Secure version
66 #define GetBufSizeForType(xType) ((sizeof(xType)*8) + BUFExtra) // We need extra for space and any other chars (such as 0x)
68 #define DEFAULT_WIDTH -1
69 #define DEFAULT_FILLER _T('0')
70 #define DEFAULT_RADIX 10
71 #define DEFAULT_PREFIXUSAGE true
74 template<
typename TNumberType>
75 inline TNumberType _abs(TNumberType number) {
return( number>=0 ? number : -number ); }
78 template<
typename TNumberType>
79 StrUtils_Return_Type UnSignedNumber2Str(
const TNumberType TNumber,
80 int Width = DEFAULT_WIDTH,
81 TCHAR FillWith = DEFAULT_FILLER,
82 int Radix = DEFAULT_RADIX,
83 bool bPrefixRadix=DEFAULT_PREFIXUSAGE
86 TCHAR buf[GetBufSizeForType(TNumberType)];
88 TCHAR* pText = buf + _countof(buf) - 1;
92 if(Width >= (_countof(buf) - 2))
93 Width = DEFAULT_WIDTH;
95 TNumberType nNumber = TNumber;
98 int digit = (int)(nNumber % Radix);
103 digit = (digit - 10 + (int)_T(
'A'));
105 digit = digit + (int)_T(
'0');
107 *pText-- = (TCHAR)digit;
109 if(Width != DEFAULT_WIDTH)
110 if(--Width == 0)
break;
112 }
while (nNumber != 0);
114 if(Width != DEFAULT_WIDTH)
115 while(Width-- != 0 && pText != buf)
118 if(bPrefixRadix && Radix == 16)
123 if(bPrefixRadix && Radix == 2)
135 template<
typename TNumberType>
136 StrUtils_Return_Type SignedNumber2Str(
const TNumberType TNumber,
137 int Width = DEFAULT_WIDTH,
138 TCHAR FillWith = DEFAULT_FILLER,
139 int Radix = DEFAULT_RADIX,
140 bool bPrefixRadix=DEFAULT_PREFIXUSAGE,
141 bool bSpaceForSign =
true
144 TCHAR buf[GetBufSizeForType(TNumberType)];
146 TCHAR* pText = buf + _countof(buf) - 1;
150 if(Width >= (_countof(buf) - 2))
151 Width = DEFAULT_WIDTH;
153 TNumberType nNumber = _abs<TNumberType>(TNumber);
156 int digit = _abs<int>((int)(nNumber % Radix));
161 digit = (digit - 10 + (int)_T(
'A'));
163 digit = digit + (int)_T(
'0');
165 *pText-- = (TCHAR)digit;
167 if(Width != DEFAULT_WIDTH)
168 if(--Width == 0)
break;
170 }
while (nNumber != 0);
172 if(Width != DEFAULT_WIDTH)
173 while(Width-- != 0 && pText != buf)
176 if(TNumber < (TNumberType)0)
182 if(bSpaceForSign) *pText-- = _T(
' ');
185 if(bPrefixRadix && Radix == 16)
190 if(bPrefixRadix && Radix == 2)
211 inline StrUtils_Return_Type ToString(
const T& Arg)
221 inline StrUtils_Return_Type ToString(
const T* pArg)
224 #pragma warning(push)
225 #pragma warning(disable:4180)
251 return ToString((
const void*)pArg);
253 #pragma warning(pop) // disable:4180
257 inline StrUtils_Return_Type ToString(T* pArg)
259 return ToString((
const T*) pArg);
263 inline StrUtils_Return_Type ToString(
const bool& bVal)
265 return bVal ? _T(
"True") : _T(
"False");
269 inline StrUtils_Return_Type ToString(
const unsigned short& nNumber)
271 return UnSignedNumber2Str(nNumber);
275 inline StrUtils_Return_Type ToString(
const signed short& nNumber)
277 return SignedNumber2Str(nNumber);
281 inline StrUtils_Return_Type ToString(
const unsigned int& nNumber)
283 return UnSignedNumber2Str(nNumber);
287 inline StrUtils_Return_Type ToString(
const signed int& nNumber)
289 return SignedNumber2Str(nNumber);
293 inline StrUtils_Return_Type ToString(
const unsigned long& lNumber)
295 return UnSignedNumber2Str(lNumber);
299 inline StrUtils_Return_Type ToString(
const signed long& lNumber)
301 return SignedNumber2Str(lNumber);
305 inline StrUtils_Return_Type ToString(
const float& fNumber)
312 TCHAR sz[GetBufSizeForType(fNumber)];
313 PRINTNUM(sz,_T(
"% 0.2f"),fNumber);
318 inline StrUtils_Return_Type ToString(
const double& fNumber)
320 return ToString((
const float) fNumber);
324 inline StrUtils_Return_Type ToString(
const char& ch)
326 StrUtils_Return_Type str;
329 str << (
const int)ch << _T(
"'") << StrUtils_Return_Type(str) << _T(
"'");
334 inline StrUtils_Return_Type ToString(
const unsigned char& ch)
336 StrUtils_Return_Type str;
339 str << (
const unsigned int)ch + _T(
"'") + StrUtils_Return_Type(str) + _T(
"'");
343 #ifdef _NATIVE_WCHAR_T_DEFINED
345 inline StrUtils_Return_Type ToString(
const wchar_t& wch)
347 const wchar_t wstr[2] = {wch, L
'\0'};
348 return ToString((
const int)wch) + _T(
"'") + StrUtils_Return_Type(wstr) + _T(
"'");
353 inline StrUtils_Return_Type ToString(
const char* sz)
355 StrUtils_Return_Type str;
356 str << _T(
"\"") << StrUtils_Return_Type(sz) << _T(
"\"");
360 inline StrUtils_Return_Type ToString(
char* sz)
362 return ToString((
const char*)sz);
366 inline StrUtils_Return_Type ToString(
const wchar_t* wsz)
368 StrUtils_Return_Type str;
369 str << _T(
"\"") << StrUtils_Return_Type(wsz) << _T(
"\"");
373 inline StrUtils_Return_Type ToString(
wchar_t* wsz)
375 return ToString((
const wchar_t*)wsz);
379 inline StrUtils_Return_Type ToString(
const void* pVoid)
381 #pragma warning(push)
382 #pragma warning(disable:4244) // Disable the nasty "LPVOID to Unsigned long" conversion warning
383 return UnSignedNumber2Str(reinterpret_cast<ULONG_PTR>(pVoid),
sizeof(ULONG_PTR) * 2, DEFAULT_FILLER, 16);
387 inline StrUtils_Return_Type ToString(
void* pVoid)
389 return ToString((
const void*)pVoid);
392 #if defined(_VECTOR_) // if included <vector>
394 inline StrUtils_Return_Type ToString(
const std::vector<T>& Container,
const TCHAR* lpszSeperator = _T(
","),
const TCHAR* lpszBegin=_T(
"{"),
const TCHAR* lpszEnd=_T(
"}"))
396 StrUtils_Return_Type Str(lpszBegin);
398 std::vector<T>::const_iterator iter = Container.begin();
399 std::vector<T>::const_iterator iterEnd = Container.end();
402 Str += ToString(*iter++);
404 while(iter != iterEnd)
405 Str += lpszSeperator + ToString(*iter++);
407 return Str + lpszEnd;
409 #endif // if defined(_VECTOR_)
411 #if defined(_LIST_) // if included <list>
413 inline StrUtils_Return_Type ToString(
const std::list<T>& Container,
const TCHAR* lpszSeperator = _T(
","),
const TCHAR* lpszBegin=_T(
"{"),
const TCHAR* lpszEnd=_T(
"}"))
415 StrUtils_Return_Type Str(lpszBegin);
417 std::list<T>::const_iterator iter = Container.begin();
418 std::list<T>::const_iterator iterEnd = Container.end();
421 Str += ToString(*iter++);
423 while(iter != iterEnd)
424 Str += lpszSeperator + ToString(*iter++);
426 return Str + lpszEnd;
428 #endif // if defined(_LIST_)
430 #if defined(_MAP_) // if included <map>
431 template<
typename K,
typename T>
432 inline StrUtils_Return_Type ToString(
const std::map<K, T>& Container,
const TCHAR* lpszSeperator = _T(
","),
const TCHAR* lpszBegin=_T(
"{"),
const TCHAR* lpszEnd=_T(
"}"))
434 StrUtils_Return_Type Str(lpszBegin);
436 std::map<K, T>::const_iterator iter = Container.begin();
437 std::map<K, T>::const_iterator iterEnd = Container.end();
441 Str += _T(
"(") + ToString(iter->first) + lpszSeperator + ToString(iter->second) + _T(
")");
445 while(iter != iterEnd)
447 Str += lpszSeperator + _T(
"(") + ToString(iter->first) + lpszSeperator + ToString(iter->second) + _T(
")");
451 return Str + lpszEnd;
453 #endif // if defined(_MAP_)
459 #endif // ifdef __STRUTILS_H__91CD1AE9_5C86_4261_AB5D_BDDAFBDA8DC5