BaseUtility.cpp 24 KB

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