common_def.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "stdafx.h"
  2. #include <algorithm>
  3. #include <stdlib.h>
  4. //#include "common.h"
  5. #include "common_def.h"
  6. inline std::string format_string(const char* format, va_list args) {
  7. size_t oldlen = BUFSIZ;
  8. char buffer[BUFSIZ]; // 默认栈上的缓冲区
  9. va_list argscopy;
  10. va_copy(argscopy, args);
  11. size_t newlen = vsnprintf(&buffer[0], oldlen, format, args) + 1;
  12. newlen++; // 算上终止符'\0'
  13. if (newlen > oldlen) { // 默认缓冲区不够大,从堆上分配
  14. std::vector<char> newbuffer(newlen);
  15. vsnprintf(newbuffer.data(), newlen, format, argscopy);
  16. return newbuffer.data();
  17. }
  18. return buffer;
  19. }
  20. std::string format_string(const char* format, ...) {
  21. va_list args;
  22. va_start(args, format);
  23. string s = format_string(format, args);
  24. va_end(args);
  25. return s;
  26. }
  27. string INT64TOSTR(INT64 val)
  28. {
  29. string total;
  30. string lowstr, highstr;
  31. PUINT32 p = (PUINT32)&val;
  32. lowstr = format_string("%08X", *(p++));
  33. highstr = format_string("0X%08X", *(p));
  34. total = highstr + lowstr;
  35. transform(total.begin(), total.end(), total.begin(), toupper);
  36. return total;
  37. }
  38. INT64 STRTOINT64(string &str)
  39. {
  40. INT64 val = 0;
  41. if(StrToInt64(str.c_str(),&val) == TRUE)
  42. {
  43. return val;
  44. }
  45. assert(0);
  46. return 0;
  47. }
  48. bool StrToInt64(const char * str,INT64 *result)
  49. {
  50. INT64 value = 0;
  51. INT64 sign = 1;
  52. INT64 radix;
  53. if(*str == '-')
  54. {
  55. sign = -1;
  56. str++;
  57. }
  58. if(*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
  59. {
  60. radix = 16;
  61. str += 2;
  62. }
  63. else if(*str == '0')
  64. {
  65. radix = 8;
  66. str++;
  67. }
  68. else
  69. {
  70. radix = 10;
  71. }
  72. while(*str)
  73. {
  74. if(radix == 16)
  75. {
  76. if(*str >= '0' && *str <= '9')
  77. {
  78. value = value * radix + *str - '0';
  79. }
  80. else
  81. {
  82. if((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
  83. {
  84. value = value * radix + (*str | 0x20) - 'a' + 10;
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. value = value * radix + *str - '0';
  95. }
  96. str++;
  97. }
  98. *result = sign*value;
  99. return true;
  100. }
  101. BOOL StrToInt32(const char * str,int *result)
  102. {
  103. int value = 0;
  104. int sign = 1;
  105. int radix;
  106. if(*str == '-')
  107. {
  108. sign = -1;
  109. str++;
  110. }
  111. if(*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
  112. {
  113. radix = 16;
  114. str += 2;
  115. }
  116. else if(*str == '0')
  117. {
  118. radix = 8;
  119. str++;
  120. }
  121. else
  122. {
  123. radix = 10;
  124. }
  125. while(*str)
  126. {
  127. if(radix == 16)
  128. {
  129. if(*str >= '0' && *str <= '9')
  130. {
  131. value = value * radix + *str - '0';
  132. }
  133. else
  134. {
  135. if((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
  136. {
  137. value = value * radix + (*str | 0x20) - 'a' + 10;
  138. }
  139. else
  140. {
  141. return FALSE;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. value = value * radix + *str - '0';
  148. }
  149. str++;
  150. }
  151. *result = sign*value;
  152. return TRUE;
  153. }
  154. wstring GetWcharFromChar(const char *pChar)
  155. {
  156. wstring wstr;
  157. wchar_t *pWcharBuff;
  158. int uLen = MultiByteToWideChar(CP_UTF8,0,pChar,-1,NULL,0);
  159. if(uLen > 0)
  160. {
  161. pWcharBuff = new wchar_t[uLen + 1];
  162. memset(pWcharBuff, 0, sizeof(wchar_t)*uLen);
  163. MultiByteToWideChar(CP_UTF8,0,pChar,-1, (LPWSTR )pWcharBuff,uLen);
  164. wstr = pWcharBuff;
  165. delete []pWcharBuff;
  166. return wstr;
  167. }
  168. return wstr;
  169. }
  170. string ConvUcharToU8(wstring &uniStr)
  171. {
  172. std::string str;
  173. int uLen = WideCharToMultiByte(CP_UTF8,0,uniStr.c_str(),-1,NULL,0,NULL,NULL);
  174. if(uLen > 0)
  175. {
  176. str.resize(uLen);
  177. WideCharToMultiByte(CP_UTF8, 0, uniStr.c_str(), -1, (LPSTR)&str[0], uLen, NULL, NULL);
  178. return str;
  179. }
  180. else
  181. {
  182. return "";
  183. }
  184. return str;
  185. }
  186. BOOL GetInt64WithCheck(string &str,INT64 &val)
  187. {
  188. INT64 nReturn = 0;
  189. if (StrToInt64(str.c_str(), &nReturn) == FALSE)
  190. {
  191. return FALSE;
  192. }
  193. val = nReturn;
  194. return TRUE;
  195. }
  196. BOOL GetIntWithCheck(string &str,int &val)
  197. {
  198. int nReturn = 0;
  199. if (StrToInt32(str.c_str(), &nReturn) == FALSE)
  200. {
  201. return FALSE;
  202. }
  203. val = nReturn;
  204. return TRUE;
  205. }
  206. int GetInt(string &str)
  207. {
  208. int nReturn = 0;
  209. if (StrToInt32(str.c_str(), &nReturn) == FALSE)
  210. {
  211. MessageBox(NULL, "The String Number is Wrong", "ERROR", MB_OK);
  212. return 0;
  213. }
  214. return nReturn;
  215. }