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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#include "stdafx.h"
#include "text.h"
/*
CString Explode(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
{
sl.RemoveAll();
if(limit == 1) {sl.AddTail(str); return _T("");}
if(!str.IsEmpty() && str[str.GetLength()-1] != sep)
str += sep;
for(ptrdiff_t i = 0, j = 0; (j = str.Find(sep, i)) >= 0; i = j+1)
{
CString tmp = str.Mid(i, j-i);
tmp.TrimLeft(sep); tmp.TrimRight(sep);
tmp.TrimLeft(); tmp.TrimRight();
sl.AddTail(tmp);
if(limit > 0 && sl.GetCount() == limit-1)
{
if(j+1 < str.GetLength())
{
CString tmp = str.Mid(j+1);
tmp.TrimLeft(sep); tmp.TrimRight(sep);
tmp.TrimLeft(); tmp.TrimRight();
sl.AddTail(tmp);
}
break;
}
}
if(sl.IsEmpty())
{
str.TrimLeft(sep); str.TrimRight(sep);
str.TrimLeft(); str.TrimRight();
sl.AddTail(str);
}
return sl.GetHead();
}
CString ExplodeMin(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
{
Explode(str, sl, sep, limit);
POSITION pos = sl.GetHeadPosition();
while(pos)
{
POSITION tmp = pos;
if(sl.GetNext(pos).IsEmpty())
sl.RemoveAt(tmp);
}
if(sl.IsEmpty()) sl.AddTail(CString()); // eh
return sl.GetHead();
}
CString Implode(CAtlList<CString>& sl, TCHAR sep)
{
CString ret;
POSITION pos = sl.GetHeadPosition();
while(pos)
{
ret += sl.GetNext(pos);
if(pos) ret += sep;
}
return(ret);
}
*/
DWORD CharSetToCodePage(DWORD dwCharSet)
{
if(dwCharSet == CP_UTF8) return CP_UTF8;
if(dwCharSet == CP_UTF7) return CP_UTF7;
CHARSETINFO cs={0};
::TranslateCharsetInfo((DWORD *)dwCharSet, &cs, TCI_SRCCHARSET);
return cs.ciACP;
}
CStringA ConvertMBCS(CStringA str, DWORD SrcCharSet, DWORD DstCharSet)
{
WCHAR* utf16 = DNew WCHAR[str.GetLength()+1];
memset(utf16, 0, (str.GetLength()+1)*sizeof(WCHAR));
CHAR* mbcs = DNew CHAR[str.GetLength()*6+1];
memset(mbcs, 0, str.GetLength()*6+1);
int len = MultiByteToWideChar(
CharSetToCodePage(SrcCharSet), 0,
str.GetBuffer(str.GetLength()), str.GetLength(),
utf16, (str.GetLength()+1)*sizeof(WCHAR));
len = WideCharToMultiByte(
CharSetToCodePage(DstCharSet), 0,
utf16, len,
mbcs, str.GetLength()*6,
NULL, NULL);
str = mbcs;
delete [] utf16;
delete [] mbcs;
return str;
}
CStringA UrlEncode(CStringA str, bool fRaw)
{
CStringA urlstr;
for(size_t i = 0; i < str.GetLength(); i++)
{
CHAR c = str[i];
if(fRaw && c == '+') urlstr += "%2B";
else if(c > 0x20 && c < 0x7f && c != '&') urlstr += c;
else if(c == 0x20) urlstr += fRaw ? ' ' : '+';
else {CStringA tmp; tmp.Format("%%%02x", (BYTE)c); urlstr += tmp;}
}
return urlstr;
}
CStringA UrlDecode(CStringA str, bool fRaw)
{
str.Replace("&", "&");
CHAR* s = str.GetBuffer(str.GetLength());
CHAR* e = s + str.GetLength();
CHAR* s1 = s;
CHAR* s2 = s;
while(s1 < e)
{
CHAR s11 = (s1 < e-1) ? (__isascii(s1[1]) && isupper(s1[1]) ? tolower(s1[1]) : s1[1]) : 0;
CHAR s12 = (s1 < e-2) ? (__isascii(s1[2]) && isupper(s1[2]) ? tolower(s1[2]) : s1[2]) : 0;
if(*s1 == '%' && s1 < e-2
&& (s1[1] >= '0' && s1[1] <= '9' || s11 >= 'a' && s11 <= 'f')
&& (s1[2] >= '0' && s1[2] <= '9' || s12 >= 'a' && s12 <= 'f'))
{
s1[1] = s11;
s1[2] = s12;
*s2 = 0;
if(s1[1] >= '0' && s1[1] <= '9') *s2 |= s1[1]-'0';
else if(s1[1] >= 'a' && s1[1] <= 'f') *s2 |= s1[1]-'a'+10;
*s2 <<= 4;
if(s1[2] >= '0' && s1[2] <= '9') *s2 |= s1[2]-'0';
else if(s1[2] >= 'a' && s1[2] <= 'f') *s2 |= s1[2]-'a'+10;
s1 += 2;
}
else
{
*s2 = *s1 == '+' && !fRaw ? ' ' : *s1;
}
s1++;
s2++;
}
str.ReleaseBuffer(s2 - s);
return str;
}
CString ExtractTag(CString tag, CMapStringToString& attribs, bool& fClosing)
{
tag.Trim();
attribs.RemoveAll();
fClosing = !tag.IsEmpty() ? tag[0] == '/' : false;
tag.TrimLeft('/');
int i = tag.Find(' ');
if(i < 0) i = tag.GetLength();
CString type = tag.Left(i).MakeLower();
tag = tag.Mid(i).Trim();
while((i = tag.Find('=')) > 0)
{
CString attrib = tag.Left(i).Trim().MakeLower();
tag = tag.Mid(i+1);
for(i = 0; i < tag.GetLength() && _istspace(tag[i]); i++);
tag = i < tag.GetLength() ? tag.Mid(i) : _T("");
if(!tag.IsEmpty() && tag[0] == '\"') {tag = tag.Mid(1); i = tag.Find('\"');}
else i = tag.Find(' ');
if(i < 0) i = tag.GetLength();
CString param = tag.Left(i).Trim();
if(!param.IsEmpty())
attribs[attrib] = param;
tag = i+1 < tag.GetLength() ? tag.Mid(i+1) : _T("");
}
return(type);
}
CAtlList<CString>& MakeLower(CAtlList<CString>& sl)
{
POSITION pos = sl.GetHeadPosition();
while(pos) sl.GetNext(pos).MakeLower();
return sl;
}
CAtlList<CString>& MakeUpper(CAtlList<CString>& sl)
{
POSITION pos = sl.GetHeadPosition();
while(pos) sl.GetNext(pos).MakeUpper();
return sl;
}
CAtlList<CString>& RemoveStrings(CAtlList<CString>& sl, int minlen, int maxlen)
{
POSITION pos = sl.GetHeadPosition();
while(pos)
{
POSITION tmp = pos;
CString& str = sl.GetNext(pos);
int len = str.GetLength();
if(len < minlen || len > maxlen) sl.RemoveAt(tmp);
}
return sl;
}
|