Util.cpp 5.8 KB

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