StringOpt.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "stdafx.h"
  2. #include "StringOpt.h"
  3. #include <windows.h>
  4. #include <cctype>
  5. #include <algorithm>
  6. #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
  7. #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
  8. namespace CCOS_Kernel
  9. {
  10. int CStringOpt::GetCharacterSetPageCode(const std::string& strPageCode)
  11. {
  12. if (strPageCode.find("IR 192") > 0)
  13. {
  14. //ISO_IR 192: Unicode in UTF-8; 65001 utf-8 Unicode (UTF-8)
  15. return 65001;
  16. }
  17. else if (strPageCode.find("IR 100") > 0)
  18. {
  19. //ISO_IR 100: Latin alphabet No. 1; 28591 iso-8859-1 Western European (ISO)
  20. return 0;//28591;
  21. }
  22. else if (strPageCode.find("IR 101") > 0)
  23. {
  24. //ISO_IR 101: Latin alphabet No. 2; 28592 iso-8859-2 Central European (ISO)
  25. return 28592;
  26. }
  27. else if (strPageCode.find("IR 109") > 0)
  28. {
  29. //ISO_IR 109: Latin alphabet No. 3; 28593 iso-8859-3 Latin 3 (ISO)
  30. return 28593;
  31. }
  32. else if (strPageCode.find("IR 110") > 0)
  33. {
  34. //ISO_IR 110: Latin alphabet No. 4; 28594 iso-8859-4 Baltic (ISO)
  35. return 28594;
  36. }
  37. else if (strPageCode.find("IR 144") > 0)
  38. {
  39. //ISO_IR 144: Cyrillic; 28595 iso-8859-5 Cyrillic (ISO)
  40. return 28595;
  41. }
  42. else if (strPageCode.find("IR 127") > 0)
  43. {
  44. //ISO_IR 127: Arabic; 28596 iso-8859-6 Arabic (ISO)
  45. return 28596;
  46. }
  47. else if (strPageCode.find("IR 126") > 0)
  48. {
  49. //ISO_IR 126: Greek; 28597 iso-8859-7 Greek (ISO)
  50. return 28597;
  51. }
  52. else if (strPageCode.find("IR 138") > 0)
  53. {
  54. //ISO_IR 138: Hebrew; 28598 iso-8859-8 Hebrew (ISO-Visual)
  55. return 28598;
  56. }
  57. else if (strPageCode.find("IR 148") > 0)
  58. {
  59. //ISO_IR 148: Latin alphabet No. 5; 28599 iso-8859-9 Turkish (ISO)
  60. return 28599;
  61. }
  62. //else if (strPageCodeName.Find("IR 13") > 0)
  63. //{
  64. // OutputDebugString("*********************************select IR 13");
  65. // //ISO_IR 13: Japanese; 50222 iso-2022-jp Japanese (JIS-Allow 1 byte Kana - SO/SI)
  66. // return 50222;
  67. //}
  68. else if (strPageCode.find("IR 13") > 0 || strPageCode.find("IR 14") > 0 || strPageCode.find("IR 87") > 0 || strPageCode.find("IR 159") > 0)
  69. {
  70. return 932;
  71. }
  72. else if (strPageCode.find("IR 166") > 0) //ISO_IR 166, ISO 2022 IR 166 -- Thai, TIS 620-2533 (1990)
  73. {
  74. return 874; //windows-874 ANSI/OEM Thai (ISO 8859-11); Thai (Windows)
  75. }
  76. else if (strPageCode.find("IR 149") > 0) //ISO 2022 IR 149 -- Korean
  77. {
  78. return 50225;
  79. }
  80. else if (strPageCode.find("IR 58") > 0) // Simplified Chinese,ISO 2022 IR 58 -- x-cp50227
  81. {
  82. return 50227;
  83. }
  84. else if (strPageCode.find("GB2312") > 0) //GB2312
  85. {
  86. return 52936;
  87. }
  88. else if (strPageCode.find("GBK") > 0) //GBK
  89. {
  90. return 936;
  91. }
  92. else if (strPageCode.find("GB18030") > 0)
  93. {
  94. //GB18030: GB18030; 54936 GB18030 Chinese Simplified (GB18030)
  95. return 54936;
  96. }
  97. return 0;
  98. }
  99. bool CStringOpt::HexString2Integer(const std::wstring& wstrSource, int& nResult)
  100. {
  101. int iResult = 0;
  102. //正负数的标识,1正 -1负
  103. int iFlag = 1;
  104. //判断字符串是否合法
  105. if (wstrSource.length() == 0 || (wstrSource[0] == '+' && wstrSource.length() == 1) || (wstrSource[0] == '-' && wstrSource.length() == 1)
  106. || (wstrSource[0] == 0x30 && wstrSource[1] == 0x78 && wstrSource.length() == 2))
  107. return false;
  108. //复制一份十六进制字符串HexStr到内存空间
  109. std::wstring wstrHexStrPt = wstrSource;
  110. wstrHexStrPt = TrimLeft(wstrHexStrPt);
  111. int nIndex = 0;
  112. //长度去掉正负号,并设置字符数的标识
  113. if (wstrHexStrPt[nIndex] == '+')
  114. ++nIndex;
  115. else if(wstrHexStrPt[nIndex] == '-')
  116. {
  117. iFlag = -1;
  118. ++nIndex;
  119. }
  120. //长度去掉"0x"开头的十六进制标识符
  121. if (wstrHexStrPt[nIndex] == '0' && wstrHexStrPt[nIndex + 1] == 'x')
  122. nIndex += 2;
  123. //循环将每个十六进制的字符转换成对应的十进制整数,出现非法字符则直接返回
  124. while (nIndex < wstrSource.length())
  125. {
  126. if ((wstrHexStrPt[nIndex] >= 48) && (wstrHexStrPt[nIndex] <= 57))
  127. wstrHexStrPt[nIndex] -= 48;
  128. else if ((wstrHexStrPt[nIndex] >= 65) && (wstrHexStrPt[nIndex] <= 70))
  129. wstrHexStrPt[nIndex] -= 65 - 10;
  130. else if ((wstrHexStrPt[nIndex] >= 97) && (wstrHexStrPt[nIndex] <= 102))
  131. wstrHexStrPt[nIndex] -= 97 - 10;
  132. else
  133. return false;
  134. //iResult = *p + iResult*16; 经过 @大致 的提醒,使用移位运算
  135. iResult = wstrHexStrPt[nIndex] + (iResult << 4);
  136. ++nIndex;
  137. }
  138. //返回转换后的整数
  139. nResult = iFlag * iResult;
  140. return true;
  141. }
  142. //std::wstring CStringOpt::Replace(const std::wstring& wstrContent, const std::wstring& strReplace, const std::wstring & strDest)
  143. /*{
  144. std::wstring strContent = wstrContent;
  145. while (true)
  146. {
  147. size_t pos = strContent.find(strReplace);
  148. if (pos != std::wstring::npos)
  149. {
  150. WCHAR pBuf[1] = { L'\0' };
  151. strContent.replace(pos, strReplace.length(), pBuf, 0);
  152. strContent.insert(pos, strDest);
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159. return strContent;
  160. }*/
  161. std::string CStringOpt::Transformwstring2string(const std::wstring& wstr, unsigned int nCodePage)
  162. {
  163. if (wstr.empty())
  164. return "";
  165. int nLen = WideCharToMultiByte(nCodePage, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL);
  166. int nBufferLen = static_cast<int>(nLen + 1);
  167. char* pDes = new(std::nothrow) char[nBufferLen];
  168. if (pDes == NULL)
  169. return "";
  170. memset(pDes, 0, nBufferLen);
  171. WideCharToMultiByte(nCodePage, 0, wstr.c_str(), (int)wstr.length(), pDes, nLen, NULL, NULL);
  172. std::string strDes = pDes;
  173. SAFE_DELETE_ARRAY(pDes);
  174. return strDes;
  175. }
  176. std::wstring CStringOpt::Transformstring2wstring(const std::string& str, unsigned int nCodePage)
  177. {
  178. if (str.empty())
  179. return L"";
  180. std::wstring val = L"";
  181. int nLen = MultiByteToWideChar(nCodePage, 0, str.c_str(), -1, NULL, 0);
  182. int nBufferLen = static_cast<int>(nLen + 1);
  183. wchar_t* pDes = new(std::nothrow) wchar_t[nBufferLen];
  184. if (pDes == NULL)
  185. return L"";
  186. memset(pDes, 0, sizeof(wchar_t)*(nBufferLen));
  187. MultiByteToWideChar(nCodePage, 0, str.c_str(), -1, pDes, nLen);
  188. std::wstring strDes = pDes;
  189. SAFE_DELETE_ARRAY(pDes);
  190. return strDes;
  191. }
  192. std::wstring CStringOpt::StringFormat(const wchar_t *wszformat, ...)
  193. {
  194. int nCount = 0;
  195. va_list argptr;
  196. va_start(argptr, wszformat);
  197. nCount = _vsnwprintf(NULL, 0, wszformat, argptr);
  198. va_end(argptr);
  199. int nBufferLen = static_cast<int>(nCount + 1);
  200. wchar_t* pwszBuf = new(std::nothrow) wchar_t[nBufferLen];
  201. if (pwszBuf == NULL)
  202. return L"";
  203. memset(pwszBuf, 0, nBufferLen*sizeof(wchar_t));
  204. va_start(argptr, wszformat);
  205. _vsnwprintf_s(pwszBuf, nBufferLen, nCount, wszformat, argptr);
  206. va_end(argptr);
  207. std::wstring wstr = pwszBuf;
  208. SAFE_DELETE_ARRAY(pwszBuf);
  209. return wstr;
  210. }
  211. std::string CStringOpt::TrimLeft(const std::string & strSource, const std::string & strTrim)
  212. {
  213. std::string str = strSource;
  214. return str.erase(0, strSource.find_first_not_of(strTrim));
  215. }
  216. std::string CStringOpt::TrimRight(const std::string & strSource, const std::string & strTrim)
  217. {
  218. std::string str = strSource;
  219. return str.erase(str.find_last_not_of(strTrim) + 1);
  220. }
  221. std::string CStringOpt::Trim(const std::string & strSource, const std::string & strTrim)
  222. {
  223. std::string str = strSource;
  224. return TrimLeft(TrimRight(str, strTrim), strTrim);
  225. }
  226. std::wstring CStringOpt::TrimLeft(const std::wstring & strSource, const std::wstring & strTrim)
  227. {
  228. std::wstring str = strSource;
  229. return str.erase(0, strSource.find_first_not_of(strTrim));
  230. }
  231. std::wstring CStringOpt::TrimRight(const std::wstring & strSource, const std::wstring & strTrim)
  232. {
  233. std::wstring str = strSource;
  234. return str.erase(str.find_last_not_of(strTrim) + 1);
  235. }
  236. std::wstring CStringOpt::Trim(const std::wstring & strSource, const std::wstring & strTrim)
  237. {
  238. std::wstring str = strSource;
  239. return TrimLeft(TrimRight(str, strTrim), strTrim);
  240. }
  241. std::string CStringOpt::Replace(const std::string& org, const std::string &keystr, const std::string &replacestr)
  242. {
  243. std::string strOrg = org;
  244. std::string::size_type pos = 0;
  245. std::string::size_type keylen = keystr.size();
  246. std::string::size_type replen = replacestr.size();
  247. while ((pos = strOrg.find(keystr, pos)) != std::string::npos)
  248. {
  249. strOrg.replace(pos, keylen, replacestr);
  250. pos += replen;
  251. }
  252. return strOrg;
  253. }
  254. std::wstring CStringOpt::Replace(const std::wstring& org, const std::wstring &keystr, const std::wstring &replacestr)
  255. {
  256. std::wstring strOrg = org;
  257. std::wstring::size_type pos = 0;
  258. std::wstring::size_type keylen = keystr.size();
  259. std::wstring::size_type replen = replacestr.size();
  260. while ((pos = strOrg.find(keystr, pos)) != std::wstring::npos)
  261. {
  262. strOrg.replace(pos, keylen, replacestr);
  263. pos += replen;
  264. }
  265. return strOrg;
  266. }
  267. std::string CStringOpt::MakeUpper(const std::string & strSource)
  268. {
  269. std::string strDes = strSource;
  270. std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::toupper);
  271. return strDes;
  272. }
  273. std::wstring CStringOpt::MakeUpper(const std::wstring & strSource)
  274. {
  275. std::wstring strDes = strSource;
  276. std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::toupper);
  277. return strDes;
  278. }
  279. std::string CStringOpt::MakeLower(const std::string & strSource)
  280. {
  281. std::string strDes = strSource;
  282. std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::tolower);
  283. return strDes;
  284. }
  285. std::wstring CStringOpt::MakeLower(const std::wstring & strSource)
  286. {
  287. std::wstring strDes = strSource;
  288. std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::tolower);
  289. return strDes;
  290. }
  291. static int HexTable[103] =
  292. { 0,0,0,0,0,0,0,0,0,
  293. 0,0,0,0,0,0,0,0,0,
  294. 0,0,0,0,0,0,0,0,0,
  295. 0,0,0,0,0,0,0,0,0,
  296. 0,0,0,0,0,0,0,0,0,
  297. 0,0,0,0,1,2,3,4,5,
  298. 6,7,8,9,0,0,0,0,0,
  299. 0,0,10,11,12,13,14,15,0,
  300. 0,0,0,0,0,0,0,0,0,
  301. 0,0,0,0,0,0,0,0,0,
  302. 0,0,0,0,0,0,0,10,11,
  303. 12,13,14,15
  304. };
  305. bool CStringOpt::Hex2Decimal(const std::string & hex_str, int& nDecimal)
  306. {
  307. nDecimal = 0;
  308. for (size_t i = 0; i < hex_str.length(); i++)
  309. {
  310. int nValue = hex_str[i];
  311. if (nValue >= 0 && nValue < 103)
  312. nDecimal = (nDecimal << 4) | HexTable[nValue];
  313. else
  314. return false;
  315. }
  316. return true;
  317. }
  318. bool CStringOpt::Hex2Decimal(const std::wstring & hex_str, int& nDecimal)
  319. {
  320. nDecimal = 0;
  321. for (size_t i = 0; i < hex_str.length(); i++)
  322. {
  323. int nValue = hex_str[i];
  324. if (nValue >= 0 && nValue < 103)
  325. nDecimal = (nDecimal << 4) | HexTable[nValue];
  326. else
  327. return false;
  328. }
  329. return true;
  330. }
  331. std::wstring CStringOpt::String(unsigned char Value)
  332. {
  333. return StringFormat(L"%d", Value);
  334. }
  335. std::wstring CStringOpt::String(char Value)
  336. {
  337. return StringFormat(L"%c", Value);
  338. }
  339. std::wstring CStringOpt::String(unsigned short Value)
  340. {
  341. return StringFormat(L"%u", Value);
  342. }
  343. std::wstring CStringOpt::String(short Value)
  344. {
  345. return StringFormat(L"%d", Value);
  346. }
  347. std::wstring CStringOpt::String(int nValue)
  348. {
  349. return StringFormat(L"%d", nValue);
  350. }
  351. std::wstring CStringOpt::String(unsigned int uValue)
  352. {
  353. return StringFormat(L"%u", uValue);
  354. }
  355. std::wstring CStringOpt::String(float fValue)
  356. {
  357. return StringFormat(L"%f", fValue);
  358. }
  359. std::wstring CStringOpt::String(double dfValue)
  360. {
  361. return StringFormat(L"%lf", dfValue);
  362. }
  363. std::wstring CStringOpt::String(long long llValue)
  364. {
  365. return StringFormat(L"%I64d", llValue);
  366. }
  367. std::wstring CStringOpt::String(unsigned long long ullValue)
  368. {
  369. return StringFormat(L"%I64u", ullValue);
  370. }
  371. //bool CStringOpt::IsDigital(const std::wstring& wstr)
  372. //{
  373. // for each (auto var in wstr)
  374. // {
  375. // if (!isdigit(var))
  376. // return false;
  377. // }
  378. // return true;
  379. //}
  380. }