Util.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // Util.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Util.h"
  5. #include <odbcinst.h>
  6. #include <comdef.h>
  7. #include <afxdb.h>
  8. #include <regex>
  9. typedef basic_string<char>::size_type S_T;
  10. wstring strA2W(const string &str)
  11. {
  12. int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  13. wchar_t *wstr = new wchar_t[len + 2];
  14. memset(wstr, 0, (2 + len)*sizeof(wchar_t));
  15. MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, len);
  16. wstring cstrDestW = wstr;
  17. delete[] wstr;
  18. return cstrDestW;
  19. }
  20. string strW2A(const wstring &wstr)
  21. {
  22. int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  23. char *str = new char[len + 2];
  24. memset(str, 0, len + 2);
  25. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, len, NULL, NULL);
  26. string cstrDestA = str;
  27. delete[] str;
  28. return cstrDestA;
  29. }
  30. string strWtoUTF8(const wstring &src)
  31. {
  32. string result="";
  33. int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 );
  34. result.resize(n);
  35. ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 );
  36. return result;
  37. }
  38. wstring strUTF8toW(const string& src)
  39. {
  40. wstring result=L"";
  41. int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 ) + 1 ;
  42. result.resize(n + 1);
  43. ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
  44. result[n] = L'\0';
  45. return result;
  46. }
  47. bool IsLeap(int year)
  48. {
  49. if((year%4==0&&year%100!=0)||year%400==0)
  50. return true;
  51. else
  52. return false;
  53. }
  54. bool IsWeekEnd(int year, int month, int day)
  55. {
  56. struct tm stTime = {0};
  57. struct tm *ptr = NULL;
  58. time_t time;
  59. stTime.tm_year = year - 1900;
  60. stTime.tm_mon = month - 1;
  61. stTime.tm_mday = day;
  62. time = mktime(&stTime);
  63. ptr = localtime(&time);
  64. return (ptr->tm_wday == 0 || ptr->tm_wday == 6);
  65. }
  66. char* PBQ_TrimLeftA(char *pTarget)
  67. {
  68. char buf[] = " \t";
  69. return PBQ_TrimLeftMA(pTarget, buf);
  70. }
  71. char* PBQ_TrimRightA(char *pTarget)
  72. {
  73. char buf[] = " \t";
  74. return PBQ_TrimRightMA(pTarget, buf);
  75. }
  76. char* PBQ_TrimLeftMA(char *pTarget, char* pChars)
  77. {
  78. char *ptr = pTarget;
  79. if(pTarget != NULL && pChars != NULL)
  80. {
  81. for (; ptr != NULL && *ptr != '\0'; ptr++)
  82. {
  83. if(NULL == strchr(pChars, *ptr))
  84. {
  85. break;
  86. }
  87. }
  88. strcpy(pTarget, ptr);
  89. }
  90. return pTarget;
  91. }
  92. char* PBQ_TrimRightMA(char *pTarget, char* pChars)
  93. {
  94. char *pEnd = NULL;
  95. if (pTarget != NULL)
  96. {
  97. pEnd = pTarget + strlen(pTarget) - 1;
  98. for (; pEnd >= pTarget; pEnd--)
  99. {
  100. if(NULL != strchr(pChars, *pEnd))
  101. {
  102. *pEnd = 0;
  103. }
  104. else
  105. {
  106. break;
  107. }
  108. }
  109. }
  110. return pTarget;
  111. }
  112. void Full2Half(wchar_t* str)
  113. {
  114. if(str != NULL)
  115. {
  116. int len = wcslen(str);
  117. int i = 0;
  118. wchar_t space[] = L" ";
  119. for(; i<len; i++)
  120. {
  121. if(str[i] == space[0])//对空格特殊处理
  122. {
  123. str[i] = ' ';
  124. }
  125. else if(str[i] >= 65281 && str[i] <= 65374)
  126. {
  127. str[i] -= (65281 - 33);
  128. }
  129. }
  130. }
  131. }
  132. bool split(const string& src, string delimit,vector<string> &v, string null_subst)
  133. {
  134. if(src.empty() || delimit.empty())
  135. return false;
  136. S_T deli_len = delimit.size();
  137. long index = -1,last_search_position = 0;
  138. while( (index=src.find(delimit,last_search_position))!=-1 )
  139. {
  140. if(index==last_search_position)
  141. v.push_back(null_subst);
  142. else
  143. v.push_back( src.substr(last_search_position, index-last_search_position) );
  144. last_search_position = index + deli_len;
  145. }
  146. string last_one = src.substr(last_search_position);
  147. v.push_back( last_one.empty()? null_subst:last_one );
  148. return true;
  149. }
  150. bool split(const wstring& src, wstring delimit, vector<wstring> &v, wstring null_subst)
  151. {
  152. if (src.empty() || delimit.empty())
  153. return false;
  154. S_T deli_len = delimit.size();
  155. long index = -1, last_search_position = 0;
  156. while ((index = src.find(delimit, last_search_position)) != -1)
  157. {
  158. if (index == last_search_position)
  159. v.push_back(null_subst);
  160. else
  161. v.push_back(src.substr(last_search_position, index - last_search_position));
  162. last_search_position = index + deli_len;
  163. }
  164. wstring last_one = src.substr(last_search_position);
  165. v.push_back(last_one.empty() ? null_subst : last_one);
  166. return true;
  167. }
  168. vector<string> splitEx(const string& src, string separate_character)
  169. {
  170. vector<string> strs;
  171. int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
  172. int lastPosition = 0, index = -1;
  173. while (-1 != (index = src.find(separate_character, lastPosition)))
  174. {
  175. strs.push_back(src.substr(lastPosition, index - lastPosition));
  176. lastPosition = index + separate_characterLen;
  177. }
  178. string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
  179. if (!lastString.empty())
  180. strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
  181. return strs;
  182. }
  183. int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len)
  184. {
  185. int i = 0, j = 0;
  186. if (0 == src_len)
  187. {
  188. src_len = strlen(src);
  189. }
  190. for (i = 0; i < src_len;)
  191. {
  192. if (j >= buf_len - 1)
  193. break;
  194. if (src[i] >= 0)
  195. {
  196. buf[j++] = src[i++];
  197. }
  198. else
  199. {
  200. unsigned short w_c = 0;
  201. char tmp[4] = "";
  202. Gb2312_2_Unicode(&w_c, src + i);
  203. Unicode_2_UTF8(tmp, &w_c);
  204. buf[j+0] = tmp[0];
  205. buf[j+1] = tmp[1];
  206. buf[j+2] = tmp[2];
  207. i += 2;
  208. j += 3;
  209. }
  210. }
  211. buf[j] = '\0';
  212. return j;
  213. }
  214. void Gb2312_2_Unicode(unsigned short* dst, const char* src)
  215. {
  216. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, src, 2, (LPWSTR)dst, 1);
  217. }
  218. void Unicode_2_UTF8(char* dst, unsigned short* src)
  219. {
  220. char *pchar = (char *)src;
  221. dst[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
  222. dst[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
  223. dst[2] = (0x80 | ( pchar[0] & 0x3F));
  224. }
  225. string ConvertUTF8toGB2312(const char *pData, size_t size)
  226. {
  227. size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0);
  228. WCHAR *pChar = new WCHAR[n+1];
  229. n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n);
  230. pChar[n]=0;
  231. n = WideCharToMultiByte(CP_ACP, 0, pChar, -1, 0, 0, 0, 0);
  232. char *p = new char[n+1];
  233. n = WideCharToMultiByte(CP_ACP, 0, pChar, -1, p, (int)n, 0, 0);
  234. string result(p);
  235. delete []pChar;
  236. delete []p;
  237. return result;
  238. }
  239. char* ConvertGB2312toUTF8(const char *pData)
  240. {
  241. int len = MultiByteToWideChar(CP_ACP, 0, pData, -1, NULL, 0);
  242. wchar_t* wstr = new wchar_t[len+1];
  243. memset(wstr, 0, len+1);
  244. MultiByteToWideChar(CP_ACP, 0, pData, -1, wstr, len);
  245. len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  246. char* str = new char[len+1];
  247. memset(str, 0, len+1);
  248. WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
  249. if(wstr) delete[] wstr;
  250. return str;
  251. }
  252. wstring UTF8ToUnicode(const char* szU8)
  253. {
  254. wstring str;
  255. if (szU8 == NULL)
  256. {
  257. return str;
  258. }
  259. //预转换,得到所需空间的大小;
  260. int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
  261. if (wcsLen <= 0) return str;
  262. //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
  263. wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t));
  264. //转换
  265. ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen);
  266. pWStr[wcsLen] = L'\0';
  267. str = pWStr;
  268. if (pWStr) free(pWStr);
  269. return str;
  270. }
  271. int CalcWord(const char *ps, const size_t length)
  272. {
  273. ASSERT(0 < length);
  274. ASSERT(NULL != ps);
  275. int nRealLen(0);
  276. int nFlag(0);
  277. while (nFlag <= length && 0 != ps[nRealLen])
  278. {
  279. if (IsChinese(ps[nRealLen]))
  280. {
  281. nRealLen++;
  282. if (0 != ps[nRealLen])
  283. nRealLen++;
  284. else
  285. break;
  286. }
  287. else
  288. nRealLen++;
  289. nFlag++;
  290. }
  291. return nRealLen;
  292. }
  293. void Int64DateToStr(const __int64& llDate, string& out)
  294. {
  295. int nYear = llDate / 10000000000L;
  296. int nMode = llDate % 10000000000L;
  297. int nMon = nMode / 100000000;
  298. nMode %= 100000000;
  299. int nDay = nMode / 1000000;
  300. nMode %= 1000000;
  301. int nHour = nMode / 10000;
  302. nMode %= 10000;
  303. int nMin = nMode / 100;
  304. int nSec = nMode % 100;
  305. char buf[20];
  306. memset(buf, 0, 20);
  307. sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", nYear,nMon,nDay,nHour,nMin,nSec);
  308. out = buf;
  309. }
  310. void Int64DateToYMDStr(const __int64& llDate, string& out)
  311. {
  312. int nYear = llDate / 10000000000L;
  313. int nMode = llDate % 10000000000L;
  314. int nMon = nMode / 100000000;
  315. nMode %= 100000000;
  316. int nDay = nMode / 1000000;
  317. nMode %= 1000000;
  318. char buf[20];
  319. memset(buf, 0, 20);
  320. sprintf(buf, "%04d-%02d-%02d", nYear,nMon,nDay);
  321. out = buf;
  322. }
  323. void Int64DateToHMStr(const __int64& llDate, string& out)
  324. {
  325. int nYear = llDate / 10000000000L;
  326. int nMode = llDate % 10000000000L;
  327. int nMon = nMode / 100000000;
  328. nMode %= 100000000;
  329. int nDay = nMode / 1000000;
  330. nMode %= 1000000;
  331. int nHour = nMode / 10000;
  332. nMode %= 10000;
  333. int nMin = nMode / 100;
  334. char buf[20];
  335. memset(buf, 0, 20);
  336. sprintf(buf, "%02d:%02d", nHour,nMin);
  337. out = buf;
  338. }
  339. unsigned char ToHex(unsigned char x)
  340. {
  341. return x > 9 ? x + 55 : x + 48;
  342. }
  343. unsigned char FromHex(unsigned char x)
  344. {
  345. unsigned char y;
  346. if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
  347. else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
  348. else if (x >= '0' && x <= '9') y = x - '0';
  349. return y;
  350. }
  351. std::string UrlEncode(const std::string& str)
  352. {
  353. std::string strTemp = "";
  354. size_t length = str.length();
  355. for (size_t i = 0; i < length; i++)
  356. {
  357. if (isalnum((unsigned char)str[i]) ||
  358. (str[i] == '-') ||
  359. (str[i] == '_') ||
  360. (str[i] == '.') ||
  361. (str[i] == '~'))
  362. strTemp += str[i];
  363. else if (str[i] == ' ')
  364. strTemp += "+";
  365. else
  366. {
  367. strTemp += '%';
  368. strTemp += ToHex((unsigned char)str[i] >> 4);
  369. strTemp += ToHex((unsigned char)str[i] % 16);
  370. }
  371. }
  372. return strTemp;
  373. }
  374. std::string UrlDecode(const std::string& str)
  375. {
  376. std::string strTemp = "";
  377. size_t length = str.length();
  378. for (size_t i = 0; i < length; i++)
  379. {
  380. if (str[i] == '+') strTemp += ' ';
  381. else if (str[i] == '%')
  382. {
  383. unsigned char high = FromHex((unsigned char)str[++i]);
  384. unsigned char low = FromHex((unsigned char)str[++i]);
  385. strTemp += high*16 + low;
  386. }
  387. else strTemp += str[i];
  388. }
  389. return strTemp;
  390. }
  391. void DeleteDirectory(CString source)
  392. {
  393. CFileFind finder;
  394. CString path;
  395. if(PathFileExists(source))
  396. {
  397. path.Format(_T("%s/*.*"),source);
  398. bool bWorking = finder.FindFile(path);
  399. while(bWorking){
  400. bWorking = finder.FindNextFile();
  401. if(finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 ..
  402. DeleteDirectory(finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName()
  403. }
  404. else{ //是文件 则直接删除
  405. DeleteFile(finder.GetFilePath());
  406. }
  407. }
  408. RemoveDirectory(source);
  409. }
  410. }
  411. void RecursiveDirectory(CString cstrDir) // 递归创建目录
  412. {
  413. if (cstrDir.GetLength() <= 3)//是根目录,无需创建目录
  414. {
  415. return;
  416. }
  417. if (cstrDir[cstrDir.GetLength()-1] == '\\') // 将路径改为目录
  418. {
  419. cstrDir.Delete(cstrDir.GetLength()-1, 1);
  420. }
  421. // 修改文件属性
  422. WIN32_FIND_DATA wfd;
  423. HANDLE hFind = FindFirstFile(cstrDir, &wfd); // 查找
  424. if (hFind != INVALID_HANDLE_VALUE)
  425. {
  426. FindClose(hFind);
  427. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  428. return;
  429. }
  430. // 创建当前目录的地目录失败
  431. if (CreateDirectory(cstrDir,NULL) == false)
  432. {// 退到上一级目录
  433. CString wstrNewDir = cstrDir;
  434. int n = wstrNewDir.ReverseFind('\\');
  435. wstrNewDir = cstrDir.Left(n);
  436. // 递归进入
  437. RecursiveDirectory(wstrNewDir); // 递归本函数,再创建目录
  438. // 递归退出后创建之前失败的目录
  439. CreateDirectory(cstrDir,NULL); // 递归返回,在存在的目录上再建目录
  440. }// 多级目录创建成功
  441. }
  442. void toUpper(char *szDestination, const char *szSource)
  443. {
  444. if (szDestination == NULL || szSource == NULL || strlen(szSource) == 0)
  445. return;
  446. while (*szSource != 0)
  447. {
  448. if (*szSource >= 'a' && *szSource <= 'z')
  449. *szDestination++ = 'A' + (*szSource++ - 'a');
  450. else
  451. *szDestination++ = *szSource++;
  452. }
  453. *szDestination = 0;
  454. }
  455. void FormatPrice(char *szDes, const double dSource)
  456. {
  457. sprintf(szDes, "%.9lf", dSource);
  458. int i(0);
  459. int nLen(strlen(szDes));
  460. for (; i < nLen; i++)
  461. {
  462. if (szDes[i] == '.')
  463. break;
  464. }
  465. for (int j = nLen - 1; j > i + 2; j--)
  466. {
  467. if (szDes[j] == '0')
  468. szDes[j] = 0;
  469. else
  470. break;
  471. }
  472. }
  473. bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark)
  474. {
  475. vector<string> vctVol;
  476. vector<string> vctRemark;
  477. bool bRet(true);
  478. if (!split(szVol, "|", vctVol))
  479. bRet = false;
  480. if (!split(szRemark, "|", vctRemark))
  481. bRet = false;
  482. if (vctVol.size() != vctRemark.size())
  483. bRet = false;
  484. if (!bRet)
  485. {
  486. if (!split(szVol, "+", vctVol))
  487. bRet = false;
  488. else
  489. bRet = true;
  490. if (!split(szRemark, "+", vctRemark))
  491. bRet = false;
  492. else
  493. bRet = true;
  494. if (vctVol.size() != vctRemark.size())
  495. bRet = false;
  496. }
  497. if (!bRet)
  498. return bRet;
  499. for (int i = 0; i < vctVol.size(); i++)
  500. {
  501. if (i != 0)
  502. strDes.append("+");
  503. if (vctVol[i].empty())
  504. strDes.append("--");
  505. else
  506. strDes.append(vctVol[i]);
  507. if (!vctRemark[i].empty())
  508. {
  509. strDes.append("(");
  510. strDes.append(vctRemark[i]);
  511. strDes.append(")");
  512. }
  513. }
  514. return true;
  515. }
  516. bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark/* = NULL*/
  517. , const char *szDanger/* = NULL*/, const char *szOCO/* = NULL*/, const char *szPACK/* = NULL*/)
  518. {
  519. if (szVol == NULL)
  520. return false;
  521. vector<const char*> vctData;
  522. vctData.push_back(szVol);
  523. if (szDanger != NULL)
  524. {
  525. vctData.push_back(szDanger);
  526. }
  527. if (szOCO != NULL)
  528. {
  529. vctData.push_back(szOCO);
  530. }
  531. if (szPACK != NULL)
  532. {
  533. vctData.push_back(szPACK);
  534. }
  535. if (szRemark != NULL && strlen(szRemark) > 0 && strcmp(szRemark, "--") != 0)
  536. {
  537. vctData.push_back(szRemark);
  538. }
  539. const char* arrSeparators[2] = {"|", "+"};
  540. bool bMulti(false);
  541. int nIdx(0);
  542. for (int i = 0; i < 2; i++)
  543. {
  544. if (strstr(szVol, arrSeparators[i]) != NULL)
  545. {
  546. nIdx = i;
  547. bMulti = true;
  548. break;
  549. }
  550. }
  551. if (bMulti)
  552. {
  553. vector<vector<string>> vctSplit;
  554. for (int i = 0; i < vctData.size(); i++)
  555. {
  556. vector<string> vct;
  557. if (!split(vctData[i], arrSeparators[nIdx], vct))
  558. {
  559. return false;
  560. }
  561. vctSplit.push_back(vct);
  562. }
  563. int nCount(vctSplit[0].size());
  564. for (int i = 1; i < vctSplit.size(); i++)
  565. {
  566. if (nCount != vctSplit[i].size())
  567. return false;
  568. }
  569. for (int i = 0; i < nCount; i++)
  570. {
  571. if (i != 0)
  572. strDes.append("+");
  573. if (strlen((vctSplit[0])[i].c_str()) == 0)
  574. strDes.append("--");
  575. else
  576. {
  577. strDes.append((vctSplit[0])[i]);
  578. }
  579. string strTemp;
  580. for (int j = 1; j < vctSplit.size(); j++)
  581. {
  582. if (strlen((vctSplit[j])[i].c_str()) != 0)
  583. {
  584. strTemp.append((vctSplit[j])[i]);
  585. strTemp.append(",");
  586. }
  587. }
  588. if (!strTemp.empty())
  589. {
  590. if (strTemp[strTemp.size() - 1] == ',')
  591. {
  592. strTemp = string(strTemp.c_str(), strTemp.size() - 1);
  593. }
  594. }
  595. if (!strTemp.empty())
  596. {
  597. strDes.append("(");
  598. strDes.append(strTemp);
  599. strDes.append(")");
  600. }
  601. }
  602. }
  603. else
  604. {
  605. if (strlen(szVol) == 0)
  606. {
  607. strDes.append("--");
  608. }
  609. else
  610. {
  611. strDes.append(szVol);
  612. }
  613. string strTemp;
  614. for (int i = 1; i < vctData.size(); i++)
  615. {
  616. if (strlen(vctData[i]) != 0)
  617. {
  618. strTemp.append(vctData[i]);
  619. strTemp.append(",");
  620. }
  621. }
  622. if (!strTemp.empty())
  623. {
  624. if (strTemp[strTemp.size() - 1] == ',')
  625. {
  626. strTemp = string(strTemp.c_str(), strTemp.size() - 1);
  627. }
  628. }
  629. if (!strTemp.empty())
  630. {
  631. strDes.append("(");
  632. strDes.append(strTemp);
  633. strDes.append(")");
  634. }
  635. }
  636. return true;
  637. }
  638. //获取ODBC中Excel驱动函数
  639. CString GetExcelDriverInfo()
  640. {
  641. TCHAR szBuf[2001];
  642. WORD cbBufMax = 2000;
  643. WORD cbBufOut;
  644. TCHAR *pszBuf = szBuf;
  645. CString sDriver=L"";
  646. // 获取已安装驱动的名称(涵数在odbcinst.h里)
  647. if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
  648. return L"";
  649. // 检索已安装的驱动是否有Excel...
  650. do
  651. {
  652. if (StrStrW(pszBuf, L"Excel") != 0)
  653. {
  654. //发现 !
  655. CString temp(pszBuf);
  656. //if(sDriver.GetLength() < temp.GetLength())
  657. sDriver = temp;
  658. break;
  659. }
  660. pszBuf = StrStrW(pszBuf, L'\0') + 1;
  661. }
  662. while (pszBuf[1] != L'\0');
  663. return sDriver;
  664. }
  665. __int64 StringToInt64(const char *szSource)
  666. {
  667. __int64 ret(0);
  668. if (strlen(szSource) == 0)
  669. return ret;
  670. const char *pTemp = szSource + strlen(szSource) - 1;
  671. __int64 nRight(1);
  672. bool bNegative(false);
  673. if ('-' == szSource[0])
  674. bNegative = true;
  675. do
  676. {
  677. if (*pTemp >= '0' && *pTemp <= '9')
  678. {
  679. ret += nRight * (0 + (*pTemp - '0'));
  680. nRight *= 10;
  681. }
  682. }
  683. while(pTemp-- != szSource);
  684. if (bNegative)
  685. ret = -ret;
  686. return ret;
  687. }
  688. string GetRequestByKey(const string& strKey, const string& strReuest)
  689. {
  690. try
  691. {
  692. smatch result;
  693. if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*?)&")))
  694. {
  695. return result[1];
  696. }
  697. else if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*)")))
  698. {
  699. return result[1];
  700. }
  701. else
  702. {
  703. return string();
  704. }
  705. }
  706. catch (...)
  707. {
  708. return string();
  709. }
  710. return string();
  711. }
  712. void GetKeysByUrl(const string & strUrl, vector<string> & vctKey)
  713. {
  714. vector<string> vctSearch;
  715. split(strUrl, "?", vctSearch);
  716. if (vctSearch.size() != 2 && vctSearch.size() != 1)
  717. {
  718. return;
  719. }
  720. string strTemp;
  721. if (vctSearch.size() == 1)
  722. {
  723. strTemp = vctSearch[0];
  724. }
  725. else if (vctSearch.size() == 2)
  726. {
  727. strTemp = vctSearch[1];
  728. }
  729. vctSearch.clear();
  730. split(strTemp, "&", vctSearch);
  731. if (vctSearch.size() <= 0)
  732. {
  733. return;
  734. }
  735. for (int i = 0; i < vctSearch.size(); i++)
  736. {
  737. strTemp = vctSearch[i];
  738. vector<string> vct;
  739. split(strTemp, "=", vct);
  740. if (vct.size() == 2)
  741. {
  742. vctKey.push_back(vct[0]);
  743. }
  744. }
  745. }
  746. __int64 GetCharNumber(char* szText, size_t n)
  747. {
  748. __int64 nDest;
  749. if (n == 0)
  750. {
  751. return -1;
  752. }
  753. for (nDest = 0; n--; szText++)
  754. {
  755. if (*szText < '0' || *szText > '9')
  756. {
  757. continue; //继续获取下一个字符
  758. }
  759. /*
  760. 1、当前字符值 *line的ascii码,减去字符0的ascii码,得出个位数字
  761. 2、原计算的value值 乘以10,向上提升一位
  762. 3、二者相加得到新的十进制数值
  763. */
  764. nDest = nDest * 10 + (*szText - '0');
  765. }
  766. if (nDest < 0)
  767. {
  768. return -1;
  769. }
  770. else
  771. {
  772. return nDest;
  773. }
  774. }
  775. CStringW CStrA2W(const CStringA &cstrSrcA)
  776. {
  777. int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0);
  778. wchar_t *wstr = new wchar_t[len];
  779. memset(wstr, 0, len*sizeof(wchar_t));
  780. MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len);
  781. CStringW cstrDestW = wstr;
  782. delete[] wstr;
  783. return cstrDestW;
  784. }
  785. CStringA CStrW2A(const CStringW &cstrSrcW)
  786. {
  787. int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);
  788. char *str = new char[len];
  789. memset(str, 0, len);
  790. WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);
  791. CStringA cstrDestA = str;
  792. delete[] str;
  793. return cstrDestA;
  794. }
  795. bool RegexStdMatch(string src, string regular)
  796. {
  797. regex pattern(regular.c_str());
  798. if (!regex_match(src, pattern))
  799. {
  800. return false;
  801. }
  802. return true;
  803. }
  804. bool RegexStdMatch(wstring src, wstring regular)
  805. {
  806. char* pre = setlocale(LC_ALL, "");
  807. setlocale(LC_ALL, "chs");
  808. std::wregex pattern(regular.c_str());
  809. if (!regex_match(src, pattern))
  810. {
  811. return false;
  812. }
  813. return true;
  814. }
  815. string replace_all(string& str, const string& old_value, const string& new_value)
  816. {
  817. while (true)
  818. {
  819. string::size_type pos(0);
  820. if ((pos = str.find(old_value)) != string::npos)
  821. str.replace(pos, old_value.length(), new_value);
  822. else break;
  823. }
  824. return str;
  825. }
  826. void sync_system_time(ULONGLONG time)
  827. {
  828. time_t _time = time;
  829. struct tm *tim = localtime(&_time);
  830. SYSTEMTIME system_time = {0}; //先获取本地时间
  831. system_time.wYear = tim->tm_year+1900;
  832. system_time.wMonth =tim->tm_mon+1;
  833. system_time.wDay = tim->tm_mday;
  834. system_time.wHour = tim->tm_hour;
  835. system_time.wMinute = tim->tm_min;
  836. system_time.wSecond = tim->tm_sec;
  837. SetLocalTime(&system_time);
  838. }