5 CString Explode(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
9 if(limit == 1) {sl.AddTail(str); return _T("");}
11 if(!str.IsEmpty() && str[str.GetLength()-1] != sep)
14 for(int i = 0, j = 0; (j = str.Find(sep, i)) >= 0; i = j+1)
16 CString tmp = str.Mid(i, j-i);
17 tmp.TrimLeft(sep); tmp.TrimRight(sep);
18 tmp.TrimLeft(); tmp.TrimRight();
20 if(limit > 0 && sl.GetCount() == limit-1)
22 if(j+1 < str.GetLength())
24 CString tmp = str.Mid(j+1);
25 tmp.TrimLeft(sep); tmp.TrimRight(sep);
26 tmp.TrimLeft(); tmp.TrimRight();
35 str.TrimLeft(sep); str.TrimRight(sep);
36 str.TrimLeft(); str.TrimRight();
43 CString ExplodeMin(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
45 Explode(str, sl, sep, limit);
46 POSITION pos = sl.GetHeadPosition();
50 if(sl.GetNext(pos).IsEmpty())
53 if(sl.IsEmpty()) sl.AddTail(CString()); // eh
58 CString Implode(CAtlList<CString>& sl, TCHAR sep)
61 POSITION pos = sl.GetHeadPosition();
64 ret += sl.GetNext(pos);
71 DWORD
CharSetToCodePage(DWORD dwCharSet
)
73 if(dwCharSet
== CP_UTF8
) return CP_UTF8
;
74 if(dwCharSet
== CP_UTF7
) return CP_UTF7
;
76 ::TranslateCharsetInfo((DWORD
*)dwCharSet
, &cs
, TCI_SRCCHARSET
);
80 CStringA
ConvertMBCS(CStringA str
, DWORD SrcCharSet
, DWORD DstCharSet
)
82 WCHAR
* utf16
= new WCHAR
[str
.GetLength()+1];
83 memset(utf16
, 0, (str
.GetLength()+1)*sizeof(WCHAR
));
85 CHAR
* mbcs
= new CHAR
[str
.GetLength()*6+1];
86 memset(mbcs
, 0, str
.GetLength()*6+1);
88 int len
= MultiByteToWideChar(
89 CharSetToCodePage(SrcCharSet
), 0,
90 str
.GetBuffer(str
.GetLength()), str
.GetLength(),
91 utf16
, (str
.GetLength()+1)*sizeof(WCHAR
));
93 len
= WideCharToMultiByte(
94 CharSetToCodePage(DstCharSet
), 0,
96 mbcs
, str
.GetLength()*6,
107 CStringA
UrlEncode(CStringA str
, bool fRaw
)
111 for(int i
= 0; i
< str
.GetLength(); i
++)
114 if(fRaw
&& c
== '+') urlstr
+= "%2B";
115 else if(c
> 0x20 && c
< 0x7f && c
!= '&') urlstr
+= c
;
116 else if(c
== 0x20) urlstr
+= fRaw
? ' ' : '+';
117 else {CStringA tmp
; tmp
.Format("%%%02x", (BYTE
)c
); urlstr
+= tmp
;}
123 CStringA
UrlDecode(CStringA str
, bool fRaw
)
125 str
.Replace("&", "&");
127 CHAR
* s
= str
.GetBuffer(str
.GetLength());
128 CHAR
* e
= s
+ str
.GetLength();
133 CHAR s11
= (s1
< e
-1) ? (__isascii(s1
[1]) && isupper(s1
[1]) ? tolower(s1
[1]) : s1
[1]) : 0;
134 CHAR s12
= (s1
< e
-2) ? (__isascii(s1
[2]) && isupper(s1
[2]) ? tolower(s1
[2]) : s1
[2]) : 0;
136 if(*s1
== '%' && s1
< e
-2
137 && (s1
[1] >= '0' && s1
[1] <= '9' || s11
>= 'a' && s11
<= 'f')
138 && (s1
[2] >= '0' && s1
[2] <= '9' || s12
>= 'a' && s12
<= 'f'))
143 if(s1
[1] >= '0' && s1
[1] <= '9') *s2
|= s1
[1]-'0';
144 else if(s1
[1] >= 'a' && s1
[1] <= 'f') *s2
|= s1
[1]-'a'+10;
146 if(s1
[2] >= '0' && s1
[2] <= '9') *s2
|= s1
[2]-'0';
147 else if(s1
[2] >= 'a' && s1
[2] <= 'f') *s2
|= s1
[2]-'a'+10;
152 *s2
= *s1
== '+' && !fRaw
? ' ' : *s1
;
159 str
.ReleaseBuffer(s2
- s
);
164 CString
ExtractTag(CString tag
, CMapStringToString
& attribs
, bool& fClosing
)
169 fClosing
= !tag
.IsEmpty() ? tag
[0] == '/' : false;
172 int i
= tag
.Find(' ');
173 if(i
< 0) i
= tag
.GetLength();
174 CString type
= tag
.Left(i
).MakeLower();
175 tag
= tag
.Mid(i
).Trim();
177 while((i
= tag
.Find('=')) > 0)
179 CString attrib
= tag
.Left(i
).Trim().MakeLower();
181 for(i
= 0; i
< tag
.GetLength() && _istspace(tag
[i
]); i
++);
182 tag
= i
< tag
.GetLength() ? tag
.Mid(i
) : _T("");
183 if(!tag
.IsEmpty() && tag
[0] == '\"') {tag
= tag
.Mid(1); i
= tag
.Find('\"');}
184 else i
= tag
.Find(' ');
185 if(i
< 0) i
= tag
.GetLength();
186 CString param
= tag
.Left(i
).Trim();
188 attribs
[attrib
] = param
;
189 tag
= i
+1 < tag
.GetLength() ? tag
.Mid(i
+1) : _T("");
195 CAtlList
<CString
>& MakeLower(CAtlList
<CString
>& sl
)
197 POSITION pos
= sl
.GetHeadPosition();
198 while(pos
) sl
.GetNext(pos
).MakeLower();
202 CAtlList
<CString
>& MakeUpper(CAtlList
<CString
>& sl
)
204 POSITION pos
= sl
.GetHeadPosition();
205 while(pos
) sl
.GetNext(pos
).MakeUpper();
209 CAtlList
<CString
>& RemoveStrings(CAtlList
<CString
>& sl
, int minlen
, int maxlen
)
211 POSITION pos
= sl
.GetHeadPosition();
215 CString
& str
= sl
.GetNext(pos
);
216 int len
= str
.GetLength();
217 if(len
< minlen
|| len
> maxlen
) sl
.RemoveAt(tmp
);