|
|
@@ -14,17 +14,29 @@ using namespace boost::archive::iterators;
|
|
|
|
|
|
unsigned char ToHex(unsigned char x)
|
|
|
{
|
|
|
- return x > 9 ? x + 55 : x + 48;
|
|
|
+ return x > 9 ? x + 55 : x + 48;
|
|
|
}
|
|
|
|
|
|
unsigned char FromHex(unsigned char x)
|
|
|
{
|
|
|
- unsigned char y;
|
|
|
- if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
|
|
|
- else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
|
|
|
- else if (x >= '0' && x <= '9') y = x - '0';
|
|
|
- else assert(0);
|
|
|
- return y;
|
|
|
+ unsigned char y;
|
|
|
+ if(x >= 'A' && x <= 'Z')
|
|
|
+ {
|
|
|
+ y = x - 'A' + 10;
|
|
|
+ }
|
|
|
+ else if(x >= 'a' && x <= 'z')
|
|
|
+ {
|
|
|
+ y = x - 'a' + 10;
|
|
|
+ }
|
|
|
+ else if(x >= '0' && x <= '9')
|
|
|
+ {
|
|
|
+ y = x - '0';
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ assert(0);
|
|
|
+ }
|
|
|
+ return y;
|
|
|
}
|
|
|
|
|
|
CLewaimaiString::CLewaimaiString()
|
|
|
@@ -38,85 +50,254 @@ CLewaimaiString::~CLewaimaiString()
|
|
|
|
|
|
bool CLewaimaiString::base64_encode(const string& input, string* output)
|
|
|
{
|
|
|
- typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
|
|
|
- stringstream result;
|
|
|
- try {
|
|
|
- copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
|
|
|
- }
|
|
|
- catch (...) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- size_t equal_count = (3 - input.length() % 3) % 3;
|
|
|
- for (size_t i = 0; i < equal_count; i++)
|
|
|
- {
|
|
|
- result.put('=');
|
|
|
- }
|
|
|
- *output = result.str();
|
|
|
- return output->empty() == false;
|
|
|
+ typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
|
|
|
+ stringstream result;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
|
|
|
+ }
|
|
|
+ catch(...)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ size_t equal_count = (3 - input.length() % 3) % 3;
|
|
|
+ for(size_t i = 0; i < equal_count; i++)
|
|
|
+ {
|
|
|
+ result.put('=');
|
|
|
+ }
|
|
|
+ *output = result.str();
|
|
|
+ return output->empty() == false;
|
|
|
}
|
|
|
|
|
|
bool CLewaimaiString::base64_decode(const string& input, string* output)
|
|
|
{
|
|
|
- typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
|
|
|
- stringstream result;
|
|
|
- try {
|
|
|
- copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
|
|
|
- }
|
|
|
- catch (...) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- *output = result.str();
|
|
|
- return output->empty() == false;
|
|
|
+ typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
|
|
|
+ stringstream result;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
|
|
|
+ }
|
|
|
+ catch(...)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ *output = result.str();
|
|
|
+ return output->empty() == false;
|
|
|
}
|
|
|
|
|
|
-void CLewaimaiString::trim(string &s)
|
|
|
+void CLewaimaiString::trim(string& s)
|
|
|
{
|
|
|
- if (!s.empty())
|
|
|
- {
|
|
|
- s.erase(0, s.find_first_not_of(" "));
|
|
|
- s.erase(s.find_last_not_of(" ") + 1);
|
|
|
- }
|
|
|
+ if(!s.empty())
|
|
|
+ {
|
|
|
+ s.erase(0, s.find_first_not_of(" "));
|
|
|
+ s.erase(s.find_last_not_of(" ") + 1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
std::string CLewaimaiString::UrlEncode(const std::string& str)
|
|
|
{
|
|
|
- std::string strTemp = "";
|
|
|
- size_t length = str.length();
|
|
|
- for (size_t i = 0; i < length; i++)
|
|
|
- {
|
|
|
- if (isalnum((unsigned char)str[i]) ||
|
|
|
- (str[i] == '-') ||
|
|
|
- (str[i] == '_') ||
|
|
|
- (str[i] == '.') ||
|
|
|
- (str[i] == '~'))
|
|
|
- strTemp += str[i];
|
|
|
- else if (str[i] == ' ')
|
|
|
- strTemp += "+";
|
|
|
- else
|
|
|
- {
|
|
|
- strTemp += '%';
|
|
|
- strTemp += ToHex((unsigned char)str[i] >> 4);
|
|
|
- strTemp += ToHex((unsigned char)str[i] % 16);
|
|
|
- }
|
|
|
- }
|
|
|
- return strTemp;
|
|
|
+ std::string strTemp = "";
|
|
|
+ size_t length = str.length();
|
|
|
+ for(size_t i = 0; i < length; i++)
|
|
|
+ {
|
|
|
+ if(isalnum((unsigned char)str[i]) ||
|
|
|
+ (str[i] == '-') ||
|
|
|
+ (str[i] == '_') ||
|
|
|
+ (str[i] == '.') ||
|
|
|
+ (str[i] == '~'))
|
|
|
+ {
|
|
|
+ strTemp += str[i];
|
|
|
+ }
|
|
|
+ else if(str[i] == ' ')
|
|
|
+ {
|
|
|
+ strTemp += "+";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ strTemp += '%';
|
|
|
+ strTemp += ToHex((unsigned char)str[i] >> 4);
|
|
|
+ strTemp += ToHex((unsigned char)str[i] % 16);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strTemp;
|
|
|
}
|
|
|
|
|
|
std::string CLewaimaiString::UrlDecode(const std::string& str)
|
|
|
{
|
|
|
- std::string strTemp = "";
|
|
|
- size_t length = str.length();
|
|
|
- for (size_t i = 0; i < length; i++)
|
|
|
- {
|
|
|
- if (str[i] == '+') strTemp += ' ';
|
|
|
- else if (str[i] == '%')
|
|
|
- {
|
|
|
- assert(i + 2 < length);
|
|
|
- unsigned char high = FromHex((unsigned char)str[++i]);
|
|
|
- unsigned char low = FromHex((unsigned char)str[++i]);
|
|
|
- strTemp += high * 16 + low;
|
|
|
- }
|
|
|
- else strTemp += str[i];
|
|
|
- }
|
|
|
- return strTemp;
|
|
|
+ std::string strTemp = "";
|
|
|
+ size_t length = str.length();
|
|
|
+ for(size_t i = 0; i < length; i++)
|
|
|
+ {
|
|
|
+ if(str[i] == '+')
|
|
|
+ {
|
|
|
+ strTemp += ' ';
|
|
|
+ }
|
|
|
+ else if(str[i] == '%')
|
|
|
+ {
|
|
|
+ assert(i + 2 < length);
|
|
|
+ unsigned char high = FromHex((unsigned char)str[++i]);
|
|
|
+ unsigned char low = FromHex((unsigned char)str[++i]);
|
|
|
+ strTemp += high * 16 + low;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ strTemp += str[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strTemp;
|
|
|
}
|
|
|
+
|
|
|
+std::string CLewaimaiString::UnicodeToUTF8(const std::wstring& wstr)
|
|
|
+{
|
|
|
+ std::string ret;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
|
+ ret = wcv.to_bytes(wstr);
|
|
|
+ }
|
|
|
+ catch(const std::exception& e)
|
|
|
+ {
|
|
|
+ std::cerr << e.what() << std::endl;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+std::wstring CLewaimaiString::UTF8ToUnicode(const std::string& str)
|
|
|
+{
|
|
|
+ std::wstring ret;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
|
+ ret = wcv.from_bytes(str);
|
|
|
+ }
|
|
|
+ catch(const std::exception& e)
|
|
|
+ {
|
|
|
+ std::cerr << e.what() << std::endl;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+std::string CLewaimaiString::UnicodeToANSI(const std::wstring& wstr)
|
|
|
+{
|
|
|
+ std::string ret;
|
|
|
+ std::mbstate_t state = {};
|
|
|
+ const wchar_t* src = wstr.data();
|
|
|
+ size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
|
|
|
+ if(static_cast<size_t>(-1) != len)
|
|
|
+ {
|
|
|
+ std::unique_ptr< char[] > buff(new char[len + 1]);
|
|
|
+ len = std::wcsrtombs(buff.get(), &src, len, &state);
|
|
|
+ if(static_cast<size_t>(-1) != len)
|
|
|
+ {
|
|
|
+ ret.assign(buff.get(), len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+std::wstring CLewaimaiString::ANSIToUnicode(const std::string& str)
|
|
|
+{
|
|
|
+ std::wstring ret;
|
|
|
+ std::mbstate_t state = {};
|
|
|
+ const char* src = str.data();
|
|
|
+ size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
|
|
|
+ if(static_cast<size_t>(-1) != len)
|
|
|
+ {
|
|
|
+ std::unique_ptr< wchar_t[] > buff(new wchar_t[len + 1]);
|
|
|
+ len = std::mbsrtowcs(buff.get(), &src, len, &state);
|
|
|
+ if(static_cast<size_t>(-1) != len)
|
|
|
+ {
|
|
|
+ ret.assign(buff.get(), len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+std::string CLewaimaiString::UTF8ToANSI(const std::string& str)
|
|
|
+{
|
|
|
+ return UnicodeToANSI(UTF8ToUnicode(str));
|
|
|
+}
|
|
|
+
|
|
|
+std::string CLewaimaiString::ANSIToUTF8(const std::string& str)
|
|
|
+{
|
|
|
+ return UnicodeToUTF8(ANSIToUnicode(str));
|
|
|
+}
|
|
|
+
|
|
|
+std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint)
|
|
|
+{
|
|
|
+ char str[256];
|
|
|
+
|
|
|
+ std::string pre = "%." + to_string(precisionAfterPoint) + "f";
|
|
|
+ sprintf(str, pre.c_str(), value);
|
|
|
+ string result = str;
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+bool CLewaimaiString::isIPAddressValid(const char* pszIPAddr)
|
|
|
+{
|
|
|
+ if(!pszIPAddr)
|
|
|
+ {
|
|
|
+ return false; //若pszIPAddr为空
|
|
|
+ }
|
|
|
+ char IP1[100], cIP[4];
|
|
|
+ int len = strlen(pszIPAddr);
|
|
|
+ int i = 0, j = len - 1;
|
|
|
+ int k, m = 0, n = 0, num = 0;
|
|
|
+ //去除首尾空格(取出从i-1到j+1之间的字符):
|
|
|
+ while(pszIPAddr[i++] == ' ');
|
|
|
+ while(pszIPAddr[j--] == ' ');
|
|
|
+
|
|
|
+ for(k = i - 1; k <= j + 1; k++)
|
|
|
+ {
|
|
|
+ IP1[m++] = *(pszIPAddr + k);
|
|
|
+ }
|
|
|
+ IP1[m] = '\0';
|
|
|
+
|
|
|
+ char* p = IP1;
|
|
|
+
|
|
|
+ while(*p != '\0')
|
|
|
+ {
|
|
|
+ if(*p == ' ' || *p < '0' || *p > '9')
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头
|
|
|
+
|
|
|
+ int sum = 0; //sum为每一子段的数值,应在0到255之间
|
|
|
+ while(*p != '.' && *p != '\0')
|
|
|
+ {
|
|
|
+ if(*p == ' ' || *p < '0' || *p > '9')
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ sum = sum * 10 + *p - 48; //每一子段字符串转化为整数
|
|
|
+ p++;
|
|
|
+ }
|
|
|
+ if(*p == '.')
|
|
|
+ {
|
|
|
+ if((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9')) //判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.”
|
|
|
+ {
|
|
|
+ num++; //记录“.”出现的次数,不能大于3
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3)
|
|
|
+ {
|
|
|
+ return false; //若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP
|
|
|
+ }
|
|
|
+
|
|
|
+ if(*p != '\0')
|
|
|
+ {
|
|
|
+ p++;
|
|
|
+ }
|
|
|
+ n = 0;
|
|
|
+ }
|
|
|
+ if(num != 3)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|