#pragma once #ifndef _UTILS_H_ #define _UTILS_H_ #include void DivideStringToArray(const CString& strSrc, const CString& strSeparator, CStringArray &arrayDest); void MergeArrayToString(const CStringArray &arraysrc, const CString& strSeparator, CString &strDest); // CString PZStrToCString(const char * pzstr); int RegexMatch(const CString& line, const CString& reg, CString results[]); int RegexMatchNoCase(const CString& line, const CString& reg, CString results[]); CStringW CStrA2CStrW(const CStringA &cstrSrcA); CStringA CStrW2CStrA(const CStringW &cstrSrcW); #ifndef CStrT2CStrA #ifdef _UNICODE #define CStrT2CStrA(cstr) CStrW2CStrA((cstr)) #else #define CStrT2CStrA(cstr) (cstr) #endif #endif #ifndef CStrT2CStrW #ifdef _UNICODE #define CStrT2CStrW(cstr) (cstr) #else #define CStrT2CStrW(cstr) CStrA2CStrW((cstr)) #endif #endif #ifndef CStrA2CStrT #ifdef _UNICODE #define CStrA2CStrT(cstr) CStrA2CStrW((cstr)) #else #define CStrA2CStrT(cstr) (cstr) #endif #endif #ifndef CStrW2CStrT #ifdef _UNICODE #define CStrW2CStrT(cstr) (cstr) #else #define CStrW2CStrT(cstr) CStrW2CStrA((cstr)) #endif #endif static inline CString String2CString(const std::string& str) { CStringA cstrA(str.c_str()); return CStrA2CStrT(cstrA); }; static inline std::string CString2String(const CString& cstr){ CStringA cstrA = CStrT2CStrA(cstr); std::string str = cstrA.GetBuffer(0); return str; }; static inline CStringW CStrA2CStrW(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; } static inline CStringA CStrW2CStrA(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; } inline float CutOneDigitDecrease(float d) { return float(int(d*10)-1)/10.0f; } inline float CutOneDigitIncrease(float d) { return float(int(d*10)+1)/10.0f; } inline CString CTimeToCString(const CTime& time) { CString str; str.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), time.GetYear(),time.GetMonth(),time.GetDay(), time.GetHour(),time.GetMinute(),time.GetSecond()); return str; } inline CTime CStringToCTime(const CString str) { CStringArray arr; CStringArray arrDate,arrTime; DivideStringToArray(str,_T(" "),arr); if (arr.GetCount()!=2) { return CTime(); } DivideStringToArray(arr[0],_T("-"),arrDate); DivideStringToArray(arr[1],_T(":"),arrTime); if (arrDate.GetCount()!=3 || arrTime.GetCount()!=3) { return CTime(); } int year,month,day,hour,min,sec; year = _tstoi(arrDate[0]); month = _tstoi(arrDate[1]); day = _tstoi(arrDate[2]); hour = _tstoi(arrTime[0]); min = _tstoi(arrTime[1]); sec = _tstoi(arrTime[2]); return CTime(year,month,day,hour,min,sec); } #endif //_UTILS_H_