PubFun.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. #include "stdafx.h"
  2. #include "PubFun.h"
  3. BOOL IsDir(LPCTSTR lpFile)
  4. {
  5. DWORD attr = GetFileAttributes(lpFile);
  6. if ( (INVALID_FILE_ATTRIBUTES != attr)
  7. && (attr & FILE_ATTRIBUTE_DIRECTORY))
  8. {
  9. return TRUE;
  10. }
  11. return FALSE;
  12. }
  13. BOOL IsFileExist(LPCTSTR lpFile)
  14. {
  15. if (GetFileAttributes(lpFile) != INVALID_FILE_ATTRIBUTES)
  16. {
  17. return TRUE;
  18. }
  19. return FALSE;
  20. }
  21. BOOL CopyFileOrDir(LPCTSTR lpSrcFile, LPCTSTR lpDstPath)
  22. {
  23. if (!IsFileExist(lpSrcFile)
  24. || !IsFileExist(lpDstPath)
  25. || !IsDir(lpDstPath))
  26. return FALSE;
  27. if (IsDir(lpSrcFile))
  28. {
  29. tstring strDstRootDir = JoinPath(lpDstPath, GetFileName(lpSrcFile).c_str());
  30. if (!IsFileExist(strDstRootDir.c_str())
  31. && !VCreateDirectory(strDstRootDir.c_str()))
  32. {
  33. return FALSE;
  34. }
  35. tstring strFind = JoinPath(lpSrcFile, _T("*"));
  36. WIN32_FIND_DATA ffd;
  37. HANDLE hFind = FindFirstFile(strFind.c_str(), &ffd);
  38. if (INVALID_HANDLE_VALUE == hFind)
  39. {
  40. return FALSE;
  41. }
  42. do
  43. {
  44. if (_tcsicmp(ffd.cFileName, _T(".")) == 0
  45. || _tcsicmp(ffd.cFileName, _T("..")) == 0)
  46. {
  47. continue;
  48. }
  49. tstring strSrcSubFile = JoinPath(lpSrcFile, ffd.cFileName);
  50. if (!CopyFileOrDir(strSrcSubFile.c_str(), strDstRootDir.c_str()))
  51. {
  52. FindClose(hFind);
  53. return FALSE;
  54. }
  55. } while (FindNextFile(hFind, &ffd) != 0);
  56. FindClose(hFind);
  57. return TRUE;
  58. }
  59. else
  60. {
  61. return VCopyFile(lpSrcFile, lpDstPath);
  62. }
  63. return FALSE;
  64. }
  65. BOOL VCopyFile(LPCTSTR lpSrcFile, LPCTSTR lpDstPath)
  66. {
  67. if (!IsFileExist(lpSrcFile)
  68. || !IsFileExist(lpDstPath)
  69. || IsDir(lpSrcFile))
  70. return FALSE;
  71. tstring strFileSrc = FormatPath(lpSrcFile);
  72. tstring strFileName = GetFileName(lpSrcFile);
  73. tstring strFileDst = JoinPath(FormatPath(lpDstPath).c_str(), strFileName.c_str());
  74. return CopyFile(strFileSrc.c_str(), strFileDst.c_str(), FALSE);
  75. }
  76. tstring JoinPath(LPCTSTR lpPath1, LPCTSTR lpPath2)
  77. {
  78. tstring strPath = FormatPath(lpPath1) + _T("\\") + FormatPath(lpPath2);
  79. return strPath;
  80. }
  81. tstring FormatPath(LPCTSTR lpPath)
  82. {
  83. tstring strPath = lpPath;
  84. tstring::size_type pos = strPath.find(_T("/"));
  85. while (pos != tstring::npos)
  86. {
  87. strPath.replace(pos, 1, _T("\\"));
  88. pos = strPath.find(_T("/"));
  89. }
  90. pos = strPath.find(_T("\\\\"));
  91. while (pos != tstring::npos)
  92. {
  93. strPath.replace(pos, 2, _T("\\"));
  94. pos = strPath.find(_T("\\\\"));
  95. }
  96. pos = strPath.find(_T("\\"));
  97. if (pos != tstring::npos
  98. && pos == 0)
  99. {
  100. strPath.erase(pos, 1);
  101. }
  102. pos = strPath.rfind(_T("\\"));
  103. if (pos != tstring::npos
  104. && pos == strPath.size() - 1)
  105. {
  106. strPath.erase(pos, 1);
  107. }
  108. return strPath;
  109. }
  110. tstring GetFileName(LPCTSTR lpPath)
  111. {
  112. tstring strPath = FormatPath(lpPath);
  113. tstring strFileName;
  114. if (strPath.rfind(_T("\\")) != tstring::npos)
  115. {
  116. strFileName = strPath.substr(strPath.rfind(_T("\\")));
  117. }
  118. else
  119. {
  120. strFileName = strPath;
  121. }
  122. return strFileName;
  123. }
  124. tstring UTF8ToTString(LPCSTR lpUTF8)
  125. {
  126. tstring strRst;
  127. int len = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0);
  128. if (len == 0)
  129. return strRst;
  130. wchar_t* wszBuf = new wchar_t[len];
  131. if (MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, wszBuf, len) == 0)
  132. {
  133. delete[] wszBuf;
  134. return strRst;
  135. }
  136. #ifdef _UNICODE
  137. strRst = wszBuf;
  138. delete[] wszBuf;
  139. #else
  140. len = WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, NULL, 0, NULL, NULL);
  141. if (len == 0)
  142. {
  143. delete[] wszBuf;
  144. return strRst;
  145. }
  146. char* szBuf = new char[len];
  147. if (WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, szBuf, len, NULL, NULL) == 0)
  148. {
  149. delete[] wszBuf;
  150. delete[] szBuf;
  151. return strRst;
  152. }
  153. delete[] wszBuf;
  154. strRst = szBuf;
  155. delete[] szBuf;
  156. #endif
  157. return strRst;
  158. }
  159. string TStringToUTF8(LPCTSTR lpTStr)
  160. {
  161. string strRst;
  162. #ifdef _UNICODE
  163. int len = WideCharToMultiByte(CP_UTF8, 0, lpTStr, -1, NULL, 0, NULL, NULL);
  164. if (len == 0)
  165. {
  166. return strRst;
  167. }
  168. char* szBuf = new char[len];
  169. if (WideCharToMultiByte(CP_UTF8, 0, lpTStr, -1, szBuf, len, NULL, NULL) == 0)
  170. {
  171. delete[] szBuf;
  172. return strRst;
  173. }
  174. strRst = szBuf;
  175. delete[] szBuf;
  176. #else
  177. int len = MultiByteToWideChar(CP_ACP, 0, lpTStr, -1, NULL, 0);
  178. if (len == 0)
  179. return strRst;
  180. wchar_t* wszBuf = new wchar_t[len];
  181. if (MultiByteToWideChar(CP_ACP, 0, lpTStr, -1, wszBuf, len) == 0)
  182. {
  183. delete[] wszBuf;
  184. return strRst;
  185. }
  186. len = WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, NULL, 0, NULL, NULL);
  187. if (len == 0)
  188. {
  189. delete[] wszBuf;
  190. return strRst;
  191. }
  192. char* szBuf = new char[len];
  193. if (WideCharToMultiByte(CP_UTF8, 0, wszBuf, -1, szBuf, len, NULL, NULL) == 0)
  194. {
  195. delete[] wszBuf;
  196. delete[] szBuf;
  197. return strRst;
  198. }
  199. delete[] wszBuf;
  200. strRst = szBuf;
  201. delete[] szBuf;
  202. #endif
  203. return strRst;
  204. }
  205. DWORD BKDRHash(LPCTSTR lp)
  206. {
  207. DWORD seed = 131; // 31 131 1313 13131 131313 etc..
  208. DWORD hash = 0;
  209. while (*lp)
  210. {
  211. hash = hash * seed + (*lp++);
  212. }
  213. return (hash & 0x7FFFFFFF);
  214. }
  215. tstring GetModulePath()
  216. {
  217. tstring strPath;
  218. TCHAR szBuf[MAX_PATH] = { 0 };
  219. GetModuleFileName(NULL, szBuf, MAX_PATH);
  220. strPath = FormatPath(szBuf);
  221. strPath = strPath.substr(0, strPath.rfind(_T("\\")));
  222. return strPath;
  223. }
  224. BOOL VCreateDirectory(LPCTSTR dir)
  225. {
  226. tstring strPath = FormatPath(dir);
  227. if (!IsFileExist(strPath.c_str()))
  228. {
  229. if (strPath.rfind(_T("\\")) != tstring::npos)
  230. {
  231. tstring strParentPath = strPath.substr(0, strPath.rfind(_T("\\")));
  232. if (!VCreateDirectory(strParentPath.c_str()))
  233. return FALSE;
  234. }
  235. return CreateDirectory(strPath.c_str(), NULL);
  236. }
  237. else if (!IsDir(strPath.c_str()))
  238. {
  239. return FALSE;
  240. }
  241. return TRUE;
  242. }
  243. BOOL VCreateFile(LPCTSTR file)
  244. {
  245. HANDLE fh = CreateFile(file,
  246. GENERIC_READ | GENERIC_WRITE,
  247. 0,
  248. NULL,
  249. CREATE_ALWAYS,
  250. FILE_ATTRIBUTE_NORMAL,
  251. NULL);
  252. if (fh == INVALID_HANDLE_VALUE)
  253. return FALSE;
  254. else
  255. CloseHandle(fh);
  256. return TRUE;
  257. }
  258. BOOL DeleteFileOrDir(LPCTSTR lpFile)
  259. {
  260. if (!IsFileExist(lpFile))
  261. return TRUE;
  262. if (IsDir(lpFile))
  263. {
  264. tstring strFind = JoinPath(lpFile, _T("*"));
  265. WIN32_FIND_DATA ffd;
  266. HANDLE hFind = FindFirstFile(strFind.c_str(), &ffd);
  267. if (INVALID_HANDLE_VALUE == hFind)
  268. {
  269. return FALSE;
  270. }
  271. do
  272. {
  273. if (_tcsicmp(ffd.cFileName, _T(".")) == 0
  274. || _tcsicmp(ffd.cFileName, _T("..")) == 0)
  275. {
  276. continue;
  277. }
  278. tstring strSubFile = JoinPath(lpFile, ffd.cFileName);
  279. if (!DeleteFileOrDir(strSubFile.c_str()))
  280. {
  281. FindClose(hFind);
  282. return FALSE;
  283. }
  284. } while (FindNextFile(hFind, &ffd) != 0);
  285. FindClose(hFind);
  286. return RemoveDirectory(lpFile);
  287. }
  288. else
  289. {
  290. return DeleteFile(lpFile);
  291. }
  292. return FALSE;
  293. }
  294. tstring AStringToTString(LPCSTR lpStr)
  295. {
  296. tstring strRst;
  297. #ifdef _UNICODE
  298. int len = MultiByteToWideChar(CP_ACP, 0, lpStr, -1, NULL, 0);
  299. if (len == 0)
  300. return strRst;
  301. wchar_t* wszBuf = new wchar_t[len];
  302. if (MultiByteToWideChar(CP_ACP, 0, lpStr, -1, wszBuf, len) == 0)
  303. {
  304. delete[] wszBuf;
  305. return strRst;
  306. }
  307. strRst = wszBuf;
  308. delete[] wszBuf;
  309. #else
  310. strRst = lpStr;
  311. #endif
  312. return strRst;
  313. }
  314. string TStringToAString(LPCTSTR lpTStr)
  315. {
  316. string strRst;
  317. #ifdef _UNICODE
  318. int len = WideCharToMultiByte(CP_ACP, 0, lpTStr, -1, NULL, 0, NULL, NULL);
  319. if (len == 0)
  320. {
  321. return strRst;
  322. }
  323. char* szBuf = new char[len];
  324. if (WideCharToMultiByte(CP_ACP, 0, lpTStr, -1, szBuf, len, NULL, NULL) == 0)
  325. {
  326. delete[] szBuf;
  327. return strRst;
  328. }
  329. strRst = szBuf;
  330. delete[] szBuf;
  331. #else
  332. strRst = lpTStr;
  333. #endif
  334. return strRst;
  335. }
  336. BOOL OpenFileByDefault(LPCTSTR lpFile)
  337. {
  338. TCHAR szExe[MAX_PATH] = { 0 };
  339. _stprintf_s(szExe, MAX_PATH, _T("explorer.exe %s"), lpFile);
  340. STARTUPINFO si;
  341. PROCESS_INFORMATION pi;
  342. ZeroMemory(&si, sizeof(si));
  343. si.cb = sizeof(si);
  344. ZeroMemory(&pi, sizeof(pi));
  345. if (CreateProcess(NULL,
  346. szExe,
  347. NULL,
  348. NULL,
  349. FALSE,
  350. 0,
  351. NULL,
  352. NULL,
  353. &si,
  354. &pi))
  355. {
  356. CloseHandle(pi.hProcess);
  357. CloseHandle(pi.hThread);
  358. return TRUE;
  359. }
  360. return FALSE;
  361. }
  362. tstring BufToString(LPVOID buf, int len)
  363. {
  364. tstring rst = _T("");
  365. if (buf == NULL || len <= 0)
  366. return rst;
  367. LPBYTE p = (LPBYTE)buf;
  368. for (int i = 0; i < len; ++i)
  369. {
  370. TCHAR str[10] = { 0 };
  371. _stprintf_s(str, _T("%02X "), p[i]);
  372. rst += str;
  373. }
  374. return rst;
  375. }
  376. bool StringToBuf(LPCTSTR lpstr, LPVOID& buf, int& len)
  377. {
  378. if (lpstr == NULL || _tcslen(lpstr) == 0)
  379. return false;
  380. buf = NULL;
  381. len = 0;
  382. int slen = _tcslen(lpstr);
  383. vector<BYTE> vecByte;
  384. BYTE n = 0, m = 0;
  385. bool add = false;
  386. for (int i = 0; i < slen; ++i)
  387. {
  388. TCHAR ch = lpstr[i];
  389. if (ch >= _T('0') && ch <= _T('9'))
  390. {
  391. m = ch - _T('0');
  392. }
  393. else if (ch >= _T('A') && ch <= _T('F'))
  394. {
  395. m = 0x0A + ch - _T('A');
  396. }
  397. else if (ch >= _T('a') && ch <= _T('f'))
  398. {
  399. m = 0x0A + ch - _T('a');
  400. }
  401. else
  402. {
  403. continue;
  404. }
  405. if (add)
  406. {
  407. n = (n << 4) + m;
  408. vecByte.push_back(n);
  409. ++len;
  410. }
  411. n = m;
  412. add = !add;
  413. }
  414. if (len == 0)
  415. {
  416. return false;
  417. }
  418. buf = new BYTE[len];
  419. for (int i = 0; i < len; ++i)
  420. {
  421. ((LPBYTE)buf)[i] = vecByte[i];
  422. }
  423. return true;
  424. }
  425. vector<tstring> VFindFiles(LPCTSTR str)
  426. {
  427. vector<tstring> vec;
  428. WIN32_FIND_DATA ffd;
  429. ZeroMemory(&ffd, sizeof(ffd));
  430. HANDLE hFind = FindFirstFile(str, &ffd);
  431. if (hFind != INVALID_HANDLE_VALUE)
  432. {
  433. do
  434. {
  435. tstring file = ffd.cFileName;
  436. vec.push_back(file);
  437. } while (FindNextFile(hFind, &ffd));
  438. FindClose(hFind);
  439. }
  440. return vec;
  441. }
  442. //CString GetExeRunPath()
  443. //{
  444. // TCHAR cSAMSRunPath[MAX_PATH] = { 0 };
  445. // GetModuleFileName(NULL, cSAMSRunPath, MAX_PATH); //获取可执行模块的路径
  446. // CString strPath = cSAMSRunPath;
  447. // int nEnd = strPath.ReverseFind('\\'); //取最后的"\"号之前地址
  448. // strPath = strPath.Left(nEnd + 1);
  449. // return strPath;
  450. //}
  451. void WriteLog(std::string strLog, int n/* = 0xFFFFFFF*/)
  452. {
  453. HANDLE hOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);//获得控制台输出句柄
  454. if (hOutputHandle)
  455. {
  456. if (n != 0xFFFFFFF)
  457. {
  458. char szParam[20];
  459. sprintf_s(szParam, "%d\n", n);
  460. strLog += szParam;
  461. }
  462. else
  463. strLog += "\n";
  464. DWORD nRet = 0;
  465. WriteConsole(hOutputHandle, strLog.c_str(), strLog.size(), &nRet, NULL);
  466. }
  467. }