Util.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Util.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Util.h"
  5. #include <odbcinst.h>
  6. #include <comdef.h>
  7. #include <afxdb.h>
  8. #include <regex>
  9. #include <tlhelp32.h>
  10. #include <psapi.h>
  11. #include <codecvt>
  12. #include <cstdlib>
  13. #include <iostream>
  14. #pragma comment(lib,"Psapi.lib")
  15. string UnicodeToGB2312(const wchar_t* pData)
  16. {
  17. string str;
  18. int len = WideCharToMultiByte(936, 0, pData, -1, NULL, 0, NULL, NULL); // 先取得转换后的ANSI字符串所需的长度
  19. if (len <= 0) return str;
  20. char* pStr = (char*)calloc(len + 1, sizeof(char)); // 分配缓冲区
  21. WideCharToMultiByte(936, 0, pData, -1, pStr, len, NULL, NULL); // 开始转换
  22. str = pStr;
  23. if (pStr) free(pStr);
  24. return str;
  25. }
  26. wstring GB2312ToUnicode(const char *pData)
  27. {
  28. wstring str;
  29. int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); // 先取得转换后的UNICODE字符串所需的长度
  30. if (len <= 0) return str;
  31. wchar_t* pWstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t));// 分配缓冲区
  32. MultiByteToWideChar(936, 0, pData, -1, pWstr, len); // 开始转换
  33. str = pWstr;
  34. if (pWstr) free(pWstr);
  35. return str;
  36. }
  37. string UnicodeToUtf8(const wchar_t* wstr)
  38. {
  39. /*string str;
  40. int len;
  41. len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
  42. if (len <= 0) return str;
  43. char *szUtf8 = (char*)calloc(len + 1, sizeof(char));
  44. WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
  45. szUtf8[len] = '\0';
  46. str = szUtf8;
  47. if (szUtf8) free(szUtf8);
  48. return str;*/
  49. std::string ret;
  50. try {
  51. std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
  52. ret = wcv.to_bytes(wstr);
  53. }
  54. catch (const std::exception & e) {
  55. std::cerr << e.what() << std::endl;
  56. }
  57. return ret;
  58. }
  59. string UtfToGbk(string strValue)
  60. {
  61. int len = MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, NULL, 0);
  62. wchar_t* wstr = new wchar_t[len + 1];
  63. memset(wstr, 0, len + 1);
  64. MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, wstr, len);
  65. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
  66. char* str = new char[len + 1];
  67. memset(str, 0, len + 1);
  68. WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
  69. if (wstr)
  70. delete[] wstr;
  71. string strRet = string(str);
  72. if (str)
  73. delete[] str;
  74. return strRet;
  75. }
  76. wstring Utf8ToUnicode(const char* szU8)
  77. {
  78. wstring str;
  79. if (szU8 == NULL)
  80. {
  81. return str;
  82. }
  83. //预转换,得到所需空间的大小;
  84. int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
  85. if (wcsLen <= 0) return str;
  86. //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
  87. wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t));
  88. //转换
  89. ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen);
  90. pWStr[wcsLen] = L'\0';
  91. str = pWStr;
  92. if (pWStr) free(pWStr);
  93. return str;
  94. }
  95. string ConvertUTF8toGB2312(const char* pData)
  96. {
  97. size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, strlen(pData), NULL, 0);
  98. WCHAR* pChar = new WCHAR[n + 1];
  99. n = MultiByteToWideChar(CP_UTF8, 0, pData, strlen(pData), pChar, n);
  100. pChar[n] = 0;
  101. n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
  102. char* p = new char[n + 1];
  103. n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
  104. string result(p);
  105. delete[]pChar;
  106. delete[]p;
  107. return result;
  108. }
  109. void GetModuleDir(wstring& dir)
  110. {
  111. TCHAR Path[MAX_PATH];
  112. TCHAR Driver[_MAX_DRIVE];
  113. TCHAR Dir[_MAX_DIR];
  114. TCHAR FileName[_MAX_FNAME];
  115. TCHAR Ext[_MAX_EXT];
  116. ::GetModuleFileName(NULL, Path, MAX_PATH - 1);
  117. _wsplitpath(Path, Driver, Dir, FileName, Ext);
  118. dir.append(Driver);
  119. dir.append(Dir);
  120. }
  121. BOOL ProgramExist(const TCHAR* szKey)
  122. {
  123. HANDLE hSysSnapshot = NULL;
  124. PROCESSENTRY32 proc;
  125. hSysSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  126. if (hSysSnapshot == (HANDLE)-1)
  127. return FALSE;
  128. proc.dwSize = sizeof(proc);
  129. if (Process32First(hSysSnapshot, &proc))
  130. {
  131. do
  132. {
  133. if (_tcsicmp(proc.szExeFile, szKey) == 0)
  134. {
  135. CloseHandle(hSysSnapshot);
  136. return TRUE;
  137. }
  138. } while (Process32Next(hSysSnapshot, &proc));
  139. }
  140. CloseHandle(hSysSnapshot);
  141. return FALSE;
  142. }
  143. void KillProgress(const TCHAR* szKey)
  144. {
  145. HANDLE hSysSnapshot = NULL;
  146. PROCESSENTRY32 proc;
  147. hSysSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  148. if (hSysSnapshot == (HANDLE)-1)
  149. return;
  150. proc.dwSize = sizeof(proc);
  151. if (Process32First(hSysSnapshot, &proc))
  152. {
  153. do
  154. {
  155. if (_tcsicmp(proc.szExeFile, szKey) == 0)
  156. {
  157. HANDLE Proc_handle = OpenProcess(PROCESS_TERMINATE, FALSE, proc.th32ProcessID);
  158. if (Proc_handle != NULL)
  159. {
  160. TerminateProcess(Proc_handle, 0);
  161. ::WaitForSingleObject(Proc_handle, INFINITE);
  162. CloseHandle(Proc_handle);
  163. Proc_handle = NULL;
  164. }
  165. }
  166. } while (Process32Next(hSysSnapshot, &proc));
  167. }
  168. CloseHandle(hSysSnapshot);
  169. }
  170. void DetectDirectory(const wstring& strIamgePath, vector<string>& listImage)
  171. {
  172. CFileFind finder;
  173. CString path;
  174. if (PathFileExists(strIamgePath.c_str()))
  175. {
  176. vector<string> _list_source;
  177. path.Format(_T("%s/*.*"), strIamgePath.c_str());
  178. bool bWorking = finder.FindFile(path);
  179. while (bWorking)
  180. {
  181. bWorking = finder.FindNextFile();
  182. if ((finder.GetFileName() == _T(".")) || (finder.GetFileName() == _T("..")))
  183. continue;
  184. CString strFilePath = finder.GetFilePath();
  185. strFilePath.Replace(L"\\", L"/");
  186. _list_source.push_back(UnicodeToUtf8(strFilePath));
  187. }
  188. for (auto iter : _list_source)
  189. {
  190. int index = iter.rfind(".jpg");
  191. if (index > 0 && index == iter.length() - 4)
  192. {
  193. listImage.push_back(iter);
  194. }
  195. }
  196. if (listImage.size() != _list_source.size())
  197. {
  198. listImage.clear();
  199. }
  200. }
  201. }
  202. void DeleteDirectory(const wstring &strPath)
  203. {
  204. if (strPath.length() == 0)
  205. return;
  206. CFileFind ff;
  207. CString _path(strPath.c_str());
  208. _path.Replace('/', '\\');
  209. _path.Append(_T("\\*"));
  210. BOOL bFound = ff.FindFile(_path, 0);
  211. while (bFound)
  212. {
  213. bFound = ff.FindNextFile();
  214. if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T("..")))
  215. continue;
  216. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  217. if (ff.IsDirectory())
  218. {
  219. DeleteDirectory((wstring)ff.GetFilePath());
  220. RemoveDirectory(ff.GetFilePath());
  221. }
  222. else
  223. {
  224. DeleteFile(ff.GetFilePath());
  225. }
  226. }
  227. ff.Close();
  228. RemoveDirectory(strPath.c_str());
  229. }
  230. bool split(const string& src, string delimit, vector<string> &v, string null_subst)
  231. {
  232. if (src.empty() || delimit.empty())
  233. return false;
  234. int deli_len = delimit.size();
  235. long index = -1, last_search_position = 0;
  236. while ((index = src.find(delimit, last_search_position)) != -1)
  237. {
  238. if (index == last_search_position)
  239. v.push_back(null_subst);
  240. else
  241. v.push_back(src.substr(last_search_position, index - last_search_position));
  242. last_search_position = index + deli_len;
  243. }
  244. string last_one = src.substr(last_search_position);
  245. v.push_back(last_one.empty() ? null_subst : last_one);
  246. return true;
  247. }