CUtils.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "pch.h"
  2. #include "CUtils.h"
  3. CUtils::CUtils()
  4. {
  5. }
  6. CUtils::~CUtils()
  7. {
  8. }
  9. int CUtils::MultiByToUtf8(const char* multi, char*& utf8)
  10. {
  11. int size = 0;
  12. size = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
  13. wchar_t* buff = NULL;
  14. buff = (wchar_t*)malloc(sizeof(wchar_t) * (size + 1));
  15. if (!buff)
  16. {
  17. return 0;
  18. }
  19. wmemset(buff, 0, size + 1);
  20. MultiByteToWideChar(CP_ACP, NULL, multi, -1, buff, size);
  21. int len = 0;
  22. len = WideCharToMultiByte(CP_UTF8, NULL, buff, size, NULL, 0, NULL, NULL);
  23. utf8 = NULL;
  24. utf8 = (char*)malloc(sizeof(char) * (len + 1));
  25. if (!utf8)
  26. {
  27. free(buff);
  28. return 0;
  29. }
  30. memset(utf8, 0, len + 1);
  31. WideCharToMultiByte(CP_UTF8, NULL, buff, size, utf8, len, NULL, NULL);
  32. free(buff);
  33. return len;
  34. }
  35. int CUtils::Utf8ToMultiBy(const char* utf8, char*& multi)
  36. {
  37. int size = 0;
  38. size = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
  39. wchar_t* buff = NULL;
  40. buff = (wchar_t*)malloc(sizeof(wchar_t) * (size + 1));
  41. if (!buff)
  42. {
  43. return 0;
  44. }
  45. wmemset(buff, 0, size + 1);
  46. MultiByteToWideChar(CP_ACP, NULL, utf8, -1, buff, size);
  47. int len = 0;
  48. len = WideCharToMultiByte(CP_ACP, NULL, buff, size, NULL, 0, NULL, NULL);
  49. multi = NULL;
  50. multi = (char*)malloc(sizeof(char) * (len + 1));
  51. if (!multi)
  52. {
  53. free(buff);
  54. return 0;
  55. }
  56. memset(multi, 0, len + 1);
  57. WideCharToMultiByte(CP_ACP, NULL, buff, size, multi, len, NULL, NULL);
  58. free(buff);
  59. return len;
  60. }
  61. //utf8תunicode
  62. int CUtils::Utf8ToUnicode(const char* utf8, wchar_t*& unicode)
  63. {
  64. int len = 0;
  65. len = MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, NULL, 0);
  66. unicode = NULL;
  67. unicode = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
  68. if (!unicode)
  69. {
  70. return 0;
  71. }
  72. wmemset(unicode, 0, len + 1);
  73. MultiByteToWideChar(CP_UTF8, NULL, utf8, -1, unicode, len);
  74. return len;
  75. }
  76. //unicodeתutf8
  77. int CUtils::UnicodeToUtf8(const wchar_t* unicode, char*& utf8)
  78. {
  79. int len = 0;
  80. len = WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, NULL, 0, NULL, NULL);
  81. utf8 = NULL;
  82. utf8 = (char*)malloc(sizeof(char) * (len + 1));
  83. memset(utf8, 0, len + 1);
  84. if (!utf8)
  85. {
  86. return 0;
  87. }
  88. WideCharToMultiByte(CP_UTF8, NULL, unicode, -1, utf8, len, NULL, NULL);
  89. return len;
  90. }
  91. //¶à×Ö½Úתunicode
  92. int CUtils::MultiByToUnicode(const char* multi, wchar_t*& unicode)
  93. {
  94. int len = 0;
  95. len = MultiByteToWideChar(CP_ACP, NULL, multi, -1, NULL, 0);
  96. unicode = NULL;
  97. unicode = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
  98. if (!unicode)
  99. {
  100. return 0;
  101. }
  102. wmemset(unicode, 0, len + 1);
  103. MultiByteToWideChar(CP_ACP, NULL, multi, -1, unicode, len);
  104. return len;
  105. }
  106. //unicodeת¶à×Ö½Ú
  107. int CUtils::UnicodeToMultiBy(const wchar_t* unicode, char*& multi)
  108. {
  109. int len = 0;
  110. len = WideCharToMultiByte(CP_ACP, NULL, unicode, -1, NULL, 0, NULL, NULL);
  111. multi = NULL;
  112. multi = (char*)malloc(sizeof(char) * (len + 1));
  113. memset(multi, 0, len + 1);
  114. if (!multi)
  115. {
  116. return 0;
  117. }
  118. WideCharToMultiByte(CP_ACP, NULL, unicode, -1, multi, len, NULL, NULL);
  119. return len;
  120. }
  121. int CUtils::fnmatch_win(const char* pattern, const char* name, int flags)
  122. {
  123. if (SafeFNMatch(pattern, strlen(pattern), name, strlen(name)))
  124. return 0;
  125. else
  126. return FNM_NOMATCH;
  127. }
  128. /**copy from Google-glog*/
  129. bool CUtils::SafeFNMatch(const char* pattern, size_t patt_len, const char* str, size_t str_len)
  130. {
  131. size_t p = 0;
  132. size_t s = 0;
  133. while (1)
  134. {
  135. if (p == patt_len && s == str_len)
  136. return true;
  137. if (p == patt_len)
  138. return false;
  139. if (s == str_len)
  140. return p + 1 == patt_len && pattern[p] == '*';
  141. if (pattern[p] == str[s] || pattern[p] == '?')
  142. {
  143. p += 1;
  144. s += 1;
  145. continue;
  146. }
  147. if (pattern[p] == '*')
  148. {
  149. if (p + 1 == patt_len) return true;
  150. do
  151. {
  152. if (SafeFNMatch(pattern + (p + 1), patt_len - (p + 1), str + s, str_len - s))
  153. {
  154. return true;
  155. }
  156. s += 1;
  157. } while (s != str_len);
  158. return false;
  159. }
  160. return false;
  161. }
  162. }
  163. std::string CUtils::CvtA2A(const
  164. std::string& str, unsigned int cpFrom/*=CP_UTF8*/, unsigned int cpTo/*=CP_ACP*/)
  165. {
  166. if (cpTo == cpFrom)
  167. return str;
  168. std::wstring strw = CvtA2W(str, cpFrom);
  169. return CvtW2A(strw, cpTo);
  170. }
  171. std::wstring CUtils::CvtW2W(const std::wstring& str, unsigned int cp)
  172. {
  173. return str;
  174. }
  175. std::wstring CUtils::CvtA2W(const std::string& str, unsigned int cp, unsigned int cp2)
  176. {
  177. UNREFERENCED_PARAMETER(cp2);
  178. wchar_t szBuf[1024];
  179. int nRet = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), szBuf, 1024);
  180. if (nRet > 0)
  181. {
  182. return std::wstring(szBuf, nRet);
  183. }
  184. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  185. {
  186. int nRet = MultiByteToWideChar(cp, 0, str.c_str(), str.length(), NULL, 0);
  187. if (nRet > 0)
  188. {
  189. wchar_t* pBuf = new wchar_t[nRet];
  190. MultiByteToWideChar(cp, 0, str.c_str(), str.length(), pBuf, nRet);
  191. std::wstring strRet(pBuf, nRet);
  192. delete[]pBuf;
  193. return strRet;
  194. }
  195. }
  196. return L"";
  197. }
  198. std::string CUtils::CvtW2A(const std::wstring& str, unsigned int cp/*=CP_ACP*/)
  199. {
  200. char szBuf[1024];
  201. int nRet = WideCharToMultiByte(cp, 0, str.c_str(), str.length(), szBuf, 1024, NULL, NULL);
  202. if (nRet > 0) return std::string(szBuf, nRet);
  203. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  204. {
  205. int nRet = WideCharToMultiByte(cp, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL);
  206. if (nRet > 0)
  207. {
  208. char* pBuf = new char[nRet];
  209. WideCharToMultiByte(cp, 0, str.c_str(), str.length(), pBuf, nRet, NULL, NULL);
  210. std::string strRet(pBuf, nRet);
  211. delete[]pBuf;
  212. return strRet;
  213. }
  214. }
  215. return "";
  216. }