1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282 |
- #include "pch.h"
- #include "BaseUtility.h"
- #include <odbcinst.h>
- #include <comdef.h>
- #include <afxdb.h>
- #include <regex>
- #include <iostream>
- #include <fstream>
- #include "Base64.h"
- #include <tuple>
- 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(std::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 FindDirFile(const CString &strPath, vector<std::tuple<CString, CString>>& vctFilePath)
- {
- 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())
- {
- FindDirFile(ff.GetFilePath(), vctFilePath);
- }
- else
- {
- vctFilePath.push_back({ ff.GetFilePath(), ff.GetFileName() });
- }
- }
- ff.Close();
- }
- 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;
- }
- string GenerateGUID()
- {
- string guid_code;
- char buf[64] = { 0 };
- GUID guid;
- if (S_OK == ::CoCreateGuid(&guid))
- {
- _snprintf(buf, sizeof(buf)
- , "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x"
- , guid.Data1
- , guid.Data2
- , guid.Data3
- , guid.Data4[0], guid.Data4[1]
- , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
- , guid.Data4[6], guid.Data4[7]
- );
- guid_code = buf;
- }
- else
- {
- CTime tmNow = CTime::GetCurrentTime();
- std::wstring strNow = tmNow.Format("%Y%m%d%H%M%S");
- /*guid_code = strW2A(strNow);*/
- }
- return guid_code;
- }
|