CLewaimaiString.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "../pch/pch.h"
  2. #include "CLewaimaiString.h"
  3. #include <boost/archive/iterators/base64_from_binary.hpp>
  4. #include <boost/archive/iterators/binary_from_base64.hpp>
  5. #include <boost/archive/iterators/transform_width.hpp>
  6. #include <string>
  7. #include <iostream>
  8. #include <sstream>
  9. using namespace std;
  10. using namespace boost::archive::iterators;
  11. unsigned char ToHex(unsigned char x)
  12. {
  13. return x > 9 ? x + 55 : x + 48;
  14. }
  15. unsigned char FromHex(unsigned char x)
  16. {
  17. unsigned char y;
  18. if(x >= 'A' && x <= 'Z')
  19. {
  20. y = x - 'A' + 10;
  21. }
  22. else if(x >= 'a' && x <= 'z')
  23. {
  24. y = x - 'a' + 10;
  25. }
  26. else if(x >= '0' && x <= '9')
  27. {
  28. y = x - '0';
  29. }
  30. else
  31. {
  32. assert(0);
  33. }
  34. return y;
  35. }
  36. CLewaimaiString::CLewaimaiString()
  37. {
  38. }
  39. CLewaimaiString::~CLewaimaiString()
  40. {
  41. }
  42. bool CLewaimaiString::base64_encode(const string& input, string* output)
  43. {
  44. typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
  45. stringstream result;
  46. try
  47. {
  48. copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
  49. }
  50. catch(...)
  51. {
  52. return false;
  53. }
  54. size_t equal_count = (3 - input.length() % 3) % 3;
  55. for(size_t i = 0; i < equal_count; i++)
  56. {
  57. result.put('=');
  58. }
  59. *output = result.str();
  60. return output->empty() == false;
  61. }
  62. bool CLewaimaiString::base64_decode(const string& input, string* output)
  63. {
  64. typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
  65. stringstream result;
  66. try
  67. {
  68. copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
  69. }
  70. catch(...)
  71. {
  72. return false;
  73. }
  74. *output = result.str();
  75. return output->empty() == false;
  76. }
  77. void CLewaimaiString::trim(string& s)
  78. {
  79. if(!s.empty())
  80. {
  81. s.erase(0, s.find_first_not_of(" "));
  82. s.erase(s.find_last_not_of(" ") + 1);
  83. }
  84. }
  85. std::string CLewaimaiString::UrlEncode(const std::string& str)
  86. {
  87. std::string strTemp = "";
  88. size_t length = str.length();
  89. for(size_t i = 0; i < length; i++)
  90. {
  91. if(isalnum((unsigned char)str[i]) ||
  92. (str[i] == '-') ||
  93. (str[i] == '_') ||
  94. (str[i] == '.') ||
  95. (str[i] == '~'))
  96. {
  97. strTemp += str[i];
  98. }
  99. else if(str[i] == ' ')
  100. {
  101. strTemp += "+";
  102. }
  103. else
  104. {
  105. strTemp += '%';
  106. strTemp += ToHex((unsigned char)str[i] >> 4);
  107. strTemp += ToHex((unsigned char)str[i] % 16);
  108. }
  109. }
  110. return strTemp;
  111. }
  112. std::string CLewaimaiString::UrlDecode(const std::string& str)
  113. {
  114. std::string strTemp = "";
  115. size_t length = str.length();
  116. for(size_t i = 0; i < length; i++)
  117. {
  118. if(str[i] == '+')
  119. {
  120. strTemp += ' ';
  121. }
  122. else if(str[i] == '%')
  123. {
  124. assert(i + 2 < length);
  125. unsigned char high = FromHex((unsigned char)str[++i]);
  126. unsigned char low = FromHex((unsigned char)str[++i]);
  127. strTemp += high * 16 + low;
  128. }
  129. else
  130. {
  131. strTemp += str[i];
  132. }
  133. }
  134. return strTemp;
  135. }
  136. /*
  137. std::string CLewaimaiString::UnicodeToUTF8(const std::wstring wstr)
  138. {
  139. std::string ret;
  140. try
  141. {
  142. std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
  143. ret = wcv.to_bytes(wstr);
  144. }
  145. catch(const std::exception& e)
  146. {
  147. std::cerr << e.what() << std::endl;
  148. }
  149. return ret;
  150. }*/
  151. /*
  152. std::wstring CLewaimaiString::UTF8ToUnicode(const std::string str)
  153. {
  154. std::wstring ret;
  155. try
  156. {
  157. std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
  158. ret = wcv.from_bytes(str);
  159. }
  160. catch(const std::exception& e)
  161. {
  162. std::cerr << e.what() << std::endl;
  163. }
  164. return ret;
  165. }*/
  166. std::string CLewaimaiString::UnicodeToUTF8(const std::wstring wstr)
  167. {
  168. char* pElementText;
  169. int iTextLen;
  170. // wide char to multi char
  171. iTextLen = WideCharToMultiByte(CP_UTF8,
  172. 0,
  173. wstr.c_str(),
  174. -1,
  175. NULL,
  176. 0,
  177. NULL,
  178. NULL);
  179. pElementText = new char[iTextLen + 1];
  180. memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
  181. ::WideCharToMultiByte(CP_UTF8,
  182. 0,
  183. wstr.c_str(),
  184. -1,
  185. pElementText,
  186. iTextLen,
  187. NULL,
  188. NULL);
  189. string strText;
  190. strText = pElementText;
  191. delete[] pElementText;
  192. return strText;
  193. }
  194. std::wstring CLewaimaiString::UTF8ToUnicode(const std::string str)
  195. {
  196. int len = 0;
  197. len = str.length();
  198. int unicodeLen = ::MultiByteToWideChar(CP_UTF8,
  199. 0,
  200. str.c_str(),
  201. -1,
  202. NULL,
  203. 0);
  204. wchar_t* pUnicode;
  205. pUnicode = new wchar_t[unicodeLen + 1];
  206. memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
  207. ::MultiByteToWideChar(CP_UTF8,
  208. 0,
  209. str.c_str(),
  210. -1,
  211. (LPWSTR)pUnicode,
  212. unicodeLen);
  213. wstring rt;
  214. rt = (wchar_t*)pUnicode;
  215. delete pUnicode;
  216. return rt;
  217. }
  218. std::string CLewaimaiString::UnicodeToANSI(const std::wstring wstr)
  219. {
  220. unsigned len = wstr.size() * 4;
  221. if(len == 0)
  222. {
  223. return "";
  224. }
  225. setlocale(LC_CTYPE, "");
  226. char* p = new char[len];
  227. wcstombs(p, wstr.c_str(), len);
  228. std::string str1(p);
  229. delete[] p;
  230. return str1;
  231. }
  232. std::wstring CLewaimaiString::ANSIToUnicode(const std::string str)
  233. {
  234. unsigned len = str.size() * 2;// 预留字节数
  235. if(len == 0)
  236. {
  237. return L"";
  238. }
  239. setlocale(LC_CTYPE, ""); //必须调用此函数
  240. wchar_t* p = new wchar_t[len];// 申请一段内存存放转换后的字符串
  241. mbstowcs(p, str.c_str(), len);// 转换
  242. std::wstring str1(p);
  243. delete[] p;// 释放申请的内存
  244. return str1;
  245. }
  246. std::string CLewaimaiString::UTF8ToANSI(const std::string str)
  247. {
  248. std::wstring wsUnicode = CLewaimaiString::UTF8ToUnicode(str);
  249. std::string sAnsi = CLewaimaiString::UnicodeToANSI(wsUnicode);
  250. return sAnsi;
  251. }
  252. std::string CLewaimaiString::ANSIToUTF8(const std::string str)
  253. {
  254. std::wstring wsUnicode = CLewaimaiString::ANSIToUnicode(str);
  255. std::string sUtf8 = CLewaimaiString::UnicodeToUTF8(wsUnicode);
  256. return sUtf8;
  257. }
  258. std::string CLewaimaiString::UnicodeToGB2312(const wstring& wstr)
  259. {
  260. std::string result;
  261. int n = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
  262. result.resize(n);
  263. ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0);
  264. return result;
  265. }
  266. std::wstring CLewaimaiString::GB2312ToUnicode(const string& src)
  267. {
  268. std::wstring result;
  269. int n = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, NULL, 0);
  270. result.resize(n);
  271. ::MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
  272. return result;
  273. }
  274. std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint)
  275. {
  276. char str[256];
  277. std::string pre = "%." + to_string(precisionAfterPoint) + "f";
  278. sprintf(str, pre.c_str(), value);
  279. string result = str;
  280. return result;
  281. }
  282. bool CLewaimaiString::isIPAddressValid(const char* pszIPAddr)
  283. {
  284. if(!pszIPAddr)
  285. {
  286. return false; //若pszIPAddr为空
  287. }
  288. char IP1[100], cIP[4];
  289. int len = strlen(pszIPAddr);
  290. int i = 0, j = len - 1;
  291. int k, m = 0, n = 0, num = 0;
  292. //去除首尾空格(取出从i-1到j+1之间的字符):
  293. while(pszIPAddr[i++] == ' ');
  294. while(pszIPAddr[j--] == ' ');
  295. for(k = i - 1; k <= j + 1; k++)
  296. {
  297. IP1[m++] = *(pszIPAddr + k);
  298. }
  299. IP1[m] = '\0';
  300. char* p = IP1;
  301. while(*p != '\0')
  302. {
  303. if(*p == ' ' || *p < '0' || *p > '9')
  304. {
  305. return false;
  306. }
  307. cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头
  308. int sum = 0; //sum为每一子段的数值,应在0到255之间
  309. while(*p != '.' && *p != '\0')
  310. {
  311. if(*p == ' ' || *p < '0' || *p > '9')
  312. {
  313. return false;
  314. }
  315. sum = sum * 10 + *p - 48; //每一子段字符串转化为整数
  316. p++;
  317. }
  318. if(*p == '.')
  319. {
  320. if((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9')) //判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.”
  321. {
  322. num++; //记录“.”出现的次数,不能大于3
  323. }
  324. else
  325. {
  326. return false;
  327. }
  328. };
  329. if((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3)
  330. {
  331. return false; //若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP
  332. }
  333. if(*p != '\0')
  334. {
  335. p++;
  336. }
  337. n = 0;
  338. }
  339. if(num != 3)
  340. {
  341. return false;
  342. }
  343. return true;
  344. }
  345. vector<string> CLewaimaiString::Split(const string& in, const string& delim)
  346. {
  347. vector<string> ret;
  348. if (in == "")
  349. {
  350. //空字符串,直接返回空vector
  351. return ret;
  352. }
  353. try
  354. {
  355. regex re{ delim };
  356. return vector<string>
  357. {
  358. sregex_token_iterator(in.begin(), in.end(), re, -1),
  359. sregex_token_iterator()
  360. };
  361. }
  362. catch(const std::exception& e)
  363. {
  364. cout << "error:" << e.what() << std::endl;
  365. }
  366. return ret;
  367. }
  368. string CLewaimaiString::Merge(const vector<string>& in, const string& delim)
  369. {
  370. std::string ret = "";
  371. for (size_t i = 0; i < in.size(); i++)
  372. {
  373. ret += in[i];
  374. if (i < in.size() - 1)
  375. {
  376. ret += delim;
  377. }
  378. }
  379. return ret;
  380. }
  381. /*
  382. *nNum是表示替换几个,默认为1表示替换第1个
  383. **/
  384. int CLewaimaiString::Replace(std::wstring& strContent, std::wstring strReplace, std::wstring strDest, int nNum)
  385. {
  386. int nCount = 0;
  387. while(true)
  388. {
  389. size_t pos = strContent.find(strReplace);
  390. if(pos != std::wstring::npos)
  391. {
  392. WCHAR pBuf[1] = { L'\0' };
  393. strContent.replace(pos, strReplace.length(), pBuf, 0);
  394. strContent.insert(pos, strDest);
  395. nCount++;
  396. if(nCount == nNum)
  397. {
  398. break;
  399. }
  400. }
  401. else
  402. {
  403. break;
  404. }
  405. }
  406. return 0;
  407. }
  408. //整个替换所有的字符串,把strBig钟的strsrc全部替换成strdst
  409. void CLewaimaiString::string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
  410. {
  411. std::string::size_type pos = 0;
  412. std::string::size_type srclen = strsrc.size();
  413. std::string::size_type dstlen = strdst.size();
  414. while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
  415. {
  416. strBig.replace(pos, srclen, strdst);
  417. pos += dstlen;
  418. }
  419. }
  420. //从文件路径或者url中获取文件名
  421. std::string CLewaimaiString::GetPathOrURLShortName(std::string strFullName)
  422. {
  423. if (strFullName.empty())
  424. {
  425. return "";
  426. }
  427. string_replace(strFullName, "/", "\\");
  428. std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
  429. return strFullName.substr(iPos, strFullName.length() - iPos);
  430. }
  431. //得到文件路径的目录
  432. std::string CLewaimaiString::GetPathDir(std::string filePath)
  433. {
  434. std::string dirPath = filePath;
  435. size_t p = filePath.find_last_of(L'\\');
  436. if (p != -1)
  437. {
  438. dirPath.erase(p);
  439. }
  440. return dirPath;
  441. }
  442. std::string CLewaimaiString::BuZifuLeft(std::string old, int num, const char zifu)
  443. {
  444. return std::string(num, zifu) + old;
  445. }
  446. std::string CLewaimaiString::GetQuweima(std::wstring input)
  447. {
  448. //先转换成GB2312,区位码都是相对于GB2312的
  449. std::string gb_hanzi = CLewaimaiString::UnicodeToGB2312(input);
  450. std::string result = "";
  451. for (size_t i = 0; i < gb_hanzi.length() - 1; )
  452. {
  453. byte* cur = (byte*)(gb_hanzi.c_str() + i);
  454. if (*cur >= 0 && *cur <= 127)
  455. {
  456. //正常的asicc码,字符串或者数字
  457. int front = (short)(*cur - '\0');
  458. front -= 32;
  459. if (front < 10)
  460. {
  461. result += "030" + to_string(front);
  462. }
  463. else
  464. {
  465. result += "03" + to_string(front);
  466. }
  467. i++;
  468. }
  469. else
  470. {
  471. int front = (short)(cur[0] - '\0');//将字节数组的第一位转换成short类型,这里(short)code[0]也是可以的
  472. int back = (short)(cur[1] - '\0');//将字节数组的第二位转换成short类型
  473. std::string s_front = to_string(front - 160);
  474. if (front - 160 < 10)
  475. {
  476. //前面补0
  477. s_front = "0" + s_front;
  478. }
  479. std::string s_back = to_string(back - 160);
  480. if (back - 160 < 10)
  481. {
  482. //前面补0
  483. s_back = "0" + s_back;
  484. }
  485. result += s_front + s_back;
  486. i++;
  487. i++;
  488. }
  489. }
  490. return result;
  491. }
  492. std::string CLewaimaiString::SqlZhuanyi(std::string old)
  493. {
  494. CLewaimaiString::string_replace(old, "'", "''");
  495. return old;
  496. }
  497. std::wstring CLewaimaiString::ShuziInputFormat(std::wstring old, std::wstring input)
  498. {
  499. if (old == L"0")
  500. {
  501. if (input == L".")
  502. {
  503. return old + input;
  504. }
  505. else if(input == L"0")
  506. {
  507. return L"0";
  508. }
  509. else
  510. {
  511. return input;
  512. }
  513. }
  514. int pos = old.find(L".");
  515. if (pos != old.npos)
  516. {
  517. //已经有一个.了
  518. if (input == L".")
  519. {
  520. return old;
  521. }
  522. if (old.length() - pos > 2)
  523. {
  524. //小数点后已经有2个数字了
  525. return old;
  526. }
  527. return old + input;
  528. }
  529. else
  530. {
  531. //还没有小数点
  532. return old + input;
  533. }
  534. }
  535. bool CLewaimaiString::is_only_number(string str)
  536. {
  537. for (int i = 0; i < str.size(); i++)
  538. {
  539. if ((str.at(i) > '9') || (str.at(i) < '0'))
  540. {
  541. return false;
  542. }
  543. }
  544. return true;
  545. }