提交 | 用户 | age
|
4b03ae
|
1 |
#pragma once
|
Q |
2 |
#ifndef _UTILS_H_
|
|
3 |
#define _UTILS_H_
|
|
4 |
#include <string>
|
|
5 |
|
|
6 |
void DivideStringToArray(const CString& strSrc, const CString& strSeparator, CStringArray &arrayDest);
|
|
7 |
void MergeArrayToString(const CStringArray &arraysrc, const CString& strSeparator, CString &strDest);
|
|
8 |
|
|
9 |
// CString PZStrToCString(const char * pzstr);
|
|
10 |
|
|
11 |
int RegexMatch(const CString& line, const CString& reg, CString results[]);
|
|
12 |
int RegexMatchNoCase(const CString& line, const CString& reg, CString results[]);
|
|
13 |
|
|
14 |
CStringW CStrA2CStrW(const CStringA &cstrSrcA);
|
|
15 |
CStringA CStrW2CStrA(const CStringW &cstrSrcW);
|
|
16 |
|
|
17 |
|
|
18 |
#ifndef CStrT2CStrA
|
|
19 |
#ifdef _UNICODE
|
|
20 |
#define CStrT2CStrA(cstr) CStrW2CStrA((cstr))
|
|
21 |
#else
|
|
22 |
#define CStrT2CStrA(cstr) (cstr)
|
|
23 |
#endif
|
|
24 |
#endif
|
|
25 |
|
|
26 |
#ifndef CStrT2CStrW
|
|
27 |
#ifdef _UNICODE
|
|
28 |
#define CStrT2CStrW(cstr) (cstr)
|
|
29 |
#else
|
|
30 |
#define CStrT2CStrW(cstr) CStrA2CStrW((cstr))
|
|
31 |
#endif
|
|
32 |
#endif
|
|
33 |
|
|
34 |
#ifndef CStrA2CStrT
|
|
35 |
#ifdef _UNICODE
|
|
36 |
#define CStrA2CStrT(cstr) CStrA2CStrW((cstr))
|
|
37 |
#else
|
|
38 |
#define CStrA2CStrT(cstr) (cstr)
|
|
39 |
#endif
|
|
40 |
#endif
|
|
41 |
|
|
42 |
|
|
43 |
#ifndef CStrW2CStrT
|
|
44 |
#ifdef _UNICODE
|
|
45 |
#define CStrW2CStrT(cstr) (cstr)
|
|
46 |
#else
|
|
47 |
#define CStrW2CStrT(cstr) CStrW2CStrA((cstr))
|
|
48 |
#endif
|
|
49 |
#endif
|
|
50 |
static inline CString String2CString(const std::string& str) { CStringA cstrA(str.c_str()); return CStrA2CStrT(cstrA); };
|
|
51 |
static inline std::string CString2String(const CString& cstr){ CStringA cstrA = CStrT2CStrA(cstr); std::string str = cstrA.GetBuffer(0); return str; };
|
|
52 |
|
|
53 |
static inline CStringW CStrA2CStrW(const CStringA &cstrSrcA)
|
|
54 |
{
|
|
55 |
int len = MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, NULL, 0);
|
|
56 |
wchar_t *wstr = new wchar_t[len];
|
|
57 |
memset(wstr, 0, len*sizeof(wchar_t));
|
|
58 |
MultiByteToWideChar(CP_ACP, 0, LPCSTR(cstrSrcA), -1, wstr, len);
|
|
59 |
CStringW cstrDestW = wstr;
|
|
60 |
delete[] wstr;
|
|
61 |
|
|
62 |
return cstrDestW;
|
|
63 |
}
|
|
64 |
|
|
65 |
static inline CStringA CStrW2CStrA(const CStringW &cstrSrcW)
|
|
66 |
{
|
|
67 |
int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);
|
|
68 |
char *str = new char[len];
|
|
69 |
memset(str, 0, len);
|
|
70 |
WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);
|
|
71 |
CStringA cstrDestA = str;
|
|
72 |
delete[] str;
|
|
73 |
|
|
74 |
return cstrDestA;
|
|
75 |
}
|
|
76 |
|
|
77 |
inline float CutOneDigitDecrease(float d)
|
|
78 |
{
|
|
79 |
return float(int(d*10)-1)/10.0f;
|
|
80 |
}
|
|
81 |
inline float CutOneDigitIncrease(float d)
|
|
82 |
{
|
|
83 |
return float(int(d*10)+1)/10.0f;
|
|
84 |
}
|
|
85 |
|
|
86 |
inline CString CTimeToCString(const CTime& time)
|
|
87 |
{
|
|
88 |
CString str;
|
|
89 |
str.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),
|
|
90 |
time.GetYear(),time.GetMonth(),time.GetDay(),
|
|
91 |
time.GetHour(),time.GetMinute(),time.GetSecond());
|
|
92 |
return str;
|
|
93 |
}
|
|
94 |
inline CTime CStringToCTime(const CString str)
|
|
95 |
{
|
|
96 |
CStringArray arr;
|
|
97 |
CStringArray arrDate,arrTime;
|
|
98 |
DivideStringToArray(str,_T(" "),arr);
|
|
99 |
if (arr.GetCount()!=2)
|
|
100 |
{
|
|
101 |
return CTime();
|
|
102 |
}
|
|
103 |
DivideStringToArray(arr[0],_T("-"),arrDate);
|
|
104 |
DivideStringToArray(arr[1],_T(":"),arrTime);
|
|
105 |
if (arrDate.GetCount()!=3 || arrTime.GetCount()!=3)
|
|
106 |
{
|
|
107 |
return CTime();
|
|
108 |
}
|
|
109 |
int year,month,day,hour,min,sec;
|
|
110 |
year = _tstoi(arrDate[0]);
|
|
111 |
month = _tstoi(arrDate[1]);
|
|
112 |
day = _tstoi(arrDate[2]);
|
|
113 |
|
|
114 |
hour = _tstoi(arrTime[0]);
|
|
115 |
min = _tstoi(arrTime[1]);
|
|
116 |
sec = _tstoi(arrTime[2]);
|
|
117 |
return CTime(year,month,day,hour,min,sec);
|
|
118 |
}
|
|
119 |
#endif //_UTILS_H_
|