BaseUtility.cpp 24 KB

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