Utils.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /**************************************************************************************************
  2. 版本所有(C), 2012-2013, 西安XXXXX公司
  3. ***************************************************************************************************
  4. 文 件 名:CommonFunc.cpp
  5. 版 本 号: 1.00初版
  6. 作 者: HYF
  7. 生成日期:2013-3-20
  8. 最近修改:
  9. 功能描述: 公共函數定義
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 : 2012-3-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include <winsock2.h>
  18. #include <iphlpapi.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "Utils.h"
  22. using namespace std;
  23. Utils::Utils() {
  24. }
  25. Utils::~Utils()
  26. {
  27. }
  28. /**************************************************************************************************
  29. 函 数 名 :GetExecDir
  30. 功能描述 :获取当前目录
  31. 输出参数 :无
  32. 返 回 值 :路径
  33. **************************************************************************************************/
  34. CString Utils::GetExecDir(void)
  35. {
  36. CString path;
  37. GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
  38. path.ReleaseBuffer();
  39. int pos = path.ReverseFind('\\');
  40. return path.Left(pos);
  41. }
  42. BOOL Utils::IsFileExist(LPCTSTR lpFileName)
  43. {
  44. if (NULL == lpFileName)
  45. {
  46. return FALSE;
  47. }
  48. BOOL bExist = TRUE;
  49. HANDLE hFind = INVALID_HANDLE_VALUE;
  50. WIN32_FIND_DATA DataFind;
  51. hFind = FindFirstFile(lpFileName, &DataFind);
  52. if (INVALID_HANDLE_VALUE != hFind)
  53. {
  54. return true;
  55. }
  56. FindClose(hFind);
  57. return FALSE;
  58. }
  59. BOOL Utils::DirectoryExists(LPCTSTR szFilePath)
  60. {
  61. if (::GetFileAttributes(szFilePath) == FILE_ATTRIBUTE_DIRECTORY)
  62. return TRUE;
  63. return FALSE;
  64. }
  65. /**************************************************************************************************
  66. 函 数 名 :GetLocalUrlDir
  67. 功能描述 :获取当前目录
  68. 输出参数 :无
  69. 返 回 值 :路径
  70. **************************************************************************************************/
  71. CString Utils::GetLocalUrlDir(void)
  72. {
  73. CString path;
  74. GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
  75. path.ReleaseBuffer();
  76. int pos = path.ReverseFind('\\');
  77. CString urlPath = path.Left(pos);
  78. urlPath.Replace('\\', '\/');
  79. return urlPath;
  80. }
  81. /**************************************************************************************************
  82. 函 数 名 :GetCurrentTimeStamp
  83. 功能描述 :获取当前时间Tick数目
  84. 输出参数 :无
  85. 返 回 值 :INT64
  86. **************************************************************************************************/
  87. INT64 Utils::GetCurrentTimeStamp()
  88. {
  89. SYSTEMTIME tmSys;
  90. GetLocalTime(&tmSys);
  91. CTime tm3(tmSys);
  92. INT64 tmDst = (tm3.GetTime()) * 1000 + tmSys.wMilliseconds;
  93. return tmDst;
  94. }
  95. /**************************************************************************************************
  96. 函 数 名 :TimeStampToString
  97. 功能描述 :时间戳转北京时间字串
  98. 输出参数 :无
  99. 返 回 值 :INT64
  100. **************************************************************************************************/
  101. CString Utils::TimeStampToString(INT64 timestamp)
  102. {
  103. int dtime = (int)(timestamp / 1000);
  104. dtime += 28800;//GTM偏移8个时区得到北京时间
  105. tm p;
  106. gmtime_s(&p, (time_t *)&dtime);
  107. char s[80];
  108. strftime(s, 80, "%Y-%m-%d %H:%M:%S", &p);
  109. return CString(s);
  110. }
  111. /**************************************************************************************************
  112. 函 数 名 :TimeStampToString
  113. 功能描述 :北京时间字串转时间戳
  114. 输出参数 :无
  115. 返 回 值 :INT64
  116. **************************************************************************************************/
  117. INT64 Utils::StringToTimeStamp(CString strInputTime)
  118. {
  119. COleVariant vtime(strInputTime);
  120. vtime.ChangeType(VT_DATE);
  121. COleDateTime cOleTime = vtime;
  122. SYSTEMTIME systime;
  123. VariantTimeToSystemTime(cOleTime, &systime);
  124. //时间戳最小值为北京时间:1970-01-01 08:00:00
  125. if (systime.wYear <= 1970 && systime.wMonth <= 1 && systime.wDay <= 1 && systime.wHour <= 7 && systime.wMinute <= 59 && systime.wSecond <= 59)
  126. return 0;
  127. CTime cTimeFromDB(systime);
  128. INT64 timestamp = cTimeFromDB.GetTime();//CTime->时间戳
  129. return timestamp;
  130. }
  131. /**************************************************************************************************
  132. 函 数 名 :ConverANSI2Unicode
  133. 功能描述 :ANSI转Unicode
  134. 输出参数 :略
  135. 返 回 值 :略
  136. **************************************************************************************************/
  137. std::wstring Utils::ConverANSI2Unicode(const std::string str)
  138. {
  139. int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  140. wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
  141. memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
  142. MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
  143. std::wstring wstr = pUnicode;
  144. delete[] pUnicode;
  145. return wstr;
  146. }
  147. /**************************************************************************************************
  148. 函 数 名 :ConverUTF82Unicode
  149. 功能描述 :UTF8转Unicode
  150. 输出参数 :略
  151. 返 回 值 :略
  152. **************************************************************************************************/
  153. std::wstring Utils::ConverUTF82Unicode(const std::string str)
  154. {
  155. int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
  156. wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
  157. memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
  158. MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
  159. std::wstring wstr = pUnicode;
  160. delete[] pUnicode;
  161. return wstr;
  162. }
  163. /**************************************************************************************************
  164. 函 数 名 :ConverUnicode2UTF8
  165. 功能描述 :Unicode转UTF8
  166. 输出参数 :略
  167. 返 回 值 :略
  168. **************************************************************************************************/
  169. std::string Utils::ConverUnicode2UTF8(const std::wstring wstr)
  170. {
  171. int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  172. char *pUtf8 = new char[utf8Len+1];
  173. memset(pUtf8,0, utf8Len+1);
  174. WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pUtf8, utf8Len, NULL, NULL);
  175. string str = pUtf8;
  176. delete[] pUtf8;
  177. return str;
  178. }
  179. /**************************************************************************************************
  180. 函 数 名 :ConverUnicode2ANSI
  181. 功能描述 :Unicode转ANSI
  182. 输出参数 :略
  183. 返 回 值 :略
  184. **************************************************************************************************/
  185. std::string Utils::ConverUnicode2ANSI(const std::wstring wstr)
  186. {
  187. int ansiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  188. char *pAnsi = new char[ansiLen+1];
  189. memset(pAnsi,0, ansiLen+1);
  190. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAnsi, ansiLen, NULL, NULL);
  191. string str = pAnsi;
  192. delete[] pAnsi;
  193. return str;
  194. }
  195. /**************************************************************************************************
  196. 函 数 名 :ConverANSI2UTF8
  197. 功能描述 :ANSI转UTF8
  198. 输出参数 :略
  199. 返 回 值 :略
  200. **************************************************************************************************/
  201. std::string Utils::ConverANSI2UTF8(const std::string & str)
  202. {
  203. int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  204. wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
  205. ZeroMemory(pwBuf, nwLen * 2 + 2);
  206. ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.length(), pwBuf, nwLen);
  207. int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
  208. char * pBuf = new char[nLen + 1];
  209. ZeroMemory(pBuf, nLen + 1);
  210. ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
  211. std::string retStr(pBuf);
  212. delete []pwBuf;
  213. delete []pBuf;
  214. pwBuf = NULL;
  215. pBuf = NULL;
  216. return retStr;
  217. }
  218. /**************************************************************************************************
  219. 函 数 名 :ConverUTF82ANSI
  220. 功能描述 :UTF8转ANSI
  221. 输出参数 :略
  222. 返 回 值 :略
  223. **************************************************************************************************/
  224. std::string Utils::ConverUTF82ANSI(const std::string str)
  225. {
  226. int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
  227. wchar_t *pUnicode = new wchar_t[unicodeLen + 1];
  228. memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
  229. MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
  230. int ansiLen = WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL);
  231. char *pAnsi = new char[ansiLen+1];
  232. memset(pAnsi,0, ansiLen+1);
  233. WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsi, ansiLen, NULL, NULL);
  234. string str1 = pAnsi;
  235. delete[] pUnicode;
  236. delete[] pAnsi;
  237. return str1;
  238. }
  239. /**************************************************************************************************
  240. 函 数 名 :DeleteFolder
  241. 功能描述 :删除文件夹
  242. 输出参数 :略
  243. 返 回 值 :略
  244. **************************************************************************************************/
  245. BOOL Utils::DeleteFolder(const CString& strDir)
  246. {
  247. if(strDir.IsEmpty())
  248. {
  249. BOOL bRet = RemoveDirectory(strDir);
  250. if (!bRet) {
  251. LOG::E("删除文件夹失败:%d", GetLastError());
  252. return FALSE;
  253. }
  254. return TRUE;
  255. }
  256. CFileFind ff;
  257. BOOL bFound = ff.FindFile(strDir + _T("\\*"),0);
  258. while(bFound)
  259. {
  260. bFound = ff.FindNextFile();
  261. if(ff.GetFileName()== _T(".")||ff.GetFileName()== _T(".."))
  262. {
  263. continue;
  264. }
  265. //去掉文件(夹)只读等属性
  266. SetFileAttributes(ff.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
  267. if(ff.IsDirectory())
  268. {
  269. //递归删除子文件夹
  270. DeleteFolder(ff.GetFilePath());
  271. RemoveDirectory(ff.GetFilePath());
  272. }else
  273. {
  274. DeleteFile(ff.GetFilePath()); //删除文件
  275. }
  276. }
  277. ff.Close();
  278. //然后删除该文件夹
  279. BOOL bRet = RemoveDirectory(strDir);
  280. if (!bRet) {
  281. LOG::E("删除文件夹失败:%d", GetLastError());
  282. }
  283. return bRet;
  284. }
  285. BOOL Utils::SHDeleteFolder(const CString& szDir) {
  286. char sourceFolder[MAX_PATH] = "";
  287. strcpy(sourceFolder, szDir);
  288. SHFILEOPSTRUCT lpFile;
  289. lpFile.wFunc = FO_DELETE;
  290. lpFile.pFrom = sourceFolder;
  291. lpFile.pTo = NULL;
  292. lpFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
  293. lpFile.fAnyOperationsAborted = false;
  294. lpFile.hNameMappings = NULL;
  295. lpFile.lpszProgressTitle = NULL;
  296. int iRet = SHFileOperation(&lpFile);
  297. if (iRet == 0) return FALSE;
  298. return TRUE;
  299. }
  300. /**************************************************************************************************
  301. 函 数 名 :DeleteFolder
  302. 功能描述 :删除文件夹
  303. 输出参数 :略
  304. 返 回 值 :略
  305. **************************************************************************************************/
  306. void Utils::MoveFolder(const CString& szSrc, const CString& szDes)
  307. {
  308. SHFILEOPSTRUCT sfo;
  309. sfo.hwnd = NULL;
  310. sfo.wFunc = FO_MOVE;
  311. sfo.pFrom = szSrc;
  312. sfo.pTo = szDes;
  313. sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
  314. SHFileOperation(&sfo);
  315. }
  316. /**************************************************************************************************
  317. 函 数 名 :IP_STR2DW
  318. 功能描述 :IP地址转换
  319. 输出参数 :略
  320. 返 回 值 :略
  321. **************************************************************************************************/
  322. DWORD Utils::IP_STR2DW(LPCTSTR szIP)
  323. {
  324. int len= (int)_tcslen(szIP);
  325. if( len <7 || len>15 )
  326. return 0 ;
  327. DWORD dwip = 0;
  328. char *p ;
  329. BYTE dd;
  330. char field[4];
  331. int i=0;
  332. int j = 3;
  333. for (p =(char *)szIP; *p!= NULL ; p++)
  334. {
  335. if ((*p >='0') && (*p <='9'))
  336. {
  337. field[i++] = * p;
  338. }
  339. if ((*(p+1) == '.') || (*(p+1) == 0 ))
  340. {
  341. field[i] = 0;
  342. i = 0;
  343. dd = (BYTE)atoi(field);
  344. dwip = dwip | ((DWORD) dd << (j--)*8);
  345. }
  346. }
  347. if (j!= -1)
  348. return 0;
  349. return dwip;
  350. }
  351. /**************************************************************************************************
  352. 函 数 名 :DMS_CreateMsg
  353. 功能描述 :创建消息包
  354. 输出参数 :略
  355. 返 回 值 :略
  356. **************************************************************************************************/
  357. DMSResponse* Utils::DMS_CreateMsg()
  358. {
  359. DMSResponse *pMSG = new DMSResponse();
  360. memset(pMSG->szMsg, 0, DMS_MSG_SIZE);
  361. pMSG->bRet = TRUE;
  362. pMSG->iCode = 0;
  363. return pMSG;
  364. }
  365. /**************************************************************************************************
  366. 函 数 名 :DMS_FreeMsg
  367. 功能描述 :释放消息包
  368. 输出参数 :略
  369. 返 回 值 :略
  370. **************************************************************************************************/
  371. void Utils::DMS_FreeMsg(DMSResponse *pMSG)
  372. {
  373. delete pMSG;
  374. return ;
  375. }
  376. /**************************************************************************************************
  377. 函 数 名 :StrSplit
  378. 功能描述 :字符串分割成数组
  379. 输出参数 :略
  380. 返 回 值 :略
  381. **************************************************************************************************/
  382. int Utils::StrSplit(const CString& strLine, char split, CStringArray &strArray)
  383. {
  384. strArray.RemoveAll();//自带清空属性
  385. CString temp = strLine;
  386. int tag = 0;
  387. while (1)
  388. {
  389. tag = temp.Find(split);
  390. if (tag >= 0)
  391. {
  392. CString substr = temp.Left(tag);
  393. substr.Trim();
  394. if (substr.GetLength()>0) strArray.Add(substr);
  395. temp = temp.Right(temp.GetLength() - tag - 1);
  396. }
  397. else { break; }
  398. }
  399. strArray.Add(temp);
  400. return (int)strArray.GetSize();
  401. }
  402. /**************************************************************************************************
  403. 函 数 名 :StrSplit
  404. 功能描述 :字符串分割成数组
  405. 输出参数 :略
  406. 返 回 值 :略
  407. **************************************************************************************************/
  408. int Utils::StrSplit(const char *str, int iLen, char split, CStringArray &strArray)
  409. {
  410. strArray.RemoveAll();//自带清空属性
  411. int iStart = 0;
  412. for (int iPos = 0; iPos < iLen; iPos++)
  413. {
  414. if (str[iPos] == '\0')
  415. {
  416. CString szTemp(str+ iStart);
  417. if (!szTemp.IsEmpty())
  418. strArray.Add(szTemp);
  419. iStart = iPos+1;
  420. }
  421. }
  422. return (int)strArray.GetSize();
  423. }
  424. /**************************************************************************************************
  425. 函 数 名 :StrSplit
  426. 功能描述 :字符串分割成数组
  427. 输出参数 :略
  428. 返 回 值 :略
  429. **************************************************************************************************/
  430. int Utils::StrSplit(const CString& strLine, LPCTSTR split, CStringArray &strArray)
  431. {
  432. strArray.RemoveAll();//自带清空属性
  433. CString temp = strLine;
  434. CString szSplit(split);
  435. int isplitsize = szSplit.GetLength();
  436. int tag = 0;
  437. while (1)
  438. {
  439. tag = temp.Find(split);
  440. if (tag >= 0)
  441. {
  442. CString substr = temp.Left(tag);
  443. substr.Trim();
  444. if (substr.GetLength()>0) strArray.Add(substr);
  445. temp = temp.Right(temp.GetLength() - (tag+isplitsize));
  446. }
  447. else { break; }
  448. }
  449. strArray.Add(temp);
  450. return (int)strArray.GetSize();
  451. }
  452. //资源加载
  453. BOOL Utils::LoadImageFromResource(CImage *pImage, UINT nResID, LPCTSTR lpTyp)
  454. {
  455. if (pImage == NULL)
  456. return false;
  457. pImage->Destroy();
  458. // 查找资源
  459. HRSRC hRsrc = ::FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(nResID), lpTyp);
  460. if (hRsrc == NULL)
  461. return false;
  462. // 加载资源
  463. HGLOBAL hImgData = ::LoadResource(AfxGetResourceHandle(), hRsrc);
  464. if (hImgData == NULL)
  465. {
  466. ::FreeResource(hImgData);
  467. return false;
  468. }
  469. // 锁定内存中的指定资源
  470. LPVOID lpVoid = ::LockResource(hImgData);
  471. LPSTREAM pStream = NULL;
  472. DWORD dwSize = ::SizeofResource(AfxGetResourceHandle(), hRsrc);
  473. HGLOBAL hNew = ::GlobalAlloc(GHND, dwSize);
  474. LPBYTE lpByte = (LPBYTE)::GlobalLock(hNew);
  475. ::memcpy(lpByte, lpVoid, dwSize);
  476. // 解除内存中的指定资源
  477. ::GlobalUnlock(hNew);
  478. // 从指定内存创建流对象
  479. HRESULT ht = ::CreateStreamOnHGlobal(hNew, TRUE, &pStream);
  480. if (ht != S_OK)
  481. {
  482. GlobalFree(hNew);
  483. }
  484. else
  485. {
  486. // 加载图片
  487. pImage->Load(pStream);
  488. GlobalFree(hNew);
  489. }
  490. // 释放资源
  491. ::FreeResource(hImgData);
  492. return true;
  493. }