// Util.cpp : 实现文件 // #include "stdafx.h" #include "Util.h" #include #include #include #include typedef basic_string::size_type S_T; wstring strA2W(const string &str) { int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t *wstr = new wchar_t[len + 2]; memset(wstr, 0, (2 + len)*sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, len); wstring cstrDestW = wstr; delete[] wstr; return cstrDestW; } string strW2A(const wstring &wstr) { int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); char *str = new char[len + 2]; memset(str, 0, len + 2); WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, len, NULL, NULL); string cstrDestA = str; delete[] str; return cstrDestA; } string strWtoUTF8(const wstring &src) { string result=""; int n = WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, 0, 0, 0, 0 ); result.resize(n); ::WideCharToMultiByte( CP_UTF8, 0, src.c_str(), -1, (char*)result.c_str(), result.length(), 0, 0 ); return result; } wstring strUTF8toW(const string& src) { wstring result=L""; int n = MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, NULL, 0 ) + 1 ; result.resize(n + 1); ::MultiByteToWideChar( CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length()); result[n] = L'\0'; return result; } 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); } char* PBQ_TrimLeftA(char *pTarget) { char buf[] = " \t"; return PBQ_TrimLeftMA(pTarget, buf); } char* PBQ_TrimRightA(char *pTarget) { char buf[] = " \t"; return PBQ_TrimRightMA(pTarget, buf); } char* PBQ_TrimLeftMA(char *pTarget, char* pChars) { char *ptr = pTarget; if(pTarget != NULL && pChars != NULL) { for (; ptr != NULL && *ptr != '\0'; ptr++) { if(NULL == strchr(pChars, *ptr)) { break; } } strcpy(pTarget, ptr); } return pTarget; } char* PBQ_TrimRightMA(char *pTarget, char* pChars) { char *pEnd = NULL; if (pTarget != NULL) { pEnd = pTarget + strlen(pTarget) - 1; for (; pEnd >= pTarget; pEnd--) { if(NULL != strchr(pChars, *pEnd)) { *pEnd = 0; } else { break; } } } return pTarget; } void Full2Half(wchar_t* str) { if(str != NULL) { int len = wcslen(str); int i = 0; wchar_t space[] = L" "; for(; i= 65281 && str[i] <= 65374) { str[i] -= (65281 - 33); } } } } bool split(const string& src, string delimit,vector &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; } bool split(const wstring& src, wstring delimit, vector &v, wstring 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; } wstring last_one = src.substr(last_search_position); v.push_back(last_one.empty() ? null_subst : last_one); return true; } vector splitEx(const string& src, string separate_character) { vector 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(CP_ACP, 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 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(CP_ACP, 0, pChar, -1, 0, 0, 0, 0); char *p = new char[n+1]; n = WideCharToMultiByte(CP_ACP, 0, pChar, -1, p, (int)n, 0, 0); string result(p); delete []pChar; delete []p; return result; } char* ConvertGB2312toUTF8(const char *pData) { int len = MultiByteToWideChar(CP_ACP, 0, pData, -1, NULL, 0); wchar_t* wstr = new wchar_t[len+1]; memset(wstr, 0, len+1); MultiByteToWideChar(CP_ACP, 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; return 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; } 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; } std::string 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 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] == '%') { 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(CString source) { CFileFind finder; CString path; if(PathFileExists(source)) { path.Format(_T("%s/*.*"),source); bool bWorking = finder.FindFile(path); while(bWorking){ bWorking = finder.FindNextFile(); if(finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 .. DeleteDirectory(finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName() } else{ //是文件 则直接删除 DeleteFile(finder.GetFilePath()); } } RemoveDirectory(source); } } void RecursiveDirectory(CString cstrDir) // 递归创建目录 { if (cstrDir.GetLength() <= 3)//是根目录,无需创建目录 { return; } if (cstrDir[cstrDir.GetLength()-1] == '\\') // 将路径改为目录 { cstrDir.Delete(cstrDir.GetLength()-1, 1); } // 修改文件属性 WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile(cstrDir, &wfd); // 查找 if (hFind != INVALID_HANDLE_VALUE) { FindClose(hFind); if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return; } // 创建当前目录的地目录失败 if (CreateDirectory(cstrDir,NULL) == false) {// 退到上一级目录 CString wstrNewDir = cstrDir; int n = wstrNewDir.ReverseFind('\\'); wstrNewDir = cstrDir.Left(n); // 递归进入 RecursiveDirectory(wstrNewDir); // 递归本函数,再创建目录 // 递归退出后创建之前失败的目录 CreateDirectory(cstrDir,NULL); // 递归返回,在存在的目录上再建目录 }// 多级目录创建成功 } 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 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 vctVol; vector 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; } 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 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> vctSplit; for (int i = 0; i < vctData.size(); i++) { vector 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; } //获取ODBC中Excel驱动函数 CString GetExcelDriverInfo() { TCHAR szBuf[2001]; WORD cbBufMax = 2000; WORD cbBufOut; TCHAR *pszBuf = szBuf; CString sDriver=L""; // 获取已安装驱动的名称(涵数在odbcinst.h里) if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut)) return L""; // 检索已安装的驱动是否有Excel... do { if (StrStrW(pszBuf, L"Excel") != 0) { //发现 ! CString temp(pszBuf); //if(sDriver.GetLength() < temp.GetLength()) sDriver = temp; break; } pszBuf = StrStrW(pszBuf, L'\0') + 1; } while (pszBuf[1] != L'\0'); return sDriver; } __int64 StringToInt64(const char *szSource) { __int64 ret(0); if (strlen(szSource) == 0) return ret; const char *pTemp = szSource + strlen(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; } string GetRequestByKey(const string& strKey, const string& strReuest) { try { smatch result; if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*?)&"))) { return result[1]; } else if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*)"))) { return result[1]; } else { return string(); } } catch (...) { return string(); } return string(); } void GetKeysByUrl(const string & strUrl, vector & vctKey) { vector vctSearch; split(strUrl, "?", vctSearch); if (vctSearch.size() != 2 && vctSearch.size() != 1) { return; } string strTemp; if (vctSearch.size() == 1) { strTemp = vctSearch[0]; } else if (vctSearch.size() == 2) { strTemp = vctSearch[1]; } vctSearch.clear(); split(strTemp, "&", vctSearch); if (vctSearch.size() <= 0) { return; } for (int i = 0; i < vctSearch.size(); i++) { strTemp = vctSearch[i]; vector vct; split(strTemp, "=", vct); if (vct.size() == 2) { vctKey.push_back(vct[0]); } } } __int64 GetCharNumber(char* 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; } } CStringW CStrA2W(const CStringA &cstrSrcA) { int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0); wchar_t *wstr = new wchar_t[len]; memset(wstr, 0, len*sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len); CStringW cstrDestW = wstr; delete[] wstr; return cstrDestW; } CStringA CStrW2A(const CStringW &cstrSrcW) { int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL); char *str = new char[len]; memset(str, 0, len); WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL); CStringA cstrDestA = str; delete[] str; return cstrDestA; } bool RegexStdMatch(string src, string regular) { regex pattern(regular.c_str()); if (!regex_match(src, pattern)) { return false; } return true; } bool RegexStdMatch(wstring src, wstring 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; } string replace_all(string& str, const string& old_value, const string& new_value) { while (true) { string::size_type pos(0); if ((pos = str.find(old_value)) != string::npos) str.replace(pos, old_value.length(), new_value); else break; } return str; } void sync_system_time(ULONGLONG time) { time_t _time = time; struct tm *tim = localtime(&_time); SYSTEMTIME system_time = {0}; //先获取本地时间 system_time.wYear = tim->tm_year+1900; system_time.wMonth =tim->tm_mon+1; system_time.wDay = tim->tm_mday; system_time.wHour = tim->tm_hour; system_time.wMinute = tim->tm_min; system_time.wSecond = tim->tm_sec; SetLocalTime(&system_time); }