123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227 |
- #include "pch.h"
- #include "BaseUtility.h"
- #include <odbcinst.h>
- #include <comdef.h>
- #include <afxdb.h>
- #include <regex>
- #include <iostream>
- #include <fstream>
- #include "Base64.h"
- typedef basic_string<char>::size_type S_T;
- bool IsLeap(int year)
- {
- if((year%4==0&&year%100!=0)||year%400==0)
- return true;
- else
- return false;
- }
- bool IsWeekEnd(int year, int month, int day)
- {
- struct tm stTime = {0};
- struct tm *ptr = NULL;
- time_t time;
- stTime.tm_year = year - 1900;
- stTime.tm_mon = month - 1;
- stTime.tm_mday = day;
- time = mktime(&stTime);
- ptr = localtime(&time);
- return (ptr->tm_wday == 0 || ptr->tm_wday == 6);
- }
- bool Nextday(int& year, int& month, int& day)
- {
- short ld[12]={31,28,31,30,31,30,31,31,30,31,30,31};
- if (!(!(year%100)&&(year%400)) && !(year%4))
- ld[1]++;
- day++;
- if (day<=0 || day>(unsigned long)ld[month-1])
- {
- day = 1; month++;
- if(month>12)
- {
- month = 1; year++;
- }
- }
- return true;
- }
- void Full2Half(wchar_t* str)
- {
- if(str != NULL)
- {
- int len = wcslen(str);
- int i = 0;
- wchar_t space[] = L" ";
- for(; i<len; i++)
- {
- if(str[i] == space[0])//对空格特殊处理
- {
- str[i] = ' ';
- }
- else if(str[i] >= 65281 && str[i] <= 65374)
- {
- str[i] -= (65281 - 33);
- }
- }
- }
- }
- bool split(const tstring& src, tstring delimit,vector<tstring> &v, tstring null_subst)
- {
- if(src.empty() || delimit.empty())
- return false;
- S_T deli_len = delimit.size();
- long index = -1,last_search_position = 0;
- while( (index=src.find(delimit,last_search_position))!=-1 )
- {
- if(index==last_search_position)
- v.push_back(null_subst);
- else
- v.push_back( src.substr(last_search_position, index-last_search_position) );
- last_search_position = index + deli_len;
- }
- tstring last_one = src.substr(last_search_position);
- v.push_back( last_one.empty()? null_subst:last_one );
- return true;
- }
- bool split(const string& src, string delimit, vector<string> &v, string null_subst)
- {
- if (src.empty() || delimit.empty())
- return false;
- S_T deli_len = delimit.size();
- long index = -1, last_search_position = 0;
- while ((index = src.find(delimit, last_search_position)) != -1)
- {
- if (index == last_search_position)
- v.push_back(null_subst);
- else
- v.push_back(src.substr(last_search_position, index - last_search_position));
- last_search_position = index + deli_len;
- }
- string last_one = src.substr(last_search_position);
- v.push_back(last_one.empty() ? null_subst : last_one);
- return true;
- }
- vector<tstring> splitEx(const tstring& src, tstring separate_character)
- {
- vector<tstring> strs;
- int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
- int lastPosition = 0, index = -1;
- while (-1 != (index = src.find(separate_character, lastPosition)))
- {
- strs.push_back(src.substr(lastPosition, index - lastPosition));
- lastPosition = index + separate_characterLen;
- }
- tstring lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
- if (!lastString.empty())
- strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
- return strs;
- }
- vector<string> splitEx(const string& src, string separate_character)
- {
- vector<string> strs;
- int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
- int lastPosition = 0, index = -1;
- while (-1 != (index = src.find(separate_character, lastPosition)))
- {
- strs.push_back(src.substr(lastPosition, index - lastPosition));
- lastPosition = index + separate_characterLen;
- }
- string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
- if (!lastString.empty())
- strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
- return strs;
- }
- int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len)
- {
- int i = 0, j = 0;
- if (0 == src_len)
- {
- src_len = strlen(src);
- }
- for (i = 0; i < src_len;)
- {
- if (j >= buf_len - 1)
- break;
- if (src[i] >= 0)
- {
- buf[j++] = src[i++];
- }
- else
- {
- unsigned short w_c = 0;
- char tmp[4] = "";
- Gb2312_2_Unicode(&w_c, src + i);
- Unicode_2_UTF8(tmp, &w_c);
- buf[j+0] = tmp[0];
- buf[j+1] = tmp[1];
- buf[j+2] = tmp[2];
- i += 2;
- j += 3;
- }
- }
- buf[j] = '\0';
- return j;
- }
- void Gb2312_2_Unicode(unsigned short* dst, const char* src)
- {
- MultiByteToWideChar(936, MB_PRECOMPOSED, src, 2, (LPWSTR)dst, 1);
- }
- void Unicode_2_UTF8(char* dst, unsigned short* src)
- {
- char *pchar = (char *)src;
- dst[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
- dst[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
- dst[2] = (0x80 | ( pchar[0] & 0x3F));
- }
- string UTF8ToTstring(const char *pData, size_t size)
- {
- size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0);
- WCHAR *pChar = new WCHAR[n+1];
- n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n);
- pChar[n]=0;
- n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
- char *p = new char[n+1];
- n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
- string result(p);
- delete []pChar;
- delete []p;
- return result;
- }
- string ConvertUTF8toGB2312(const char* pData, size_t size)
- {
- size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0);
- WCHAR* pChar = new WCHAR[n + 1];
- n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n);
- pChar[n] = 0;
- n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
- char* p = new char[n + 1];
- n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
- string result(p);
- delete[]pChar;
- delete[]p;
- return result;
- }
- string ConvertGB2312toUTF8(const char *pData)
- {
- int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0);
- wchar_t* wstr = new wchar_t[len+1];
- memset(wstr, 0, len+1);
- MultiByteToWideChar(936, 0, pData, -1, wstr, len);
- len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
- char* str = new char[len+1];
- memset(str, 0, len+1);
- WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
- if(wstr) delete[] wstr;
- string strUtf8(str);
- if(str) delete[] str;
- return strUtf8;
- }
- wstring GB2312ToUnicode(const char *pData)
- {
- wstring str;
- int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); // 先取得转换后的UNICODE字符串所需的长度
- if (len <= 0) return str;
- wchar_t* pWstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t)); // 分配缓冲区
- MultiByteToWideChar(936, 0, pData, -1, pWstr, len); // 开始转换
- str = pWstr;
- if (pWstr) free(pWstr);
- return str;
- }
- string UnicodeToGB2312(TCHAR *pData)
- {
- string str;
- int len = WideCharToMultiByte(936, 0, pData, -1, NULL, 0, NULL, NULL); // 先取得转换后的ANSI字符串所需的长度
- if (len <= 0) return str;
- char* pStr = (char*)calloc(len + 1, sizeof(char)); // 分配缓冲区
- WideCharToMultiByte(936, 0, pData, -1, pStr, len, NULL, NULL); // 开始转换
- str = pStr;
- if (pStr) free(pStr);
- return str;
- }
- void UTF8toANSI(string &strUTF8)
- {
- //获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区
- UINT nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, NULL, NULL);
- WCHAR *wszBuffer = new WCHAR[nLen + 1];
- nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, wszBuffer, nLen);
- wszBuffer[nLen] = 0;
- nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);
- char *szBuffer = new char[nLen + 1];
- nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);
- szBuffer[nLen] = 0;
- strUTF8 = szBuffer;
- //清理内存
- delete[]szBuffer;
- delete[]wszBuffer;
- }
- string UnicodeToGB2312(const tstring& strSrc)
- {
- return UnicodeToGB2312((LPTSTR)strSrc.c_str());
- }
- string UnicodeToGB2312(const CString& strSrc)
- {
- tstring wstr = strSrc;
- return UnicodeToGB2312((LPTSTR)wstr.c_str());
- }
- wstring UTF8ToUnicode(const char* szU8)
- {
- wstring str;
- if (szU8 == NULL)
- {
- return str;
- }
- //预转换,得到所需空间的大小;
- int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
- if (wcsLen <= 0) return str;
- //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
- wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t));
- //转换
- ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen);
- pWStr[wcsLen] = L'\0';
- str = pWStr;
- if (pWStr) free(pWStr);
- return str;
- }
- string UnicodeToUtf8(const wchar_t* unicode)
- {
- string str;
- int len;
- len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
- if (len <= 0) return str;
- char *szUtf8 = (char*)calloc(len + 1, sizeof(char));
- WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
- szUtf8[len] = '\0';
- str = szUtf8;
- if (szUtf8) free(szUtf8);
- return str;
- }
- tstring GB2312ToTstring(string str)
- {
- tstring tstr=_T("");
- #ifdef _UNICODE
- tstr=GB2312ToUnicode(str.c_str());
- #else
- tstr=str;
- #endif
- return tstr;
- }
- string TstringToGB2312(tstring tstr)
- {
- string str = "";
- #ifdef _UNICODE
- str = UnicodeToGB2312(tstr);
- #else
- str = pData;
- #endif
- return str;
- }
- string TstringToUTF8(tstring tstr)
- {
- string str = "";
- #ifdef _UNICODE
- str = UnicodeToUtf8(tstr.c_str());
- #else
- char* p=ConvertGB2312toUTF8(tstr.c_str());
- str=*p;
- delete[]p;
- #endif
- return str;
- }
- tstring UTF8ToTstring(string str)
- {
- tstring tstr = _T("");
- #ifdef _UNICODE
- tstr = UTF8ToUnicode(str.c_str());
- #else
- tstr = ConvertUTF8toGB2312(str.c_str(), str.length());
- #endif
- return tstr;
- }
- bool TransToPriceFormat(const tstring& strSrc, tstring& strDes, double& dDes)
- {
- vector<tstring> vctPrice;
- if (!split(strSrc, _T("|"), vctPrice))
- return false;
- strDes.clear();
- for (int i = 0; i < vctPrice.size(); i++)
- {
- if (vctPrice[i].length() == 0)
- continue;
- dDes += _tstof(vctPrice[i].c_str());
- strDes.append(vctPrice[i]);
- if (i != vctPrice.size()-1)
- {
- strDes.append(_T("+"));
- }
- }
- return true;
- }
- int CalcWord(const char *ps, const size_t length)
- {
- ASSERT(0 < length);
- ASSERT(NULL != ps);
- int nRealLen(0);
- int nFlag(0);
- while (nFlag <= length && 0 != ps[nRealLen])
- {
- if (IsChinese(ps[nRealLen]))
- {
- nRealLen++;
- if (0 != ps[nRealLen])
- nRealLen++;
- else
- break;
- }
- else
- nRealLen++;
- nFlag++;
- }
- return nRealLen;
- }
- void Int64DateToStr(const __int64& llDate, string& out)
- {
- int nYear = llDate / 10000000000L;
- int nMode = llDate % 10000000000L;
- int nMon = nMode / 100000000;
- nMode %= 100000000;
- int nDay = nMode / 1000000;
- nMode %= 1000000;
- int nHour = nMode / 10000;
- nMode %= 10000;
-
- int nMin = nMode / 100;
- int nSec = nMode % 100;
- char buf[20];
- memset(buf, 0, 20);
- sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", nYear,nMon,nDay,nHour,nMin,nSec);
-
- out = buf;
- }
- void Int64DateToYMDStr(const __int64& llDate, string& out)
- {
- int nYear = llDate / 10000000000L;
- int nMode = llDate % 10000000000L;
- int nMon = nMode / 100000000;
- nMode %= 100000000;
- int nDay = nMode / 1000000;
- nMode %= 1000000;
- char buf[20];
- memset(buf, 0, 20);
- sprintf(buf, "%04d-%02d-%02d", nYear,nMon,nDay);
-
- out = buf;
- }
- void Int64DateToHMStr(const __int64& llDate, string& out)
- {
- int nYear = llDate / 10000000000L;
- int nMode = llDate % 10000000000L;
- int nMon = nMode / 100000000;
- nMode %= 100000000;
- int nDay = nMode / 1000000;
- nMode %= 1000000;
- int nHour = nMode / 10000;
- nMode %= 10000;
-
- int nMin = nMode / 100;
- char buf[20];
- memset(buf, 0, 20);
- sprintf(buf, "%02d:%02d", nHour,nMin);
-
- out = buf;
- }
- 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';
- return y;
- }
- string UrlEncode(const string& str)
- {
- 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;
- }
- string UrlDecode(const string& str)
- {
- string strTemp = "";
- size_t length = str.length();
- for (size_t i = 0; i < length; i++)
- {
- if (str[i] == '+') strTemp += ' ';
- else if (str[i] == '%')
- {
- 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;
- }
- void DeleteDirectory(const CString &strPath)
- {
- if (strPath.IsEmpty())
- return;
- CFileFind ff;
- BOOL bFound = ff.FindFile(strPath + _T("\\*"), 0);
- while(bFound)
- {
- bFound = ff.FindNextFile();
- if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T("..")))
- continue;
- SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
- if (ff.IsDirectory())
- {
- DeleteDirectory(ff.GetFilePath());
- RemoveDirectory(ff.GetFilePath());
- }
- else
- {
- DeleteFile(ff.GetFilePath());
- }
- }
- ff.Close();
- //RemoveDirectory(strPath);
- }
- void toUpper(char *szDestination, const char *szSource)
- {
- if (szDestination == NULL || szSource == NULL || strlen(szSource) == 0)
- return;
- while (*szSource != 0)
- {
- if (*szSource >= 'a' && *szSource <= 'z')
- *szDestination++ = 'A' + (*szSource++ - 'a');
- else
- *szDestination++ = *szSource++;
- }
- *szDestination = 0;
- }
- void toUpper(TCHAR *szDestination, const TCHAR *szSource)
- {
- if (szDestination == NULL || szSource == NULL || _tcslen(szSource) == 0)
- return;
- while (*szSource != 0)
- {
- if (*szSource >= 'a' && *szSource <= 'z')
- *szDestination++ = 'A' + (*szSource++ - 'a');
- else
- *szDestination++ = *szSource++;
- }
- *szDestination = 0;
- }
- void FormatPrice(char *szDes, const double dSource)
- {
- sprintf(szDes, "%.9lf", dSource);
- int i(0);
- int nLen(strlen(szDes));
- for (; i < nLen; i++)
- {
- if (szDes[i] == '.')
- break;
- }
- for (int j = nLen - 1; j > i + 2; j--)
- {
- if (szDes[j] == '0')
- szDes[j] = 0;
- else
- break;
- }
- }
- bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark)
- {
- vector<string> vctVol;
- vector<string> vctRemark;
- bool bRet(true);
- if (!split(szVol, "|", vctVol))
- bRet = false;
- if (!split(szRemark, "|", vctRemark))
- bRet = false;
- if (vctVol.size() != vctRemark.size())
- bRet = false;
- if (!bRet)
- {
- if (!split(szVol, "+", vctVol))
- bRet = false;
- else
- bRet = true;
- if (!split(szRemark, "+", vctRemark))
- bRet = false;
- else
- bRet = true;
- if (vctVol.size() != vctRemark.size())
- bRet = false;
- }
- if (!bRet)
- return bRet;
- for (int i = 0; i < vctVol.size(); i++)
- {
- if (i != 0)
- strDes.append("+");
- if (vctVol[i].empty())
- strDes.append("--");
- else
- strDes.append(vctVol[i]);
- if (!vctRemark[i].empty())
- {
- strDes.append("(");
- strDes.append(vctRemark[i]);
- strDes.append(")");
- }
- }
- return true;
- }
- string replace(const char *szContext, char oldChar, const char *szNew)
- {
- int nLen = strlen(szContext) + 1;
- if (szNew != NULL)
- nLen += strlen(szNew);
- char *pTemp = new char[nLen];
- autoptr_arr<char> arrguard(pTemp);
- memset(pTemp, 0, nLen);
- char *pResult = pTemp;
- while(*szContext != 0)
- {
- if (*szContext == oldChar)
- {
- if (szNew != NULL)
- {
- while (*szNew!= 0)
- {
- *pTemp++ = *szNew++;
- }
- }
- }
- else
- {
- *pTemp++ = *szContext;
- }
- szContext++;
- }
- return string(pResult);
- }
- bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark/* = NULL*/
- , const char *szDanger/* = NULL*/, const char *szOCO/* = NULL*/, const char *szPACK/* = NULL*/)
- {
- if (szVol == NULL)
- return false;
- vector<const char*> vctData;
- vctData.push_back(szVol);
- if (szDanger != NULL)
- {
- vctData.push_back(szDanger);
- }
- if (szOCO != NULL)
- {
- vctData.push_back(szOCO);
- }
- if (szPACK != NULL)
- {
- vctData.push_back(szPACK);
- }
- if (szRemark != NULL && strlen(szRemark) > 0 && strcmp(szRemark, "--") != 0)
- {
- vctData.push_back(szRemark);
- }
- const char* arrSeparators[2] = {"|", "+"};
- bool bMulti(false);
- int nIdx(0);
- for (int i = 0; i < 2; i++)
- {
- if (strstr(szVol, arrSeparators[i]) != NULL)
- {
- nIdx = i;
- bMulti = true;
- break;
- }
- }
- if (bMulti)
- {
- vector<vector<string>> vctSplit;
- for (int i = 0; i < vctData.size(); i++)
- {
- vector<string> vct;
- if (!split(vctData[i], arrSeparators[nIdx], vct))
- {
- return false;
- }
- vctSplit.push_back(vct);
- }
- int nCount(vctSplit[0].size());
- for (int i = 1; i < vctSplit.size(); i++)
- {
- if (nCount != vctSplit[i].size())
- return false;
- }
- for (int i = 0; i < nCount; i++)
- {
- if (i != 0)
- strDes.append("+");
-
- if (strlen((vctSplit[0])[i].c_str()) == 0)
- strDes.append("--");
- else
- {
- strDes.append((vctSplit[0])[i]);
- }
- string strTemp;
- for (int j = 1; j < vctSplit.size(); j++)
- {
- if (strlen((vctSplit[j])[i].c_str()) != 0)
- {
- strTemp.append((vctSplit[j])[i]);
- strTemp.append(",");
- }
- }
- if (!strTemp.empty())
- {
- if (strTemp[strTemp.size() - 1] == ',')
- {
- strTemp = string(strTemp.c_str(), strTemp.size() - 1);
- }
- }
- if (!strTemp.empty())
- {
- strDes.append("(");
- strDes.append(strTemp);
- strDes.append(")");
- }
- }
- }
- else
- {
- if (strlen(szVol) == 0)
- {
- strDes.append("--");
- }
- else
- {
- strDes.append(szVol);
- }
-
- string strTemp;
- for (int i = 1; i < vctData.size(); i++)
- {
- if (strlen(vctData[i]) != 0)
- {
- strTemp.append(vctData[i]);
- strTemp.append(",");
- }
- }
- if (!strTemp.empty())
- {
- if (strTemp[strTemp.size() - 1] == ',')
- {
- strTemp = string(strTemp.c_str(), strTemp.size() - 1);
- }
- }
- if (!strTemp.empty())
- {
- strDes.append("(");
- strDes.append(strTemp);
- strDes.append(")");
- }
- }
- return true;
- }
- __int64 StringToInt64(const TCHAR *szSource)
- {
- __int64 ret(0);
- if (_tcslen(szSource) == 0)
- return ret;
- const TCHAR *pTemp = szSource + _tcslen(szSource) - 1;
- __int64 nRight(1);
- bool bNegative(false);
- if ('-' == szSource[0])
- bNegative = true;
- do
- {
- if (*pTemp >= '0' && *pTemp <= '9')
- {
- ret += nRight * (0 + (*pTemp - '0'));
- nRight *= 10;
- }
- }
- while(pTemp-- != szSource);
- if (bNegative)
- ret = -ret;
- return ret;
- }
- void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src)
- {
- int srcSize = _tcslen(src);
- if (srcSize == 0)
- {
- return;
- }
- if (srcSize > destSize)
- memcpy(dest, src, destSize);
- else
- _tcscpy(dest, src);
- }
- void SafeCopyData(TCHAR dest[], int destSize, tstring str)
- {
- int srcSize = str.length();
- if (srcSize == 0)
- {
- return;
- }
- if (srcSize > destSize)
- memcpy(dest, str.c_str(), destSize);
- else
- _tcscpy(dest, str.c_str());
- }
- tstring GetRequestByKey(const tstring& tstrKey, const tstring& tstrReuest)
- {
- string strKey = UnicodeToGB2312(tstrKey);
- string strReuest = UnicodeToGB2312(tstrReuest);
- try
- {
- smatch result;
- if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*?)&")))
- {
- return GB2312ToUnicode(result[1].str().c_str());
- }
- else if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*)")))
- {
- return GB2312ToUnicode(result[1].str().c_str());
- }
- else
- {
- return tstring();
- }
- }
- catch (...)
- {
- return tstring();
- }
- return tstring();
- }
- void GetKeysByUrl(const tstring & strUrl, vector<tstring> & vctKey)
- {
- vector<tstring> vctSearch;
- split(strUrl, _T("?"), vctSearch);
- if (vctSearch.size() != 2 && vctSearch.size() != 1)
- {
- return;
- }
- tstring strTemp;
- if (vctSearch.size() == 1)
- {
- strTemp = vctSearch[0];
- }
- else if (vctSearch.size() == 2)
- {
- strTemp = vctSearch[1];
- }
- vctSearch.clear();
- split(strTemp, _T("&"), vctSearch);
- if (vctSearch.size() <= 0)
- {
- return;
- }
- for (int i = 0; i < vctSearch.size(); i++)
- {
- strTemp = vctSearch[i];
- vector<tstring> vct;
- split(strTemp, _T("="), vct);
- if (vct.size() == 2)
- {
- vctKey.push_back(vct[0]);
- }
- }
- }
- __int64 GetCharNumber(TCHAR* szText, size_t n)
- {
- __int64 nDest;
- if (n == 0)
- {
- return -1;
- }
- for (nDest = 0; n--; szText++)
- {
- if (*szText < '0' || *szText > '9')
- {
- continue; //继续获取下一个字符
- }
- /*
- 1、当前字符值 *line的ascii码,减去字符0的ascii码,得出个位数字
- 2、原计算的value值 乘以10,向上提升一位
- 3、二者相加得到新的十进制数值
- */
- nDest = nDest * 10 + (*szText - '0');
- }
- if (nDest < 0)
- {
- return -1;
- }
- else
- {
- return nDest;
- }
- }
- tstring ANSIToUnicode(const string& str)
- {
- int len = 0;
- len = str.length();
- int unicodeLen = ::MultiByteToWideChar(936,
- 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(936,
- 0,
- str.c_str(),
- -1,
- (LPWSTR)pUnicode,
- unicodeLen);
- tstring rt;
- rt = (wchar_t*)pUnicode;
- delete pUnicode;
- return rt;
- }
- CString AddNumberSeparate(CString strSrc)
- {
- auto IsNumberFunc = [=](CString str)
- {
- int nCount = 0;
- for (int i = 0; i < str.GetLength(); i++)
- {
- if (i == 0)
- {
- if (str.GetAt(i) == '-')
- {
- continue;
- }
- }
- if (!((str.GetAt(i) >= '0' && str.GetAt(i) <= '9') || str.GetAt(i) == '.'))
- {
- return FALSE;
- }
- if (str.GetAt(i) == '.')
- {
- nCount++;
- }
- }
- if (nCount > 1)
- {
- return FALSE;
- }
- return TRUE;
- };
- CString str(strSrc);
- if (IsNumberFunc(str))
- {
- int nPos = 0;
- nPos = str.Find(_T("."), nPos);
- vector<int> vcteparatePos;
- if (nPos == -1)
- {
- for (int i = str.GetLength() - 3; i > 0; i--)
- {
- if ((str.GetLength() - i) % 3 == 0)
- {
- if (!(i == 1 && str.GetAt(0) == '-'))
- {
- vcteparatePos.push_back(i);
- }
- }
- }
- }
- else
- {
- for (int i = nPos - 3; i > 0; i--)
- {
- if ((nPos - i) % 3 == 0)
- {
- if (!(i == 1 && str.GetAt(0) == '-'))
- {
- vcteparatePos.push_back(i);
- }
- }
- }
- }
- for (int i = 0; i < vcteparatePos.size(); i++)
- {
- str.Insert(vcteparatePos[i], ',');
- }
- }
- return str;
- }
- bool RegexStdMatch(string src, string regular)
- {
- regex pattern(regular.c_str());
- if (!regex_match(src, pattern))
- {
- return false;
- }
- return true;
- }
- bool RegexStdMatch(tstring src, tstring regular)
- {
- char* pre = setlocale(LC_ALL, "");
- setlocale(LC_ALL, "chs");
- std::wregex pattern(regular.c_str());
- if (!regex_match(src, pattern))
- {
- return false;
- }
- return true;
- }
- CString FormatNumberString(CString strData)
- {
- TCHAR szTemp[32] = { 0 };
- _stprintf(szTemp, _T("%0.4lf"), _tstof(strData));
- CString str = szTemp;
- str.TrimRight(_T("0"));
- if (str.Find(_T(".")) == (str.GetLength() - 1))
- str.TrimRight(_T("."));
- return str;
- }
- tstring replace_all(tstring& str, const tstring& old_value, const tstring& new_value)
- {
- while (true)
- {
- tstring::size_type pos(0);
- if ((pos = str.find(old_value)) != tstring::npos)
- str.replace(pos, old_value.length(), new_value);
- else break;
- }
- return str;
- }
- tstring FormatInt32Value(int nValue)
- {
- TCHAR szTemp[64] = { 0 };
- _stprintf(szTemp, _T("%d"), nValue);
- return tstring(szTemp);
- }
- string FormatInt32Value2(int nValue)
- {
- char szTemp[64] = { 0 };
- sprintf(szTemp, "%d", nValue);
- return string(szTemp);
- }
- tstring FormatInt64Value(__int64 nValue)
- {
- TCHAR szTemp[64] = { 0 };
- _stprintf(szTemp, _T("%lld"), nValue);
- return tstring(szTemp);
- }
- tstring FormatFloatValue(double fValue)
- {
- TCHAR szTemp[32] = { 0 };
- _stprintf(szTemp, _T("%0.3lf"), fValue);
- CString str = szTemp;
- str.TrimRight(_T("0"));
- if (str.Find(_T(".")) == (str.GetLength() - 1))
- str.TrimRight(_T("."));
- return tstring(str);
- }
- string FormatFloatValue2(double fValue)
- {
- char szTemp[64] = { 0 };
- sprintf(szTemp, "%0.3lf", fValue);
- return string(szTemp);
- }
- CString GetAppPath()
- {
- CString strRetun = _T("");
- #ifdef _UNICODE
- TCHAR szBuff[MAX_PATH];
- HMODULE module = GetModuleHandle(0);
- GetModuleFileName(module, szBuff, sizeof(szBuff));
- strRetun.Format(_T("%s"), szBuff);
- #else
- HMODULE module = GetModuleHandle(0);
- CHAR szBuff[MAX_PATH];
- GetModuleFileName(module, szBuff, sizeof(szBuff));
- strRetun.Format(_T("%s"), szBuff);
- #endif
- int pos = strRetun.ReverseFind(_T('\\'));
- if (pos != -1)
- {
- strRetun = strRetun.Left(pos);
- }
- return strRetun;
- }
- string GetImageEncodeString(string strPath)
- {
- ifstream is(strPath.c_str(), ifstream::in | ios::binary);
- is.seekg(0, is.end);
- int length = is.tellg();
- is.seekg(0, is.beg);
- char *buffer = new char[length];
- is.read(buffer, length);
- //主要注意这一句是利用base64.h文件中的编码函数将buffer编码为base64码
- CBase64Coder base64;
- std::string strImg = base64.encode(buffer, length);
- string strTemp = "data:image/jpeg;base64,";
- strTemp.append(strImg);
- //打印img看一下
- string strEncode = UrlEncode(strTemp);
- //删除buffer
- delete[]buffer;
- //关闭指针
- is.close();
- return strTemp;
- }
|