BaseUtility.cpp 24 KB

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