common_def.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <string>
  3. using namespace std;
  4. #pragma warning(disable:4996)
  5. std::string format_string(const char* format, ...);
  6. string INT64TOSTR(INT64 val);
  7. INT64 STRTOINT64(string &str);
  8. BOOL GetIntWithCheck(string &str, int &val);
  9. BOOL GetInt64WithCheck(string &str, INT64 &val);
  10. int GetInt(string &str);
  11. BOOL StrToInt32(const char * str,int *result);
  12. bool StrToInt64(const char * str,INT64 *result);
  13. string ConvUcharToU8(wstring &uniStr);
  14. wstring GetWcharFromChar(const char *pChar);
  15. template <class Type> bool StrToIntT(const char * str,Type *result)
  16. {
  17. Type value = 0;
  18. Type sign = 1;
  19. Type radix;
  20. if (str == NULL)
  21. {
  22. return false;
  23. }
  24. if (strlen(str) == 0)
  25. {
  26. return false;
  27. }
  28. if (*str == '-')
  29. {
  30. sign = -1;
  31. str++;
  32. }
  33. if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
  34. {
  35. radix = 16;
  36. str += 2;
  37. }
  38. //else if(*str == '0')
  39. //{
  40. // radix = 8;
  41. // str++;
  42. //}
  43. else
  44. {
  45. bool HitF = false;
  46. size_t Len = strlen(str);
  47. for (size_t i = 0; i < Len; i++)
  48. {
  49. if (str[i] >= '0' && str[i] <= '9')
  50. {
  51. continue;
  52. }
  53. HitF = true;
  54. break;
  55. }
  56. if (HitF)
  57. {
  58. radix = 16;
  59. }
  60. else
  61. {
  62. radix = 10;
  63. }
  64. }
  65. while (*str)
  66. {
  67. if (radix == 16)
  68. {
  69. if (*str >= '0' && *str <= '9')
  70. {
  71. value = value * radix + *str - '0';
  72. }
  73. else
  74. {
  75. if ((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
  76. {
  77. value = value * radix + (*str | 0x20) - 'a' + 10;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. }
  85. else
  86. {
  87. value = value * radix + *str - '0';
  88. }
  89. str++;
  90. }
  91. *result = sign*value;
  92. return true;
  93. };
  94. template <class Type> bool GetIntWithCheckT(wstring &str, Type &val ) {
  95. BYTE *szSrc = new BYTE[str.size() * sizeof(WCHAR) + sizeof(WCHAR)];
  96. wcscpy((wchar_t *)szSrc, str.c_str());
  97. char *szBuf = NULL;
  98. DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)szSrc, -1,
  99. NULL,0, NULL, FALSE);
  100. szBuf = new char[dwNum];
  101. WideCharToMultiByte(CP_OEMCP, NULL, (LPCWSTR)szSrc, -1,
  102. szBuf, dwNum, NULL, FALSE);
  103. Type nReturn = 0;
  104. if(StrToIntT(szBuf,&nReturn) == FALSE)
  105. {
  106. return false;
  107. }
  108. if (szSrc != NULL)
  109. {
  110. delete [] szSrc;
  111. szSrc = NULL;
  112. }
  113. if (szBuf != NULL)
  114. {
  115. delete [] szBuf;
  116. szBuf = NULL;
  117. }
  118. val = nReturn;
  119. return true;
  120. };
  121. template <class Type> bool GetIntWithCheckT1(string &str, Type &val) {
  122. Type nReturn = 0;
  123. if (StrToIntT(str.c_str(), &nReturn) == FALSE)
  124. {
  125. return false;
  126. }
  127. val = nReturn;
  128. return true;
  129. };