#include "stdafx.h" #include "StringOpt.h" #include #include #include #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } } #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } } namespace CCOS_Kernel { int CStringOpt::GetCharacterSetPageCode(const std::string& strPageCode) { if (strPageCode.find("IR 192") > 0) { //ISO_IR 192: Unicode in UTF-8; 65001 utf-8 Unicode (UTF-8) return 65001; } else if (strPageCode.find("IR 100") > 0) { //ISO_IR 100: Latin alphabet No. 1; 28591 iso-8859-1 Western European (ISO) return 0;//28591; } else if (strPageCode.find("IR 101") > 0) { //ISO_IR 101: Latin alphabet No. 2; 28592 iso-8859-2 Central European (ISO) return 28592; } else if (strPageCode.find("IR 109") > 0) { //ISO_IR 109: Latin alphabet No. 3; 28593 iso-8859-3 Latin 3 (ISO) return 28593; } else if (strPageCode.find("IR 110") > 0) { //ISO_IR 110: Latin alphabet No. 4; 28594 iso-8859-4 Baltic (ISO) return 28594; } else if (strPageCode.find("IR 144") > 0) { //ISO_IR 144: Cyrillic; 28595 iso-8859-5 Cyrillic (ISO) return 28595; } else if (strPageCode.find("IR 127") > 0) { //ISO_IR 127: Arabic; 28596 iso-8859-6 Arabic (ISO) return 28596; } else if (strPageCode.find("IR 126") > 0) { //ISO_IR 126: Greek; 28597 iso-8859-7 Greek (ISO) return 28597; } else if (strPageCode.find("IR 138") > 0) { //ISO_IR 138: Hebrew; 28598 iso-8859-8 Hebrew (ISO-Visual) return 28598; } else if (strPageCode.find("IR 148") > 0) { //ISO_IR 148: Latin alphabet No. 5; 28599 iso-8859-9 Turkish (ISO) return 28599; } //else if (strPageCodeName.Find("IR 13") > 0) //{ // OutputDebugString("*********************************select IR 13"); // //ISO_IR 13: Japanese; 50222 iso-2022-jp Japanese (JIS-Allow 1 byte Kana - SO/SI) // return 50222; //} else if (strPageCode.find("IR 13") > 0 || strPageCode.find("IR 14") > 0 || strPageCode.find("IR 87") > 0 || strPageCode.find("IR 159") > 0) { return 932; } else if (strPageCode.find("IR 166") > 0) //ISO_IR 166, ISO 2022 IR 166 -- Thai, TIS 620-2533 (1990) { return 874; //windows-874 ANSI/OEM Thai (ISO 8859-11); Thai (Windows) } else if (strPageCode.find("IR 149") > 0) //ISO 2022 IR 149 -- Korean { return 50225; } else if (strPageCode.find("IR 58") > 0) // Simplified Chinese,ISO 2022 IR 58 -- x-cp50227 { return 50227; } else if (strPageCode.find("GB2312") > 0) //GB2312 { return 52936; } else if (strPageCode.find("GBK") > 0) //GBK { return 936; } else if (strPageCode.find("GB18030") > 0) { //GB18030: GB18030; 54936 GB18030 Chinese Simplified (GB18030) return 54936; } return 0; } bool CStringOpt::HexString2Integer(const std::wstring& wstrSource, int& nResult) { int iResult = 0; //正负数的标识,1正 -1负 int iFlag = 1; //判断字符串是否合法 if (wstrSource.length() == 0 || (wstrSource[0] == '+' && wstrSource.length() == 1) || (wstrSource[0] == '-' && wstrSource.length() == 1) || (wstrSource[0] == 0x30 && wstrSource[1] == 0x78 && wstrSource.length() == 2)) return false; //复制一份十六进制字符串HexStr到内存空间 std::wstring wstrHexStrPt = wstrSource; wstrHexStrPt = TrimLeft(wstrHexStrPt); int nIndex = 0; //长度去掉正负号,并设置字符数的标识 if (wstrHexStrPt[nIndex] == '+') ++nIndex; else if(wstrHexStrPt[nIndex] == '-') { iFlag = -1; ++nIndex; } //长度去掉"0x"开头的十六进制标识符 if (wstrHexStrPt[nIndex] == '0' && wstrHexStrPt[nIndex + 1] == 'x') nIndex += 2; //循环将每个十六进制的字符转换成对应的十进制整数,出现非法字符则直接返回 while (nIndex < wstrSource.length()) { if ((wstrHexStrPt[nIndex] >= 48) && (wstrHexStrPt[nIndex] <= 57)) wstrHexStrPt[nIndex] -= 48; else if ((wstrHexStrPt[nIndex] >= 65) && (wstrHexStrPt[nIndex] <= 70)) wstrHexStrPt[nIndex] -= 65 - 10; else if ((wstrHexStrPt[nIndex] >= 97) && (wstrHexStrPt[nIndex] <= 102)) wstrHexStrPt[nIndex] -= 97 - 10; else return false; //iResult = *p + iResult*16; 经过 @大致 的提醒,使用移位运算 iResult = wstrHexStrPt[nIndex] + (iResult << 4); ++nIndex; } //返回转换后的整数 nResult = iFlag * iResult; return true; } //std::wstring CStringOpt::Replace(const std::wstring& wstrContent, const std::wstring& strReplace, const std::wstring & strDest) /*{ std::wstring strContent = wstrContent; while (true) { size_t pos = strContent.find(strReplace); if (pos != std::wstring::npos) { WCHAR pBuf[1] = { L'\0' }; strContent.replace(pos, strReplace.length(), pBuf, 0); strContent.insert(pos, strDest); } else { break; } } return strContent; }*/ std::string CStringOpt::Transformwstring2string(const std::wstring& wstr, unsigned int nCodePage) { if (wstr.empty()) return ""; int nLen = WideCharToMultiByte(nCodePage, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL); int nBufferLen = static_cast(nLen + 1); char* pDes = new(std::nothrow) char[nBufferLen]; if (pDes == NULL) return ""; memset(pDes, 0, nBufferLen); WideCharToMultiByte(nCodePage, 0, wstr.c_str(), (int)wstr.length(), pDes, nLen, NULL, NULL); std::string strDes = pDes; SAFE_DELETE_ARRAY(pDes); return strDes; } std::wstring CStringOpt::Transformstring2wstring(const std::string& str, unsigned int nCodePage) { if (str.empty()) return L""; std::wstring val = L""; int nLen = MultiByteToWideChar(nCodePage, 0, str.c_str(), -1, NULL, 0); int nBufferLen = static_cast(nLen + 1); wchar_t* pDes = new(std::nothrow) wchar_t[nBufferLen]; if (pDes == NULL) return L""; memset(pDes, 0, sizeof(wchar_t)*(nBufferLen)); MultiByteToWideChar(nCodePage, 0, str.c_str(), -1, pDes, nLen); std::wstring strDes = pDes; SAFE_DELETE_ARRAY(pDes); return strDes; } std::wstring CStringOpt::StringFormat(const wchar_t *wszformat, ...) { int nCount = 0; va_list argptr; va_start(argptr, wszformat); nCount = _vsnwprintf(NULL, 0, wszformat, argptr); va_end(argptr); int nBufferLen = static_cast(nCount + 1); wchar_t* pwszBuf = new(std::nothrow) wchar_t[nBufferLen]; if (pwszBuf == NULL) return L""; memset(pwszBuf, 0, nBufferLen*sizeof(wchar_t)); va_start(argptr, wszformat); _vsnwprintf_s(pwszBuf, nBufferLen, nCount, wszformat, argptr); va_end(argptr); std::wstring wstr = pwszBuf; SAFE_DELETE_ARRAY(pwszBuf); return wstr; } std::string CStringOpt::TrimLeft(const std::string & strSource, const std::string & strTrim) { std::string str = strSource; return str.erase(0, strSource.find_first_not_of(strTrim)); } std::string CStringOpt::TrimRight(const std::string & strSource, const std::string & strTrim) { std::string str = strSource; return str.erase(str.find_last_not_of(strTrim) + 1); } std::string CStringOpt::Trim(const std::string & strSource, const std::string & strTrim) { std::string str = strSource; return TrimLeft(TrimRight(str, strTrim), strTrim); } std::wstring CStringOpt::TrimLeft(const std::wstring & strSource, const std::wstring & strTrim) { std::wstring str = strSource; return str.erase(0, strSource.find_first_not_of(strTrim)); } std::wstring CStringOpt::TrimRight(const std::wstring & strSource, const std::wstring & strTrim) { std::wstring str = strSource; return str.erase(str.find_last_not_of(strTrim) + 1); } std::wstring CStringOpt::Trim(const std::wstring & strSource, const std::wstring & strTrim) { std::wstring str = strSource; return TrimLeft(TrimRight(str, strTrim), strTrim); } std::string CStringOpt::Replace(const std::string& org, const std::string &keystr, const std::string &replacestr) { std::string strOrg = org; std::string::size_type pos = 0; std::string::size_type keylen = keystr.size(); std::string::size_type replen = replacestr.size(); while ((pos = strOrg.find(keystr, pos)) != std::string::npos) { strOrg.replace(pos, keylen, replacestr); pos += replen; } return strOrg; } std::wstring CStringOpt::Replace(const std::wstring& org, const std::wstring &keystr, const std::wstring &replacestr) { std::wstring strOrg = org; std::wstring::size_type pos = 0; std::wstring::size_type keylen = keystr.size(); std::wstring::size_type replen = replacestr.size(); while ((pos = strOrg.find(keystr, pos)) != std::wstring::npos) { strOrg.replace(pos, keylen, replacestr); pos += replen; } return strOrg; } std::string CStringOpt::MakeUpper(const std::string & strSource) { std::string strDes = strSource; std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::toupper); return strDes; } std::wstring CStringOpt::MakeUpper(const std::wstring & strSource) { std::wstring strDes = strSource; std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::toupper); return strDes; } std::string CStringOpt::MakeLower(const std::string & strSource) { std::string strDes = strSource; std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::tolower); return strDes; } std::wstring CStringOpt::MakeLower(const std::wstring & strSource) { std::wstring strDes = strSource; std::transform(strSource.begin(), strSource.end(), strDes.begin(), ::tolower); return strDes; } static int HexTable[103] = { 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,1,2,3,4,5, 6,7,8,9,0,0,0,0,0, 0,0,10,11,12,13,14,15,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11, 12,13,14,15 }; bool CStringOpt::Hex2Decimal(const std::string & hex_str, int& nDecimal) { nDecimal = 0; for (size_t i = 0; i < hex_str.length(); i++) { int nValue = hex_str[i]; if (nValue >= 0 && nValue < 103) nDecimal = (nDecimal << 4) | HexTable[nValue]; else return false; } return true; } bool CStringOpt::Hex2Decimal(const std::wstring & hex_str, int& nDecimal) { nDecimal = 0; for (size_t i = 0; i < hex_str.length(); i++) { int nValue = hex_str[i]; if (nValue >= 0 && nValue < 103) nDecimal = (nDecimal << 4) | HexTable[nValue]; else return false; } return true; } std::wstring CStringOpt::String(unsigned char Value) { return StringFormat(L"%d", Value); } std::wstring CStringOpt::String(char Value) { return StringFormat(L"%c", Value); } std::wstring CStringOpt::String(unsigned short Value) { return StringFormat(L"%u", Value); } std::wstring CStringOpt::String(short Value) { return StringFormat(L"%d", Value); } std::wstring CStringOpt::String(int nValue) { return StringFormat(L"%d", nValue); } std::wstring CStringOpt::String(unsigned int uValue) { return StringFormat(L"%u", uValue); } std::wstring CStringOpt::String(float fValue) { return StringFormat(L"%f", fValue); } std::wstring CStringOpt::String(double dfValue) { return StringFormat(L"%lf", dfValue); } std::wstring CStringOpt::String(long long llValue) { return StringFormat(L"%I64d", llValue); } std::wstring CStringOpt::String(unsigned long long ullValue) { return StringFormat(L"%I64u", ullValue); } //bool CStringOpt::IsDigital(const std::wstring& wstr) //{ // for each (auto var in wstr) // { // if (!isdigit(var)) // return false; // } // return true; //} }