CLewaimaiString.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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::DoubleToString(const double value, unsigned int precisionAfterPoint)
  259. {
  260. char str[256];
  261. std::string pre = "%." + to_string(precisionAfterPoint) + "f";
  262. sprintf(str, pre.c_str(), value);
  263. string result = str;
  264. return result;
  265. }
  266. bool CLewaimaiString::isIPAddressValid(const char* pszIPAddr)
  267. {
  268. if(!pszIPAddr)
  269. {
  270. return false; //若pszIPAddr为空
  271. }
  272. char IP1[100], cIP[4];
  273. int len = strlen(pszIPAddr);
  274. int i = 0, j = len - 1;
  275. int k, m = 0, n = 0, num = 0;
  276. //去除首尾空格(取出从i-1到j+1之间的字符):
  277. while(pszIPAddr[i++] == ' ');
  278. while(pszIPAddr[j--] == ' ');
  279. for(k = i - 1; k <= j + 1; k++)
  280. {
  281. IP1[m++] = *(pszIPAddr + k);
  282. }
  283. IP1[m] = '\0';
  284. char* p = IP1;
  285. while(*p != '\0')
  286. {
  287. if(*p == ' ' || *p < '0' || *p > '9')
  288. {
  289. return false;
  290. }
  291. cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头
  292. int sum = 0; //sum为每一子段的数值,应在0到255之间
  293. while(*p != '.' && *p != '\0')
  294. {
  295. if(*p == ' ' || *p < '0' || *p > '9')
  296. {
  297. return false;
  298. }
  299. sum = sum * 10 + *p - 48; //每一子段字符串转化为整数
  300. p++;
  301. }
  302. if(*p == '.')
  303. {
  304. if((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9')) //判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.”
  305. {
  306. num++; //记录“.”出现的次数,不能大于3
  307. }
  308. else
  309. {
  310. return false;
  311. }
  312. };
  313. if((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3)
  314. {
  315. return false; //若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP
  316. }
  317. if(*p != '\0')
  318. {
  319. p++;
  320. }
  321. n = 0;
  322. }
  323. if(num != 3)
  324. {
  325. return false;
  326. }
  327. return true;
  328. }
  329. vector<string> CLewaimaiString::Split(const string& in, const string& delim)
  330. {
  331. vector<string> ret;
  332. try
  333. {
  334. regex re{ delim };
  335. return vector<string>
  336. {
  337. sregex_token_iterator(in.begin(), in.end(), re, -1),
  338. sregex_token_iterator()
  339. };
  340. }
  341. catch(const std::exception& e)
  342. {
  343. cout << "error:" << e.what() << std::endl;
  344. }
  345. return ret;
  346. }
  347. /*
  348. *nNum是表示替换几个,默认为1表示替换第1个
  349. **/
  350. int CLewaimaiString::Replace(std::wstring& strContent, std::wstring strReplace, std::wstring strDest, int nNum)
  351. {
  352. int nCount = 0;
  353. while(true)
  354. {
  355. size_t pos = strContent.find(strReplace);
  356. if(pos != std::wstring::npos)
  357. {
  358. WCHAR pBuf[1] = { L'\0' };
  359. strContent.replace(pos, strReplace.length(), pBuf, 0);
  360. strContent.insert(pos, strDest);
  361. nCount++;
  362. if(nCount == nNum)
  363. {
  364. break;
  365. }
  366. }
  367. else
  368. {
  369. break;
  370. }
  371. }
  372. return 0;
  373. }