| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- #include "../pch/pch.h"
- #include "CLewaimaiString.h"
- #include <boost/archive/iterators/base64_from_binary.hpp>
- #include <boost/archive/iterators/binary_from_base64.hpp>
- #include <boost/archive/iterators/transform_width.hpp>
- #include <string>
- #include <iostream>
- #include <sstream>
- using namespace std;
- using namespace boost::archive::iterators;
- unsigned char ToHex(unsigned char x)
- {
- 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;
- }
- CLewaimaiString::CLewaimaiString()
- {
- }
- 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;
- }
- 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;
- }
- void CLewaimaiString::trim(string& s)
- {
- 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 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 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::UnicodeToUTF8(const std::wstring wstr)
- {
- char* pElementText;
- int iTextLen;
- // wide char to multi char
- iTextLen = WideCharToMultiByte(CP_UTF8,
- 0,
- wstr.c_str(),
- -1,
- NULL,
- 0,
- NULL,
- NULL);
- pElementText = new char[iTextLen + 1];
- memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
- ::WideCharToMultiByte(CP_UTF8,
- 0,
- wstr.c_str(),
- -1,
- pElementText,
- iTextLen,
- NULL,
- NULL);
- string strText;
- strText = pElementText;
- delete[] pElementText;
- return strText;
- }
- std::wstring CLewaimaiString::UTF8ToUnicode(const std::string str)
- {
- int len = 0;
- len = str.length();
- int unicodeLen = ::MultiByteToWideChar(CP_UTF8,
- 0,
- str.c_str(),
- -1,
- NULL,
- 0);
- wchar_t* pUnicode;
- pUnicode = new wchar_t[unicodeLen + 1];
- memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
- ::MultiByteToWideChar(CP_UTF8,
- 0,
- str.c_str(),
- -1,
- (LPWSTR)pUnicode,
- unicodeLen);
- wstring rt;
- rt = (wchar_t*)pUnicode;
- delete pUnicode;
- return rt;
- }
- std::string CLewaimaiString::UnicodeToANSI(const std::wstring wstr)
- {
- unsigned len = wstr.size() * 4;
- if(len == 0)
- {
- return "";
- }
- setlocale(LC_CTYPE, "");
- char* p = new char[len];
- wcstombs(p, wstr.c_str(), len);
- std::string str1(p);
- delete[] p;
- return str1;
- }
- std::wstring CLewaimaiString::ANSIToUnicode(const std::string str)
- {
- unsigned len = str.size() * 2;// 预留字节数
- if(len == 0)
- {
- return L"";
- }
- setlocale(LC_CTYPE, ""); //必须调用此函数
- wchar_t* p = new wchar_t[len];// 申请一段内存存放转换后的字符串
- mbstowcs(p, str.c_str(), len);// 转换
- std::wstring str1(p);
- delete[] p;// 释放申请的内存
- return str1;
- }
- std::string CLewaimaiString::UTF8ToANSI(const std::string str)
- {
- std::wstring wsUnicode = CLewaimaiString::UTF8ToUnicode(str);
- std::string sAnsi = CLewaimaiString::UnicodeToANSI(wsUnicode);
- return sAnsi;
- }
- std::string CLewaimaiString::ANSIToUTF8(const std::string str)
- {
- std::wstring wsUnicode = CLewaimaiString::ANSIToUnicode(str);
- std::string sUtf8 = CLewaimaiString::UnicodeToUTF8(wsUnicode);
- return sUtf8;
- }
- std::string CLewaimaiString::UnicodeToGB2312(const wstring& wstr)
- {
- std::string result;
- int n = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
- result.resize(n);
- ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0);
- return result;
- }
- std::wstring CLewaimaiString::GB2312ToUnicode(const string& src)
- {
- std::wstring result;
- int n = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, NULL, 0);
- result.resize(n);
- ::MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
- return result;
- }
- 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;
- }
- vector<string> CLewaimaiString::Split(const string& in, const string& delim)
- {
- vector<string> ret;
- if (in == "")
- {
- //空字符串,直接返回空vector
- return ret;
- }
- try
- {
- regex re{ delim };
- return vector<string>
- {
- sregex_token_iterator(in.begin(), in.end(), re, -1),
- sregex_token_iterator()
- };
- }
- catch(const std::exception& e)
- {
- cout << "error:" << e.what() << std::endl;
- }
- return ret;
- }
- string CLewaimaiString::Merge(const vector<string>& in, const string& delim)
- {
- std::string ret = "";
- for (size_t i = 0; i < in.size(); i++)
- {
- ret += in[i];
- if (i < in.size() - 1)
- {
- ret += delim;
- }
- }
- return ret;
- }
- /*
- *nNum是表示替换几个,默认为1表示替换第1个
- **/
- int CLewaimaiString::Replace(std::wstring& strContent, std::wstring strReplace, std::wstring strDest, int nNum)
- {
- int nCount = 0;
- 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);
- nCount++;
- if(nCount == nNum)
- {
- break;
- }
- }
- else
- {
- break;
- }
- }
- return 0;
- }
- //整个替换所有的字符串,把strBig钟的strsrc全部替换成strdst
- void CLewaimaiString::string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
- {
- std::string::size_type pos = 0;
- std::string::size_type srclen = strsrc.size();
- std::string::size_type dstlen = strdst.size();
- while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
- {
- strBig.replace(pos, srclen, strdst);
- pos += dstlen;
- }
- }
- //从文件路径或者url中获取文件名
- std::string CLewaimaiString::GetPathOrURLShortName(std::string strFullName)
- {
- if (strFullName.empty())
- {
- return "";
- }
- string_replace(strFullName, "/", "\\");
- std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
- return strFullName.substr(iPos, strFullName.length() - iPos);
- }
- //得到文件路径的目录
- std::string CLewaimaiString::GetPathDir(std::string filePath)
- {
- std::string dirPath = filePath;
- size_t p = filePath.find_last_of(L'\\');
- if (p != -1)
- {
- dirPath.erase(p);
- }
- return dirPath;
- }
- std::string CLewaimaiString::BuZifuLeft(std::string old, int num, const char zifu)
- {
- return std::string(num, zifu) + old;
- }
- std::string CLewaimaiString::GetQuweima(std::wstring input)
- {
- //先转换成GB2312,区位码都是相对于GB2312的
- std::string gb_hanzi = CLewaimaiString::UnicodeToGB2312(input);
- std::string result = "";
- for (size_t i = 0; i < gb_hanzi.length() - 1; )
- {
- byte* cur = (byte*)(gb_hanzi.c_str() + i);
- if (*cur >= 0 && *cur <= 127)
- {
- //正常的asicc码,字符串或者数字
- int front = (short)(*cur - '\0');
- front -= 32;
- if (front < 10)
- {
- result += "030" + to_string(front);
- }
- else
- {
- result += "03" + to_string(front);
- }
- i++;
- }
- else
- {
- int front = (short)(cur[0] - '\0');//将字节数组的第一位转换成short类型,这里(short)code[0]也是可以的
- int back = (short)(cur[1] - '\0');//将字节数组的第二位转换成short类型
- std::string s_front = to_string(front - 160);
- if (front - 160 < 10)
- {
- //前面补0
- s_front = "0" + s_front;
- }
- std::string s_back = to_string(back - 160);
- if (back - 160 < 10)
- {
- //前面补0
- s_back = "0" + s_back;
- }
- result += s_front + s_back;
- i++;
- i++;
- }
- }
- return result;
- }
- std::string CLewaimaiString::SqlZhuanyi(std::string old)
- {
- CLewaimaiString::string_replace(old, "'", "''");
- return old;
- }
- std::wstring CLewaimaiString::ShuziInputFormat(std::wstring old, std::wstring input)
- {
- if (old == L"0")
- {
- if (input == L".")
- {
- return old + input;
- }
- else if(input == L"0")
- {
- return L"0";
- }
- else
- {
- return input;
- }
- }
- int pos = old.find(L".");
- if (pos != old.npos)
- {
- //已经有一个.了
- if (input == L".")
- {
- return old;
- }
- if (old.length() - pos > 2)
- {
- //小数点后已经有2个数字了
- return old;
- }
- return old + input;
- }
- else
- {
- //还没有小数点
- return old + input;
- }
- }
- bool CLewaimaiString::is_only_number(string str)
- {
- for (int i = 0; i < str.size(); i++)
- {
- if ((str.at(i) > '9') || (str.at(i) < '0'))
- {
- return false;
- }
- }
- return true;
- }
|