BaseUtility.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. #include "pch.h"
  2. #include "BaseUtility.h"
  3. #include <odbcinst.h>
  4. #include <comdef.h>
  5. #include <afxdb.h>
  6. #include <regex>
  7. #include <iostream>
  8. #include <fstream>
  9. #include "Base64.h"
  10. #include <tuple>
  11. typedef basic_string<char>::size_type S_T;
  12. bool IsLeap(int year)
  13. {
  14. if((year%4==0&&year%100!=0)||year%400==0)
  15. return true;
  16. else
  17. return false;
  18. }
  19. bool IsWeekEnd(int year, int month, int day)
  20. {
  21. struct tm stTime = {0};
  22. struct tm *ptr = NULL;
  23. time_t time;
  24. stTime.tm_year = year - 1900;
  25. stTime.tm_mon = month - 1;
  26. stTime.tm_mday = day;
  27. time = mktime(&stTime);
  28. ptr = localtime(&time);
  29. return (ptr->tm_wday == 0 || ptr->tm_wday == 6);
  30. }
  31. bool Nextday(int& year, int& month, int& day)
  32. {
  33. short ld[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  34. if (!(!(year%100)&&(year%400)) && !(year%4))
  35. ld[1]++;
  36. day++;
  37. if (day<=0 || day>(unsigned long)ld[month-1])
  38. {
  39. day = 1; month++;
  40. if(month>12)
  41. {
  42. month = 1; year++;
  43. }
  44. }
  45. return true;
  46. }
  47. void Full2Half(wchar_t* str)
  48. {
  49. if(str != NULL)
  50. {
  51. int len = wcslen(str);
  52. int i = 0;
  53. wchar_t space[] = L" ";
  54. for(; i<len; i++)
  55. {
  56. if(str[i] == space[0])//对空格特殊处理
  57. {
  58. str[i] = ' ';
  59. }
  60. else if(str[i] >= 65281 && str[i] <= 65374)
  61. {
  62. str[i] -= (65281 - 33);
  63. }
  64. }
  65. }
  66. }
  67. bool split(const tstring& src, tstring delimit,vector<tstring> &v, tstring null_subst)
  68. {
  69. if(src.empty() || delimit.empty())
  70. return false;
  71. S_T deli_len = delimit.size();
  72. long index = -1,last_search_position = 0;
  73. while( (index=src.find(delimit,last_search_position))!=-1 )
  74. {
  75. if(index==last_search_position)
  76. v.push_back(null_subst);
  77. else
  78. v.push_back( src.substr(last_search_position, index-last_search_position) );
  79. last_search_position = index + deli_len;
  80. }
  81. tstring last_one = src.substr(last_search_position);
  82. v.push_back( last_one.empty()? null_subst:last_one );
  83. return true;
  84. }
  85. bool split(const string& src, string delimit, vector<string> &v, string null_subst)
  86. {
  87. if (src.empty() || delimit.empty())
  88. return false;
  89. S_T deli_len = delimit.size();
  90. long index = -1, last_search_position = 0;
  91. while ((index = src.find(delimit, last_search_position)) != -1)
  92. {
  93. if (index == last_search_position)
  94. v.push_back(null_subst);
  95. else
  96. v.push_back(src.substr(last_search_position, index - last_search_position));
  97. last_search_position = index + deli_len;
  98. }
  99. string last_one = src.substr(last_search_position);
  100. v.push_back(last_one.empty() ? null_subst : last_one);
  101. return true;
  102. }
  103. vector<tstring> splitEx(const tstring& src, tstring separate_character)
  104. {
  105. vector<tstring> strs;
  106. int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
  107. int lastPosition = 0, index = -1;
  108. while (-1 != (index = src.find(separate_character, lastPosition)))
  109. {
  110. strs.push_back(src.substr(lastPosition, index - lastPosition));
  111. lastPosition = index + separate_characterLen;
  112. }
  113. tstring lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
  114. if (!lastString.empty())
  115. strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
  116. return strs;
  117. }
  118. vector<string> splitEx(const string& src, string separate_character)
  119. {
  120. vector<string> strs;
  121. int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
  122. int lastPosition = 0, index = -1;
  123. while (-1 != (index = src.find(separate_character, lastPosition)))
  124. {
  125. strs.push_back(src.substr(lastPosition, index - lastPosition));
  126. lastPosition = index + separate_characterLen;
  127. }
  128. string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
  129. if (!lastString.empty())
  130. strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
  131. return strs;
  132. }
  133. int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len)
  134. {
  135. int i = 0, j = 0;
  136. if (0 == src_len)
  137. {
  138. src_len = strlen(src);
  139. }
  140. for (i = 0; i < src_len;)
  141. {
  142. if (j >= buf_len - 1)
  143. break;
  144. if (src[i] >= 0)
  145. {
  146. buf[j++] = src[i++];
  147. }
  148. else
  149. {
  150. unsigned short w_c = 0;
  151. char tmp[4] = "";
  152. Gb2312_2_Unicode(&w_c, src + i);
  153. Unicode_2_UTF8(tmp, &w_c);
  154. buf[j+0] = tmp[0];
  155. buf[j+1] = tmp[1];
  156. buf[j+2] = tmp[2];
  157. i += 2;
  158. j += 3;
  159. }
  160. }
  161. buf[j] = '\0';
  162. return j;
  163. }
  164. void Gb2312_2_Unicode(unsigned short* dst, const char* src)
  165. {
  166. MultiByteToWideChar(936, MB_PRECOMPOSED, src, 2, (LPWSTR)dst, 1);
  167. }
  168. void Unicode_2_UTF8(char* dst, unsigned short* src)
  169. {
  170. char *pchar = (char *)src;
  171. dst[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  172. dst[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  173. dst[2] = (0x80 | ( pchar[0] & 0x3F));
  174. }
  175. string UTF8ToTstring(const char *pData, size_t size)
  176. {
  177. size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0);
  178. WCHAR *pChar = new WCHAR[n+1];
  179. n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n);
  180. pChar[n]=0;
  181. n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
  182. char *p = new char[n+1];
  183. n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
  184. string result(p);
  185. delete []pChar;
  186. delete []p;
  187. return result;
  188. }
  189. string ConvertUTF8toGB2312(const char* pData, size_t size)
  190. {
  191. size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0);
  192. WCHAR* pChar = new WCHAR[n + 1];
  193. n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n);
  194. pChar[n] = 0;
  195. n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
  196. char* p = new char[n + 1];
  197. n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
  198. string result(p);
  199. delete[]pChar;
  200. delete[]p;
  201. return result;
  202. }
  203. string ConvertGB2312toUTF8(const char *pData)
  204. {
  205. int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0);
  206. wchar_t* wstr = new wchar_t[len+1];
  207. memset(wstr, 0, len+1);
  208. MultiByteToWideChar(936, 0, pData, -1, wstr, len);
  209. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  210. char* str = new char[len+1];
  211. memset(str, 0, len+1);
  212. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  213. if(wstr) delete[] wstr;
  214. string strUtf8(str);
  215. if(str) delete[] str;
  216. return strUtf8;
  217. }
  218. wstring GB2312ToUnicode(const char *pData)
  219. {
  220. wstring str;
  221. int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); // 先取得转换后的UNICODE字符串所需的长度
  222. if (len <= 0) return str;
  223. wchar_t* pWstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t)); // 分配缓冲区
  224. MultiByteToWideChar(936, 0, pData, -1, pWstr, len); // 开始转换
  225. str = pWstr;
  226. if (pWstr) free(pWstr);
  227. return str;
  228. }
  229. string UnicodeToGB2312(TCHAR *pData)
  230. {
  231. string str;
  232. int len = WideCharToMultiByte(936, 0, pData, -1, NULL, 0, NULL, NULL); // 先取得转换后的ANSI字符串所需的长度
  233. if (len <= 0) return str;
  234. char* pStr = (char*)calloc(len + 1, sizeof(char)); // 分配缓冲区
  235. WideCharToMultiByte(936, 0, pData, -1, pStr, len, NULL, NULL); // 开始转换
  236. str = pStr;
  237. if (pStr) free(pStr);
  238. return str;
  239. }
  240. void UTF8toANSI(std::string &strUTF8)
  241. {
  242. //获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区
  243. UINT nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, NULL, NULL);
  244. WCHAR *wszBuffer = new WCHAR[nLen + 1];
  245. nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, wszBuffer, nLen);
  246. wszBuffer[nLen] = 0;
  247. nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);
  248. char *szBuffer = new char[nLen + 1];
  249. nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);
  250. szBuffer[nLen] = 0;
  251. strUTF8 = szBuffer;
  252. //清理内存
  253. delete[]szBuffer;
  254. delete[]wszBuffer;
  255. }
  256. string UnicodeToGB2312(const tstring& strSrc)
  257. {
  258. return UnicodeToGB2312((LPTSTR)strSrc.c_str());
  259. }
  260. string UnicodeToGB2312(const CString& strSrc)
  261. {
  262. tstring wstr = strSrc;
  263. return UnicodeToGB2312((LPTSTR)wstr.c_str());
  264. }
  265. wstring UTF8ToUnicode(const char* szU8)
  266. {
  267. wstring str;
  268. if (szU8 == NULL)
  269. {
  270. return str;
  271. }
  272. //预转换,得到所需空间的大小;
  273. int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
  274. if (wcsLen <= 0) return str;
  275. //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
  276. wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t));
  277. //转换
  278. ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen);
  279. pWStr[wcsLen] = L'\0';
  280. str = pWStr;
  281. if (pWStr) free(pWStr);
  282. return str;
  283. }
  284. string UnicodeToUtf8(const wchar_t* unicode)
  285. {
  286. string str;
  287. int len;
  288. len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
  289. if (len <= 0) return str;
  290. char *szUtf8 = (char*)calloc(len + 1, sizeof(char));
  291. WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
  292. szUtf8[len] = '\0';
  293. str = szUtf8;
  294. if (szUtf8) free(szUtf8);
  295. return str;
  296. }
  297. tstring GB2312ToTstring(string str)
  298. {
  299. tstring tstr=_T("");
  300. #ifdef _UNICODE
  301. tstr=GB2312ToUnicode(str.c_str());
  302. #else
  303. tstr=str;
  304. #endif
  305. return tstr;
  306. }
  307. string TstringToGB2312(tstring tstr)
  308. {
  309. string str = "";
  310. #ifdef _UNICODE
  311. str = UnicodeToGB2312(tstr);
  312. #else
  313. str = pData;
  314. #endif
  315. return str;
  316. }
  317. string TstringToUTF8(tstring tstr)
  318. {
  319. string str = "";
  320. #ifdef _UNICODE
  321. str = UnicodeToUtf8(tstr.c_str());
  322. #else
  323. char* p=ConvertGB2312toUTF8(tstr.c_str());
  324. str=*p;
  325. delete[]p;
  326. #endif
  327. return str;
  328. }
  329. tstring UTF8ToTstring(string str)
  330. {
  331. tstring tstr = _T("");
  332. #ifdef _UNICODE
  333. tstr = UTF8ToUnicode(str.c_str());
  334. #else
  335. tstr = ConvertUTF8toGB2312(str.c_str(), str.length());
  336. #endif
  337. return tstr;
  338. }
  339. bool TransToPriceFormat(const tstring& strSrc, tstring& strDes, double& dDes)
  340. {
  341. vector<tstring> vctPrice;
  342. if (!split(strSrc, _T("|"), vctPrice))
  343. return false;
  344. strDes.clear();
  345. for (int i = 0; i < vctPrice.size(); i++)
  346. {
  347. if (vctPrice[i].length() == 0)
  348. continue;
  349. dDes += _tstof(vctPrice[i].c_str());
  350. strDes.append(vctPrice[i]);
  351. if (i != vctPrice.size()-1)
  352. {
  353. strDes.append(_T("+"));
  354. }
  355. }
  356. return true;
  357. }
  358. int CalcWord(const char *ps, const size_t length)
  359. {
  360. ASSERT(0 < length);
  361. ASSERT(NULL != ps);
  362. int nRealLen(0);
  363. int nFlag(0);
  364. while (nFlag <= length && 0 != ps[nRealLen])
  365. {
  366. if (IsChinese(ps[nRealLen]))
  367. {
  368. nRealLen++;
  369. if (0 != ps[nRealLen])
  370. nRealLen++;
  371. else
  372. break;
  373. }
  374. else
  375. nRealLen++;
  376. nFlag++;
  377. }
  378. return nRealLen;
  379. }
  380. void Int64DateToStr(const __int64& llDate, string& out)
  381. {
  382. int nYear = llDate / 10000000000L;
  383. int nMode = llDate % 10000000000L;
  384. int nMon = nMode / 100000000;
  385. nMode %= 100000000;
  386. int nDay = nMode / 1000000;
  387. nMode %= 1000000;
  388. int nHour = nMode / 10000;
  389. nMode %= 10000;
  390. int nMin = nMode / 100;
  391. int nSec = nMode % 100;
  392. char buf[20];
  393. memset(buf, 0, 20);
  394. sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", nYear,nMon,nDay,nHour,nMin,nSec);
  395. out = buf;
  396. }
  397. void Int64DateToYMDStr(const __int64& llDate, string& out)
  398. {
  399. int nYear = llDate / 10000000000L;
  400. int nMode = llDate % 10000000000L;
  401. int nMon = nMode / 100000000;
  402. nMode %= 100000000;
  403. int nDay = nMode / 1000000;
  404. nMode %= 1000000;
  405. char buf[20];
  406. memset(buf, 0, 20);
  407. sprintf(buf, "%04d-%02d-%02d", nYear,nMon,nDay);
  408. out = buf;
  409. }
  410. void Int64DateToHMStr(const __int64& llDate, string& out)
  411. {
  412. int nYear = llDate / 10000000000L;
  413. int nMode = llDate % 10000000000L;
  414. int nMon = nMode / 100000000;
  415. nMode %= 100000000;
  416. int nDay = nMode / 1000000;
  417. nMode %= 1000000;
  418. int nHour = nMode / 10000;
  419. nMode %= 10000;
  420. int nMin = nMode / 100;
  421. char buf[20];
  422. memset(buf, 0, 20);
  423. sprintf(buf, "%02d:%02d", nHour,nMin);
  424. out = buf;
  425. }
  426. unsigned char ToHex(unsigned char x)
  427. {
  428. return x > 9 ? x + 55 : x + 48;
  429. }
  430. unsigned char FromHex(unsigned char x)
  431. {
  432. unsigned char y;
  433. if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
  434. else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
  435. else if (x >= '0' && x <= '9') y = x - '0';
  436. return y;
  437. }
  438. string UrlEncode(const string& str)
  439. {
  440. string strTemp = "";
  441. size_t length = str.length();
  442. for (size_t i = 0; i < length; i++)
  443. {
  444. if (isalnum((unsigned char)str[i]) ||
  445. (str[i] == '-') ||
  446. (str[i] == '_') ||
  447. (str[i] == '.') ||
  448. (str[i] == '~'))
  449. strTemp += str[i];
  450. else if (str[i] == ' ')
  451. strTemp += "+";
  452. else
  453. {
  454. strTemp += '%';
  455. strTemp += ToHex((unsigned char)str[i] >> 4);
  456. strTemp += ToHex((unsigned char)str[i] % 16);
  457. }
  458. }
  459. return strTemp;
  460. }
  461. string UrlDecode(const string& str)
  462. {
  463. string strTemp = "";
  464. size_t length = str.length();
  465. for (size_t i = 0; i < length; i++)
  466. {
  467. if (str[i] == '+') strTemp += ' ';
  468. else if (str[i] == '%')
  469. {
  470. unsigned char high = FromHex((unsigned char)str[++i]);
  471. unsigned char low = FromHex((unsigned char)str[++i]);
  472. strTemp += high*16 + low;
  473. }
  474. else strTemp += str[i];
  475. }
  476. return strTemp;
  477. }
  478. void DeleteDirectory(const CString &strPath)
  479. {
  480. if (strPath.IsEmpty())
  481. return;
  482. CFileFind ff;
  483. BOOL bFound = ff.FindFile(strPath + _T("\\*"), 0);
  484. while(bFound)
  485. {
  486. bFound = ff.FindNextFile();
  487. if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T("..")))
  488. continue;
  489. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  490. if (ff.IsDirectory())
  491. {
  492. DeleteDirectory(ff.GetFilePath());
  493. //RemoveDirectory(ff.GetFilePath());
  494. }
  495. else
  496. {
  497. DeleteFile(ff.GetFilePath());
  498. }
  499. }
  500. ff.Close();
  501. //RemoveDirectory(strPath);
  502. }
  503. void FindDirFile(const CString &strPath, vector<std::tuple<CString, CString>>& vctFilePath)
  504. {
  505. if (strPath.IsEmpty())
  506. return;
  507. CFileFind ff;
  508. BOOL bFound = ff.FindFile(strPath + _T("\\*"), 0);
  509. while (bFound)
  510. {
  511. bFound = ff.FindNextFile();
  512. if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T("..")))
  513. continue;
  514. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  515. if (ff.IsDirectory())
  516. {
  517. FindDirFile(ff.GetFilePath(), vctFilePath);
  518. }
  519. else
  520. {
  521. vctFilePath.push_back({ ff.GetFilePath(), ff.GetFileName() });
  522. }
  523. }
  524. ff.Close();
  525. }
  526. void toUpper(char *szDestination, const char *szSource)
  527. {
  528. if (szDestination == NULL || szSource == NULL || strlen(szSource) == 0)
  529. return;
  530. while (*szSource != 0)
  531. {
  532. if (*szSource >= 'a' && *szSource <= 'z')
  533. *szDestination++ = 'A' + (*szSource++ - 'a');
  534. else
  535. *szDestination++ = *szSource++;
  536. }
  537. *szDestination = 0;
  538. }
  539. void toUpper(TCHAR *szDestination, const TCHAR *szSource)
  540. {
  541. if (szDestination == NULL || szSource == NULL || _tcslen(szSource) == 0)
  542. return;
  543. while (*szSource != 0)
  544. {
  545. if (*szSource >= 'a' && *szSource <= 'z')
  546. *szDestination++ = 'A' + (*szSource++ - 'a');
  547. else
  548. *szDestination++ = *szSource++;
  549. }
  550. *szDestination = 0;
  551. }
  552. void FormatPrice(char *szDes, const double dSource)
  553. {
  554. sprintf(szDes, "%.9lf", dSource);
  555. int i(0);
  556. int nLen(strlen(szDes));
  557. for (; i < nLen; i++)
  558. {
  559. if (szDes[i] == '.')
  560. break;
  561. }
  562. for (int j = nLen - 1; j > i + 2; j--)
  563. {
  564. if (szDes[j] == '0')
  565. szDes[j] = 0;
  566. else
  567. break;
  568. }
  569. }
  570. bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark)
  571. {
  572. vector<string> vctVol;
  573. vector<string> vctRemark;
  574. bool bRet(true);
  575. if (!split(szVol, "|", vctVol))
  576. bRet = false;
  577. if (!split(szRemark, "|", vctRemark))
  578. bRet = false;
  579. if (vctVol.size() != vctRemark.size())
  580. bRet = false;
  581. if (!bRet)
  582. {
  583. if (!split(szVol, "+", vctVol))
  584. bRet = false;
  585. else
  586. bRet = true;
  587. if (!split(szRemark, "+", vctRemark))
  588. bRet = false;
  589. else
  590. bRet = true;
  591. if (vctVol.size() != vctRemark.size())
  592. bRet = false;
  593. }
  594. if (!bRet)
  595. return bRet;
  596. for (int i = 0; i < vctVol.size(); i++)
  597. {
  598. if (i != 0)
  599. strDes.append("+");
  600. if (vctVol[i].empty())
  601. strDes.append("--");
  602. else
  603. strDes.append(vctVol[i]);
  604. if (!vctRemark[i].empty())
  605. {
  606. strDes.append("(");
  607. strDes.append(vctRemark[i]);
  608. strDes.append(")");
  609. }
  610. }
  611. return true;
  612. }
  613. string replace(const char *szContext, char oldChar, const char *szNew)
  614. {
  615. int nLen = strlen(szContext) + 1;
  616. if (szNew != NULL)
  617. nLen += strlen(szNew);
  618. char *pTemp = new char[nLen];
  619. autoptr_arr<char> arrguard(pTemp);
  620. memset(pTemp, 0, nLen);
  621. char *pResult = pTemp;
  622. while(*szContext != 0)
  623. {
  624. if (*szContext == oldChar)
  625. {
  626. if (szNew != NULL)
  627. {
  628. while (*szNew!= 0)
  629. {
  630. *pTemp++ = *szNew++;
  631. }
  632. }
  633. }
  634. else
  635. {
  636. *pTemp++ = *szContext;
  637. }
  638. szContext++;
  639. }
  640. return string(pResult);
  641. }
  642. bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark/* = NULL*/
  643. , const char *szDanger/* = NULL*/, const char *szOCO/* = NULL*/, const char *szPACK/* = NULL*/)
  644. {
  645. if (szVol == NULL)
  646. return false;
  647. vector<const char*> vctData;
  648. vctData.push_back(szVol);
  649. if (szDanger != NULL)
  650. {
  651. vctData.push_back(szDanger);
  652. }
  653. if (szOCO != NULL)
  654. {
  655. vctData.push_back(szOCO);
  656. }
  657. if (szPACK != NULL)
  658. {
  659. vctData.push_back(szPACK);
  660. }
  661. if (szRemark != NULL && strlen(szRemark) > 0 && strcmp(szRemark, "--") != 0)
  662. {
  663. vctData.push_back(szRemark);
  664. }
  665. const char* arrSeparators[2] = {"|", "+"};
  666. bool bMulti(false);
  667. int nIdx(0);
  668. for (int i = 0; i < 2; i++)
  669. {
  670. if (strstr(szVol, arrSeparators[i]) != NULL)
  671. {
  672. nIdx = i;
  673. bMulti = true;
  674. break;
  675. }
  676. }
  677. if (bMulti)
  678. {
  679. vector<vector<string>> vctSplit;
  680. for (int i = 0; i < vctData.size(); i++)
  681. {
  682. vector<string> vct;
  683. if (!split(vctData[i], arrSeparators[nIdx], vct))
  684. {
  685. return false;
  686. }
  687. vctSplit.push_back(vct);
  688. }
  689. int nCount(vctSplit[0].size());
  690. for (int i = 1; i < vctSplit.size(); i++)
  691. {
  692. if (nCount != vctSplit[i].size())
  693. return false;
  694. }
  695. for (int i = 0; i < nCount; i++)
  696. {
  697. if (i != 0)
  698. strDes.append("+");
  699. if (strlen((vctSplit[0])[i].c_str()) == 0)
  700. strDes.append("--");
  701. else
  702. {
  703. strDes.append((vctSplit[0])[i]);
  704. }
  705. string strTemp;
  706. for (int j = 1; j < vctSplit.size(); j++)
  707. {
  708. if (strlen((vctSplit[j])[i].c_str()) != 0)
  709. {
  710. strTemp.append((vctSplit[j])[i]);
  711. strTemp.append(",");
  712. }
  713. }
  714. if (!strTemp.empty())
  715. {
  716. if (strTemp[strTemp.size() - 1] == ',')
  717. {
  718. strTemp = string(strTemp.c_str(), strTemp.size() - 1);
  719. }
  720. }
  721. if (!strTemp.empty())
  722. {
  723. strDes.append("(");
  724. strDes.append(strTemp);
  725. strDes.append(")");
  726. }
  727. }
  728. }
  729. else
  730. {
  731. if (strlen(szVol) == 0)
  732. {
  733. strDes.append("--");
  734. }
  735. else
  736. {
  737. strDes.append(szVol);
  738. }
  739. string strTemp;
  740. for (int i = 1; i < vctData.size(); i++)
  741. {
  742. if (strlen(vctData[i]) != 0)
  743. {
  744. strTemp.append(vctData[i]);
  745. strTemp.append(",");
  746. }
  747. }
  748. if (!strTemp.empty())
  749. {
  750. if (strTemp[strTemp.size() - 1] == ',')
  751. {
  752. strTemp = string(strTemp.c_str(), strTemp.size() - 1);
  753. }
  754. }
  755. if (!strTemp.empty())
  756. {
  757. strDes.append("(");
  758. strDes.append(strTemp);
  759. strDes.append(")");
  760. }
  761. }
  762. return true;
  763. }
  764. __int64 StringToInt64(const TCHAR *szSource)
  765. {
  766. __int64 ret(0);
  767. if (_tcslen(szSource) == 0)
  768. return ret;
  769. const TCHAR *pTemp = szSource + _tcslen(szSource) - 1;
  770. __int64 nRight(1);
  771. bool bNegative(false);
  772. if ('-' == szSource[0])
  773. bNegative = true;
  774. do
  775. {
  776. if (*pTemp >= '0' && *pTemp <= '9')
  777. {
  778. ret += nRight * (0 + (*pTemp - '0'));
  779. nRight *= 10;
  780. }
  781. }
  782. while(pTemp-- != szSource);
  783. if (bNegative)
  784. ret = -ret;
  785. return ret;
  786. }
  787. void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src)
  788. {
  789. int srcSize = _tcslen(src);
  790. if (srcSize == 0)
  791. {
  792. return;
  793. }
  794. if (srcSize > destSize)
  795. memcpy(dest, src, destSize);
  796. else
  797. _tcscpy(dest, src);
  798. }
  799. void SafeCopyData(TCHAR dest[], int destSize, tstring str)
  800. {
  801. int srcSize = str.length();
  802. if (srcSize == 0)
  803. {
  804. return;
  805. }
  806. if (srcSize > destSize)
  807. memcpy(dest, str.c_str(), destSize);
  808. else
  809. _tcscpy(dest, str.c_str());
  810. }
  811. tstring GetRequestByKey(const tstring& tstrKey, const tstring& tstrReuest)
  812. {
  813. string strKey = UnicodeToGB2312(tstrKey);
  814. string strReuest = UnicodeToGB2312(tstrReuest);
  815. try
  816. {
  817. smatch result;
  818. if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*?)&")))
  819. {
  820. return GB2312ToUnicode(result[1].str().c_str());
  821. }
  822. else if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*)")))
  823. {
  824. return GB2312ToUnicode(result[1].str().c_str());
  825. }
  826. else
  827. {
  828. return tstring();
  829. }
  830. }
  831. catch (...)
  832. {
  833. return tstring();
  834. }
  835. return tstring();
  836. }
  837. void GetKeysByUrl(const tstring & strUrl, vector<tstring> & vctKey)
  838. {
  839. vector<tstring> vctSearch;
  840. split(strUrl, _T("?"), vctSearch);
  841. if (vctSearch.size() != 2 && vctSearch.size() != 1)
  842. {
  843. return;
  844. }
  845. tstring strTemp;
  846. if (vctSearch.size() == 1)
  847. {
  848. strTemp = vctSearch[0];
  849. }
  850. else if (vctSearch.size() == 2)
  851. {
  852. strTemp = vctSearch[1];
  853. }
  854. vctSearch.clear();
  855. split(strTemp, _T("&"), vctSearch);
  856. if (vctSearch.size() <= 0)
  857. {
  858. return;
  859. }
  860. for (int i = 0; i < vctSearch.size(); i++)
  861. {
  862. strTemp = vctSearch[i];
  863. vector<tstring> vct;
  864. split(strTemp, _T("="), vct);
  865. if (vct.size() == 2)
  866. {
  867. vctKey.push_back(vct[0]);
  868. }
  869. }
  870. }
  871. __int64 GetCharNumber(TCHAR* szText, size_t n)
  872. {
  873. __int64 nDest;
  874. if (n == 0)
  875. {
  876. return -1;
  877. }
  878. for (nDest = 0; n--; szText++)
  879. {
  880. if (*szText < '0' || *szText > '9')
  881. {
  882. continue; //继续获取下一个字符
  883. }
  884. /*
  885. 1、当前字符值 *line的ascii码,减去字符0的ascii码,得出个位数字
  886. 2、原计算的value值 乘以10,向上提升一位
  887. 3、二者相加得到新的十进制数值
  888. */
  889. nDest = nDest * 10 + (*szText - '0');
  890. }
  891. if (nDest < 0)
  892. {
  893. return -1;
  894. }
  895. else
  896. {
  897. return nDest;
  898. }
  899. }
  900. tstring ANSIToUnicode(const string& str)
  901. {
  902. int len = 0;
  903. len = str.length();
  904. int unicodeLen = ::MultiByteToWideChar(936,
  905. 0,
  906. str.c_str(),
  907. -1,
  908. NULL,
  909. 0);
  910. wchar_t * pUnicode;
  911. pUnicode = new wchar_t[unicodeLen + 1];
  912. memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));
  913. ::MultiByteToWideChar(936,
  914. 0,
  915. str.c_str(),
  916. -1,
  917. (LPWSTR)pUnicode,
  918. unicodeLen);
  919. tstring rt;
  920. rt = (wchar_t*)pUnicode;
  921. delete pUnicode;
  922. return rt;
  923. }
  924. CString AddNumberSeparate(CString strSrc)
  925. {
  926. auto IsNumberFunc = [=](CString str)
  927. {
  928. int nCount = 0;
  929. for (int i = 0; i < str.GetLength(); i++)
  930. {
  931. if (i == 0)
  932. {
  933. if (str.GetAt(i) == '-')
  934. {
  935. continue;
  936. }
  937. }
  938. if (!((str.GetAt(i) >= '0' && str.GetAt(i) <= '9') || str.GetAt(i) == '.'))
  939. {
  940. return FALSE;
  941. }
  942. if (str.GetAt(i) == '.')
  943. {
  944. nCount++;
  945. }
  946. }
  947. if (nCount > 1)
  948. {
  949. return FALSE;
  950. }
  951. return TRUE;
  952. };
  953. CString str(strSrc);
  954. if (IsNumberFunc(str))
  955. {
  956. int nPos = 0;
  957. nPos = str.Find(_T("."), nPos);
  958. vector<int> vcteparatePos;
  959. if (nPos == -1)
  960. {
  961. for (int i = str.GetLength() - 3; i > 0; i--)
  962. {
  963. if ((str.GetLength() - i) % 3 == 0)
  964. {
  965. if (!(i == 1 && str.GetAt(0) == '-'))
  966. {
  967. vcteparatePos.push_back(i);
  968. }
  969. }
  970. }
  971. }
  972. else
  973. {
  974. for (int i = nPos - 3; i > 0; i--)
  975. {
  976. if ((nPos - i) % 3 == 0)
  977. {
  978. if (!(i == 1 && str.GetAt(0) == '-'))
  979. {
  980. vcteparatePos.push_back(i);
  981. }
  982. }
  983. }
  984. }
  985. for (int i = 0; i < vcteparatePos.size(); i++)
  986. {
  987. str.Insert(vcteparatePos[i], ',');
  988. }
  989. }
  990. return str;
  991. }
  992. bool RegexStdMatch(string src, string regular)
  993. {
  994. regex pattern(regular.c_str());
  995. if (!regex_match(src, pattern))
  996. {
  997. return false;
  998. }
  999. return true;
  1000. }
  1001. bool RegexStdMatch(tstring src, tstring regular)
  1002. {
  1003. char* pre = setlocale(LC_ALL, "");
  1004. setlocale(LC_ALL, "chs");
  1005. std::wregex pattern(regular.c_str());
  1006. if (!regex_match(src, pattern))
  1007. {
  1008. return false;
  1009. }
  1010. return true;
  1011. }
  1012. CString FormatNumberString(CString strData)
  1013. {
  1014. TCHAR szTemp[32] = { 0 };
  1015. _stprintf(szTemp, _T("%0.4lf"), _tstof(strData));
  1016. CString str = szTemp;
  1017. str.TrimRight(_T("0"));
  1018. if (str.Find(_T(".")) == (str.GetLength() - 1))
  1019. str.TrimRight(_T("."));
  1020. return str;
  1021. }
  1022. tstring replace_all(tstring& str, const tstring& old_value, const tstring& new_value)
  1023. {
  1024. while (true)
  1025. {
  1026. tstring::size_type pos(0);
  1027. if ((pos = str.find(old_value)) != tstring::npos)
  1028. str.replace(pos, old_value.length(), new_value);
  1029. else break;
  1030. }
  1031. return str;
  1032. }
  1033. tstring FormatInt32Value(int nValue)
  1034. {
  1035. TCHAR szTemp[64] = { 0 };
  1036. _stprintf(szTemp, _T("%d"), nValue);
  1037. return tstring(szTemp);
  1038. }
  1039. string FormatInt32Value2(int nValue)
  1040. {
  1041. char szTemp[64] = { 0 };
  1042. sprintf(szTemp, "%d", nValue);
  1043. return string(szTemp);
  1044. }
  1045. tstring FormatInt64Value(__int64 nValue)
  1046. {
  1047. TCHAR szTemp[64] = { 0 };
  1048. _stprintf(szTemp, _T("%lld"), nValue);
  1049. return tstring(szTemp);
  1050. }
  1051. tstring FormatFloatValue(double fValue)
  1052. {
  1053. TCHAR szTemp[32] = { 0 };
  1054. _stprintf(szTemp, _T("%0.3lf"), fValue);
  1055. CString str = szTemp;
  1056. str.TrimRight(_T("0"));
  1057. if (str.Find(_T(".")) == (str.GetLength() - 1))
  1058. str.TrimRight(_T("."));
  1059. return tstring(str);
  1060. }
  1061. string FormatFloatValue2(double fValue)
  1062. {
  1063. char szTemp[64] = { 0 };
  1064. sprintf(szTemp, "%0.3lf", fValue);
  1065. return string(szTemp);
  1066. }
  1067. CString GetAppPath()
  1068. {
  1069. CString strRetun = _T("");
  1070. #ifdef _UNICODE
  1071. TCHAR szBuff[MAX_PATH];
  1072. HMODULE module = GetModuleHandle(0);
  1073. GetModuleFileName(module, szBuff, sizeof(szBuff));
  1074. strRetun.Format(_T("%s"), szBuff);
  1075. #else
  1076. HMODULE module = GetModuleHandle(0);
  1077. CHAR szBuff[MAX_PATH];
  1078. GetModuleFileName(module, szBuff, sizeof(szBuff));
  1079. strRetun.Format(_T("%s"), szBuff);
  1080. #endif
  1081. int pos = strRetun.ReverseFind(_T('\\'));
  1082. if (pos != -1)
  1083. {
  1084. strRetun = strRetun.Left(pos);
  1085. }
  1086. return strRetun;
  1087. }
  1088. string GetImageEncodeString(string strPath)
  1089. {
  1090. ifstream is(strPath.c_str(), ifstream::in | ios::binary);
  1091. is.seekg(0, is.end);
  1092. int length = is.tellg();
  1093. is.seekg(0, is.beg);
  1094. char *buffer = new char[length];
  1095. is.read(buffer, length);
  1096. //主要注意这一句是利用base64.h文件中的编码函数将buffer编码为base64码
  1097. CBase64Coder base64;
  1098. std::string strImg = base64.encode(buffer, length);
  1099. string strTemp = "data:image/jpeg;base64,";
  1100. strTemp.append(strImg);
  1101. //打印img看一下
  1102. //string strEncode = UrlEncode(strTemp);
  1103. //删除buffer
  1104. delete[]buffer;
  1105. //关闭指针
  1106. is.close();
  1107. return strTemp;
  1108. }
  1109. string GenerateGUID()
  1110. {
  1111. string guid_code;
  1112. char buf[64] = { 0 };
  1113. GUID guid;
  1114. if (S_OK == ::CoCreateGuid(&guid))
  1115. {
  1116. _snprintf(buf, sizeof(buf)
  1117. , "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x"
  1118. , guid.Data1
  1119. , guid.Data2
  1120. , guid.Data3
  1121. , guid.Data4[0], guid.Data4[1]
  1122. , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
  1123. , guid.Data4[6], guid.Data4[7]
  1124. );
  1125. guid_code = buf;
  1126. }
  1127. else
  1128. {
  1129. CTime tmNow = CTime::GetCurrentTime();
  1130. std::wstring strNow = tmNow.Format("%Y%m%d%H%M%S");
  1131. /*guid_code = strW2A(strNow);*/
  1132. }
  1133. return guid_code;
  1134. }