Global.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include <SetupAPI.h>
  4. #include <InitGuid.h>
  5. #include <WinIoCtl.h>
  6. #include <direct.h>
  7. #include <fcntl.h>
  8. #include <share.h>
  9. #include <sys\stat.h>
  10. #pragma comment(lib, "SetupAPI.lib")
  11. namespace Global
  12. {
  13. //////////////////////////////////////////////////////////////////////////
  14. // 全局变量;
  15. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  16. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  17. TCHAR g_szFna[MAX_PATH] = { 0 };
  18. TCHAR g_szConfig[MAX_PATH] = { 0 };
  19. TCHAR g_szLogPath[MAX_PATH] = { 0 };
  20. bool g_bEnableLog = false;
  21. COleDateTime g_lastTime;
  22. time_t g_time = time(NULL);
  23. ThreadSection g_critSection;
  24. FILE *gp_log_fp = NULL;
  25. //////////////////////////////////////////////////////////////////////////
  26. void MKDIR(LPCTSTR dir)
  27. {
  28. //////////////////////////////////////////////////////////////////////////
  29. // 创建目录;
  30. int nleft = 0;
  31. int nIndex = -1;
  32. string strdir = dir;
  33. strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
  34. if (strdir.at(strdir.size() - 1) != _T('\\'))
  35. strdir.append(_T("\\"));
  36. // 共享路径和硬盘盘符;
  37. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  38. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  39. else if (strdir.at(2) == _T('\\'))
  40. nleft = 3;
  41. do
  42. {
  43. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  44. if (nIndex != string::npos)
  45. {
  46. if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  47. {
  48. WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  49. break;
  50. }
  51. nleft += nIndex + 1;
  52. }
  53. } while (nIndex != -1);
  54. };
  55. // 全局函数;
  56. /************************************************************************/
  57. /* 函数:WriteTextLog[7/28/2009 Jeff];
  58. /* 描述:写文本日志;
  59. /* 参数:;
  60. /* [IN] :;
  61. /* 返回:void;
  62. /* 注意:;
  63. /* 示例:;
  64. /*
  65. /* 修改:;
  66. /* 日期:;
  67. /* 内容:;
  68. /************************************************************************/
  69. void WriteTextLog(const TCHAR* format, ...)
  70. {
  71. static ThreadSection _critSection;
  72. AutoThreadSection aSection(&_critSection);
  73. // 将日志内容输入到文件中;
  74. // 获取今年年份;
  75. __time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  76. struct tm gmtm = { 0 };
  77. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  78. // 解析出日志路径;
  79. TCHAR szlogpath[MAX_PATH] = { 0 };
  80. _stprintf_s(szlogpath, _T("%srunner-log %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
  81. // 打开或创建文件;
  82. FILE* fp = NULL;
  83. //MKDIR(g_szLogPath);
  84. //if (_taccess(szlogpath, 0) != -1)
  85. #ifndef UNICODE
  86. if (_access(szlogpath, 0) != -1)
  87. #else
  88. if (_taccess(szlogpath, 0) != -1)
  89. #endif
  90. { // 存在;
  91. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  92. // 移动到末尾;
  93. fseek(fp, 0, SEEK_END);
  94. }
  95. else
  96. { // 不存在;
  97. _tfopen_s(&fp, szlogpath, _T("w+"));
  98. }
  99. if (fp == NULL)
  100. return;
  101. // 格式化前设置语言区域;
  102. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  103. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  104. // 格式化日志内容;
  105. va_list args = NULL;
  106. int len = 0;
  107. TCHAR* buffer = NULL;
  108. va_start(args, format);
  109. // _vscprintf doesn't count. terminating '\0'
  110. len = _vsctprintf(format, args) + 1;
  111. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  112. _vstprintf_s(buffer, len, format, args);
  113. _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
  114. // 关闭文件,释放资源并设置回原语言区域;
  115. free(buffer);
  116. fclose(fp);
  117. _tsetlocale(LC_CTYPE, old_locale);
  118. free(old_locale); //还原区域设定;
  119. }
  120. void WritePythonLog(const TCHAR* msg)
  121. {
  122. AutoThreadSection aSection(&g_critSection);
  123. // 将日志内容输入到文件中;
  124. // 获取今年年份;
  125. __time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  126. struct tm gmtm = { 0 };
  127. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  128. int nErr = -1;
  129. // 打开或创建文件;
  130. FILE* fp = NULL;
  131. #ifndef UNICODE
  132. if (_access(g_szLogPath, 0) != -1)
  133. #else
  134. if (_taccess(g_szLogPath, 0) != -1)
  135. #endif
  136. { // 存在;
  137. if (0 == _tfopen_s(&fp, g_szLogPath, _T("a+")))
  138. {
  139. // 移动到末尾;
  140. fseek(fp, 0, SEEK_END);
  141. }
  142. else
  143. {
  144. WriteTextLog(_T("打开文件失败:%s,%d"), g_szLogPath, GetLastError());
  145. return;
  146. }
  147. }
  148. else
  149. { // 不存在;
  150. MKDIR(g_szLogPath);
  151. if (0 != _tfopen_s(&fp, g_szLogPath, _T("w+")) )
  152. {
  153. WriteTextLog(_T("创建文件失败:%s,%d"), g_szLogPath, GetLastError());
  154. return;
  155. }
  156. }
  157. if (fp == NULL)
  158. {
  159. WriteTextLog(_T("文件句柄空:%s,%d"), g_szLogPath, GetLastError());
  160. return;
  161. }
  162. // 格式化前设置语言区域;
  163. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  164. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  165. _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);
  166. // 关闭文件,释放资源并设置回原语言区域;
  167. fclose(fp);
  168. _tsetlocale(LC_CTYPE, old_locale);
  169. free(old_locale); //还原区域设定;
  170. }
  171. void ClosePythonLog()
  172. {
  173. AutoThreadSection aSection(&g_critSection);
  174. if ( gp_log_fp )
  175. {
  176. WriteTextLog(_T("关闭文件:%s"), g_szLogPath);
  177. fclose(gp_log_fp);
  178. gp_log_fp = NULL;
  179. }
  180. }
  181. // 去除空格;
  182. std::string& trim(std::string& str)
  183. {
  184. int nIndex = 0;
  185. while ((nIndex = str.find_first_of(' ')) != std::string::npos)
  186. str.erase(nIndex, 1);
  187. return str;
  188. }
  189. // hModule 模块句柄 NULL表示当前模块;
  190. bool GetVersion(IN const TCHAR* fname, OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
  191. {
  192. VS_FIXEDFILEINFO* pVi = NULL;
  193. DWORD dwHandle = 0;
  194. int size = GetFileVersionInfoSize(fname, &dwHandle);
  195. if (size > 0)
  196. {
  197. BYTE* buffer = new BYTE[size];
  198. memset(buffer, 0, size);
  199. if (GetFileVersionInfo(fname, 0, size, buffer))
  200. {
  201. if (VerQueryValue(buffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& size))
  202. {
  203. pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
  204. pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
  205. pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
  206. pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
  207. pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
  208. pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
  209. pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
  210. pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
  211. delete[] buffer;
  212. return true;
  213. }
  214. }
  215. delete[] buffer;
  216. }
  217. return false;
  218. }
  219. BOOL GetVersion(IN HMODULE hModule, OUT DWORD(&dwFVArray)[4], OUT DWORD(&dwPVArray)[4])
  220. {
  221. TCHAR fname[MAX_PATH];
  222. VS_FIXEDFILEINFO* pVi;
  223. DWORD dwHandle;
  224. if (GetModuleFileName(hModule, fname, MAX_PATH))
  225. {
  226. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  227. if (nSize > 0)
  228. {
  229. BYTE* pBuffer = new BYTE[nSize];
  230. memset(pBuffer, 0, nSize);
  231. if (GetFileVersionInfo(fname, 0, nSize, pBuffer))
  232. {
  233. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& nSize))
  234. {
  235. dwFVArray[0] = HIWORD(pVi->dwFileVersionMS);
  236. dwFVArray[1] = LOWORD(pVi->dwFileVersionMS);
  237. dwFVArray[2] = HIWORD(pVi->dwFileVersionLS);
  238. dwFVArray[3] = LOWORD(pVi->dwFileVersionLS);
  239. dwPVArray[0] = HIWORD(pVi->dwProductVersionMS);
  240. dwPVArray[1] = LOWORD(pVi->dwProductVersionMS);
  241. dwPVArray[2] = HIWORD(pVi->dwProductVersionLS);
  242. dwPVArray[3] = LOWORD(pVi->dwProductVersionLS);
  243. delete[]pBuffer;
  244. return TRUE;
  245. }
  246. }
  247. if (pBuffer)
  248. delete[]pBuffer;
  249. }
  250. }
  251. return FALSE;
  252. }
  253. WCHAR* ASCII2UNICODE(IN LPCCH lpASCIIStr)
  254. {
  255. if (lpASCIIStr == NULL)
  256. return NULL;
  257. // 获取宽字符字节数;
  258. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  259. if (cchWideChar == 0)
  260. return NULL;
  261. // 转换成宽字符串;
  262. WCHAR* pWideChar = new WCHAR[cchWideChar + 1];
  263. memset(pWideChar, 0, sizeof(WCHAR) * (cchWideChar + 1));
  264. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pWideChar, cchWideChar);
  265. if (nWriteNum != cchWideChar)
  266. {
  267. if (pWideChar)
  268. delete[] pWideChar;
  269. return NULL;
  270. }
  271. return pWideChar;
  272. }
  273. BOOL UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT string& strResult)
  274. {
  275. if (lpUNICODEStr == NULL)
  276. return FALSE;
  277. // 获取多字节字符字节数;
  278. int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  279. if (cbMultiByte == 0)
  280. return FALSE;
  281. // 转换成多字节字符;
  282. CHAR* pResult = new CHAR[cbMultiByte];
  283. memset(pResult, 0, cbMultiByte);
  284. int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
  285. if (nWriteNum != cbMultiByte)
  286. return FALSE;
  287. strResult = pResult;
  288. if (pResult)
  289. delete[] pResult;
  290. return TRUE;
  291. }
  292. BOOL ASCII2UTF8(IN LPCCH lpASCIIStr, OUT string& strResult)
  293. {
  294. // 将ASCII字符串转成UNICODE字符串;
  295. WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
  296. if (pWideChar == NULL)
  297. return FALSE;
  298. // 再将UICODE转成UTF8;
  299. BOOL bResult = UNICODE2UTF8(pWideChar, strResult);
  300. if (pWideChar)
  301. delete[] pWideChar;
  302. return bResult;
  303. }
  304. string EnCode_UTF8URL(IN const CHAR* pText)
  305. {
  306. string tt = "";
  307. string dd = "";
  308. ASCII2UTF8(pText, tt);
  309. size_t len = tt.length();
  310. for (size_t i = 0; i < len; i++)
  311. {
  312. if (isalnum((BYTE)tt.at(i)))
  313. {
  314. char tempbuff[2] = { 0 };
  315. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  316. dd.append(tempbuff);
  317. }
  318. else if (isspace((BYTE)tt.at(i)))
  319. {
  320. dd.append("+");
  321. }
  322. else
  323. {
  324. char tempbuff[4];
  325. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  326. dd.append(tempbuff);
  327. }
  328. }
  329. return dd;
  330. }
  331. void EnCode_UTF8URL(IN const CHAR* pText, OUT string& strResult)
  332. {
  333. string tt = "";
  334. ASCII2UTF8(pText, tt);
  335. size_t len = tt.length();
  336. for (size_t i = 0; i < len; i++)
  337. {
  338. if (isalnum((BYTE)tt.at(i)))
  339. {
  340. char tempbuff[2] = { 0 };
  341. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  342. strResult.append(tempbuff);
  343. }
  344. else if (isspace((BYTE)tt.at(i)))
  345. {
  346. strResult.append("+");
  347. }
  348. else
  349. {
  350. char tempbuff[4];
  351. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  352. strResult.append(tempbuff);
  353. }
  354. }
  355. }
  356. //做为解Url使用
  357. char CharToInt(char ch)
  358. {
  359. if (ch >= '0' && ch <= '9')return (char)(ch - '0');
  360. if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
  361. if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
  362. return -1;
  363. }
  364. char StrToBin(IN char(&str)[2])
  365. {
  366. char tempWord[2];
  367. char chn;
  368. tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
  369. tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
  370. chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
  371. return chn;
  372. }
  373. CHAR* UNICODE2ASCII(IN LPWCH lpUNICODEStr)
  374. {
  375. if (lpUNICODEStr == NULL)
  376. return NULL;
  377. // 获取多字节字符字节数;
  378. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  379. if (cbMultiByte == 0)
  380. return NULL;
  381. // 转换成多字节字符;
  382. CHAR *pMultiByteStr = new CHAR[cbMultiByte + 1];
  383. memset(pMultiByteStr, 0, cbMultiByte + 1);
  384. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  385. if (nWriteNum != cbMultiByte)
  386. {
  387. if (pMultiByteStr)
  388. delete[]pMultiByteStr;
  389. return NULL;
  390. }
  391. pMultiByteStr[cbMultiByte] = '\0';
  392. return pMultiByteStr;
  393. }
  394. BOOL UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
  395. {
  396. if (lpUNICODEStr == NULL)
  397. return FALSE;
  398. // 获取多字节字符字节数;
  399. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  400. if (cbMultiByte == 0 || cbMultiByte >= nASCIIStrLen)
  401. return FALSE;
  402. // 转换成多字节字符;
  403. memset((void*)pASCIIStr, 0, nASCIIStrLen);
  404. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pASCIIStr, cbMultiByte, NULL, NULL);
  405. if (nWriteNum != cbMultiByte)
  406. {
  407. return FALSE;
  408. }
  409. return TRUE;
  410. }
  411. /************************************************************************/
  412. /* 函数:[7/26/2016 IT];
  413. /* 描述:;
  414. /* 参数:;
  415. /* [IN] :;
  416. /* [OUT] :;
  417. /* [IN/OUT] :;
  418. /* 返回:void;
  419. /* 注意:;
  420. /* 示例:;
  421. /*
  422. /* 修改:;
  423. /* 日期:;
  424. /* 内容:;
  425. /************************************************************************/
  426. string DeCode_URLUNICODE(IN const CHAR* pURLText)
  427. {
  428. string str = pURLText;
  429. string strResult = "";
  430. INT nIndex = 0;
  431. string strTemp = "";
  432. while (str.find("\\u", 0, 2) != string::npos)
  433. {
  434. nIndex = str.find("\\u", 0, 2);
  435. strResult.append(str.substr(0, nIndex));
  436. strTemp = str.substr(nIndex + 2, 4);
  437. str = str.substr(nIndex + 2 + 4);
  438. CHAR szReturn[10] = { 0 };
  439. union __UNION_VAR_INT {
  440. BYTE ch[2];
  441. int value;
  442. }unionVarInt;
  443. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  444. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  445. WCHAR szWide[2] = { 0 };
  446. szWide[0] = unionVarInt.value;
  447. UNICODE2ASCII(szWide, szReturn, 10);
  448. strResult.append(szReturn);
  449. }
  450. strResult.append(str);
  451. return strResult;
  452. }
  453. // 通过注册表查找系统当前串口信息;
  454. BOOL IsValidString(LPCTSTR lpszString)
  455. {
  456. if (lpszString == NULL)
  457. return FALSE;
  458. do
  459. {
  460. // ASCII可显示的字符;
  461. if (*lpszString < 32 || *lpszString > 126)
  462. {
  463. return FALSE;
  464. }
  465. } while (*++lpszString);
  466. return TRUE;
  467. }
  468. } // namespace Global