-
Notifications
You must be signed in to change notification settings - Fork 1
/
pch.cpp
194 lines (158 loc) · 4.96 KB
/
pch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "pch.h"
inline void swapDoubleInAsc(double& d1, double& d2)
{
if (d1 - d2 > LT_EPSILON)
swap(d1, d2);
}
// LT_Equal = 0,
// LT_GreatThan = 1,
// LT_LessThan = -1
LT_COMPARE compDouble(double d1, double d2)
{
if (d1 - d2 > LT_EPSILON)
return LT_GreatThan;
else if (d2 - d1 > LT_EPSILON)
return LT_LessThan;
return LT_Equal;
}
inline bool findStr(const string& src, const string& str)
{
if (src.find(str) == string::npos) // 未找到
return false;
return true;
}
#ifdef WIN32
inline wstring Utf2U(const string& strUtf8)
{
wstring wstrRet(L"");
int nLen = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), strUtf8.length(), NULL, 0);
if (nLen == ERROR_NO_UNICODE_TRANSLATION)
throw "Utf8ToUnicode出错:无效的UTF-8字符串。";
wstrRet.resize(nLen, '\0');
MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), strUtf8.length(), (LPWSTR)wstrRet.c_str(), nLen);
return wstrRet;
}
inline string U2Utf(const wstring& wstr)
{
string strDes;
if (wstr.empty())
return strDes;
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
if (0 == nLen)
return strDes;
strDes.resize(nLen);
::WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), (LPSTR)strDes.c_str(), nLen, NULL, NULL);
return strDes;
}
inline string U2A(const wstring& str)
{
string strDes;
if (str.empty())
return strDes;
int nLen = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
if (0 == nLen)
return strDes;
strDes.resize(nLen);
::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), (LPSTR)strDes.c_str(), nLen, NULL, NULL);
return strDes;
}
inline wstring A2U(const string& str)
{
wstring wstrRet;
if (str.empty())
return wstrRet;
int nLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
if (0 == nLen)
return wstrRet;
wstrRet.resize(nLen, '\0');
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), (LPWSTR)wstrRet.c_str(), nLen);
return wstrRet;
}
string StringToUtf(string strValue)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1];//加上末尾'\0'
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), strValue.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete[]pBuf;
delete[]pwBuf;
return retStr;
}
#else
#define Sleep(X) std::this_thread::sleep_for(std::chrono::milliseconds(X));
wstring Utf2U(const string& s)
{
setlocale(LC_CTYPE, "zh_CN.UTF-8");
int iWLen = mbstowcs(NULL, s.c_str(), s.length());
wchar_t* lpwsz = new wchar_t[iWLen + 1];
mbstowcs(lpwsz, s.c_str(), s.length());
wstring wstr(lpwsz);
delete[]lpwsz;
return wstr;
}
string U2A(const wstring& ws)
{
//string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
//setlocale(LC_ALL, "chs");
//const wchar_t* _Source = ws.c_str();
//size_t _Dsize = 2 * ws.size() + 1;
//char *_Dest = new char[_Dsize];
//std::memset(_Dest, 0, _Dsize);
//wcstombs(_Dest, _Source, _Dsize);
//string result = _Dest;
//delete[]_Dest;
//setlocale(LC_ALL, curLocale.c_str());
// return result;
string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "zh_CN.UTF-8");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 3 * ws.size() + 1;
char* _Dest = new char[_Dsize];
std::memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
wstring A2U(const string& s)
{
//setlocale(LC_ALL, "chs");
//const char* _Source = s.c_str();
//size_t _Dsize = s.size() + 1;
//wchar_t *_Dest = new wchar_t[_Dsize];
//wmemset(_Dest, 0, _Dsize);
//mbstowcs(_Dest, _Source, _Dsize);
//wstring result = _Dest;
//delete[]_Dest;
//setlocale(LC_ALL, "C");
//return result;
setlocale(LC_CTYPE, "zh_CN.UTF-8");
int iWLen = mbstowcs(NULL, s.c_str(), s.length());
wchar_t* lpwsz = new wchar_t[iWLen + 1];
mbstowcs(lpwsz, s.c_str(), s.length());
wstring wstr(lpwsz);
delete[]lpwsz;
return wstr;
}
string U2Utf(const wstring& ws)
{
string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "zh_CN.UTF-8");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 3 * ws.size() + 1;
char* _Dest = new char[_Dsize];
std::memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
#endif