Remove unused ProjectConfiguration
[xy_vsfilter.git] / src / dsutil / text.cpp
blob9264a5a85baccaadb2fac5f4518fe7669cb007ef
1 #include "stdafx.h"
2 #include "text.h"
4 /*
5 CString Explode(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
7 sl.RemoveAll();
9 if(limit == 1) {sl.AddTail(str); return _T("");}
11 if(!str.IsEmpty() && str[str.GetLength()-1] != sep)
12 str += 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();
19 sl.AddTail(tmp);
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();
27 sl.AddTail(tmp);
29 break;
33 if(sl.IsEmpty())
35 str.TrimLeft(sep); str.TrimRight(sep);
36 str.TrimLeft(); str.TrimRight();
37 sl.AddTail(str);
40 return sl.GetHead();
43 CString ExplodeMin(CString str, CAtlList<CString>& sl, TCHAR sep, int limit)
45 Explode(str, sl, sep, limit);
46 POSITION pos = sl.GetHeadPosition();
47 while(pos)
49 POSITION tmp = pos;
50 if(sl.GetNext(pos).IsEmpty())
51 sl.RemoveAt(tmp);
53 if(sl.IsEmpty()) sl.AddTail(CString()); // eh
55 return sl.GetHead();
58 CString Implode(CAtlList<CString>& sl, TCHAR sep)
60 CString ret;
61 POSITION pos = sl.GetHeadPosition();
62 while(pos)
64 ret += sl.GetNext(pos);
65 if(pos) ret += sep;
67 return(ret);
71 DWORD CharSetToCodePage(DWORD dwCharSet)
73 if(dwCharSet == CP_UTF8) return CP_UTF8;
74 if(dwCharSet == CP_UTF7) return CP_UTF7;
75 CHARSETINFO cs={0};
76 ::TranslateCharsetInfo((DWORD *)dwCharSet, &cs, TCI_SRCCHARSET);
77 return cs.ciACP;
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,
95 utf16, len,
96 mbcs, str.GetLength()*6,
97 NULL, NULL);
99 str = mbcs;
101 delete [] utf16;
102 delete [] mbcs;
104 return str;
107 CStringA UrlEncode(CStringA str, bool fRaw)
109 CStringA urlstr;
111 for(int i = 0; i < str.GetLength(); i++)
113 CHAR c = str[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;}
120 return urlstr;
123 CStringA UrlDecode(CStringA str, bool fRaw)
125 str.Replace("&amp;", "&");
127 CHAR* s = str.GetBuffer(str.GetLength());
128 CHAR* e = s + str.GetLength();
129 CHAR* s1 = s;
130 CHAR* s2 = s;
131 while(s1 < e)
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'))
140 s1[1] = s11;
141 s1[2] = s12;
142 *s2 = 0;
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;
145 *s2 <<= 4;
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;
148 s1 += 2;
150 else
152 *s2 = *s1 == '+' && !fRaw ? ' ' : *s1;
155 s1++;
156 s2++;
159 str.ReleaseBuffer(s2 - s);
161 return str;
164 CString ExtractTag(CString tag, CMapStringToString& attribs, bool& fClosing)
166 tag.Trim();
167 attribs.RemoveAll();
169 fClosing = !tag.IsEmpty() ? tag[0] == '/' : false;
170 tag.TrimLeft('/');
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();
180 tag = tag.Mid(i+1);
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();
187 if(!param.IsEmpty())
188 attribs[attrib] = param;
189 tag = i+1 < tag.GetLength() ? tag.Mid(i+1) : _T("");
192 return(type);
195 CAtlList<CString>& MakeLower(CAtlList<CString>& sl)
197 POSITION pos = sl.GetHeadPosition();
198 while(pos) sl.GetNext(pos).MakeLower();
199 return sl;
202 CAtlList<CString>& MakeUpper(CAtlList<CString>& sl)
204 POSITION pos = sl.GetHeadPosition();
205 while(pos) sl.GetNext(pos).MakeUpper();
206 return sl;
209 CAtlList<CString>& RemoveStrings(CAtlList<CString>& sl, int minlen, int maxlen)
211 POSITION pos = sl.GetHeadPosition();
212 while(pos)
214 POSITION tmp = pos;
215 CString& str = sl.GetNext(pos);
216 int len = str.GetLength();
217 if(len < minlen || len > maxlen) sl.RemoveAt(tmp);
219 return sl;