text.cpp

00001 #include "stdafx.h"
00002 #include <afxtempl.h>
00003 
00004 /*
00005 CString Explode(CString str, CList<CString>& sl, TCHAR sep, int limit)
00006 {
00007         sl.RemoveAll();
00008 
00009         if(limit == 1) {sl.AddTail(str); return _T("");}
00010 
00011         if(!str.IsEmpty() && str[str.GetLength()-1] != sep)
00012                 str += sep;
00013 
00014         for(int i = 0, j = 0; (j = str.Find(sep, i)) >= 0; i = j+1)
00015         {
00016                 CString tmp = str.Mid(i, j-i);
00017                 tmp.TrimLeft(sep); tmp.TrimRight(sep);
00018                 tmp.TrimLeft(); tmp.TrimRight();
00019                 sl.AddTail(tmp);
00020                 if(limit > 0 && sl.GetCount() == limit-1)
00021                 {
00022                         if(j+1 < str.GetLength()) 
00023                         {
00024                                 CString tmp = str.Mid(j+1);
00025                                 tmp.TrimLeft(sep); tmp.TrimRight(sep);
00026                                 tmp.TrimLeft(); tmp.TrimRight();
00027                                 sl.AddTail(tmp);
00028                         }
00029                         break;
00030                 }
00031         }
00032 
00033         if(sl.IsEmpty())
00034         {
00035                 str.TrimLeft(sep); str.TrimRight(sep);
00036                 str.TrimLeft(); str.TrimRight();
00037                 sl.AddTail(str);
00038         }
00039 
00040         return sl.GetHead();
00041 }
00042 
00043 CString ExplodeMin(CString str, CList<CString>& sl, TCHAR sep, int limit)
00044 {
00045         Explode(str, sl, sep, limit);
00046         POSITION pos = sl.GetHeadPosition();
00047         while(pos) 
00048         {
00049                 POSITION tmp = pos;
00050                 if(sl.GetNext(pos).IsEmpty())
00051                         sl.RemoveAt(tmp);
00052         }
00053         if(sl.IsEmpty()) sl.AddTail(CString()); // eh
00054 
00055         return sl.GetHead();
00056 }
00057 
00058 CString Implode(CList<CString>& sl, TCHAR sep)
00059 {
00060         CString ret;
00061         POSITION pos = sl.GetHeadPosition();
00062         while(pos)
00063         {
00064                 ret += sl.GetNext(pos);
00065                 if(pos) ret += sep;
00066         }
00067         return(ret);
00068 }
00069 */
00070 
00071 DWORD CharSetToCodePage(DWORD dwCharSet)
00072 {
00073         if(dwCharSet == CP_UTF8) return CP_UTF8;
00074         if(dwCharSet == CP_UTF7) return CP_UTF7;
00075         CHARSETINFO cs={0};
00076         ::TranslateCharsetInfo((DWORD *)dwCharSet, &cs, TCI_SRCCHARSET);
00077         return cs.ciACP;
00078 }
00079 
00080 CStringA ConvertMBCS(CStringA str, DWORD SrcCharSet, DWORD DstCharSet)
00081 {
00082         WCHAR* utf16 = new WCHAR[str.GetLength()+1];
00083         memset(utf16, 0, (str.GetLength()+1)*sizeof(WCHAR));
00084 
00085         CHAR* mbcs = new CHAR[str.GetLength()*6+1];
00086         memset(mbcs, 0, str.GetLength()*6+1);
00087 
00088         int len = MultiByteToWideChar(
00089                 CharSetToCodePage(SrcCharSet), 0, 
00090                 str.GetBuffer(str.GetLength()), str.GetLength(), 
00091                 utf16, (str.GetLength()+1)*sizeof(WCHAR));
00092 
00093         len = WideCharToMultiByte(
00094                 CharSetToCodePage(DstCharSet), 0, 
00095                 utf16, len, 
00096                 mbcs, str.GetLength()*6,
00097                 NULL, NULL);
00098 
00099         str = mbcs;
00100 
00101         delete [] utf16;
00102         delete [] mbcs;
00103 
00104         return str;
00105 }
00106 
00107 CStringA UrlEncode(CStringA str, bool fRaw)
00108 {
00109         CStringA urlstr;
00110 
00111         for(int i = 0; i < str.GetLength(); i++)
00112         {
00113                 CHAR c = str[i];
00114                 if(fRaw && c == '+') urlstr += "%2B";
00115                 else if(c > 0x20 && c < 0x7f && c != '&') urlstr += c;
00116                 else if(c == 0x20) urlstr += fRaw ? ' ' : '+';
00117                 else {CStringA tmp; tmp.Format("%%%02x", (BYTE)c); urlstr += tmp;}
00118         }
00119 
00120         return urlstr;
00121 }
00122 
00123 CStringA UrlDecode(CStringA str, bool fRaw)
00124 {
00125         str.Replace("&amp;", "&");
00126 
00127         CHAR* s = str.GetBuffer(str.GetLength());
00128         CHAR* e = s + str.GetLength();
00129         CHAR* s1 = s;
00130         CHAR* s2 = s;
00131         while(s1 < e)
00132         {
00133                 CHAR s11 = (s1 < e-1) ? (__isascii(s1[1]) && isupper(s1[1]) ? tolower(s1[1]) : s1[1]) : 0;
00134                 CHAR s12 = (s1 < e-2) ? (__isascii(s1[2]) && isupper(s1[2]) ? tolower(s1[2]) : s1[2]) : 0;
00135 
00136                 if(*s1 == '%' && s1 < e-2
00137                 && (s1[1] >= '0' && s1[1] <= '9' || s11 >= 'a' && s11 <= 'f')
00138                 && (s1[2] >= '0' && s1[2] <= '9' || s12 >= 'a' && s12 <= 'f'))
00139                 {
00140                         s1[1] = s11;
00141                         s1[2] = s12;
00142                         *s2 = 0;
00143                         if(s1[1] >= '0' && s1[1] <= '9') *s2 |= s1[1]-'0';
00144                         else if(s1[1] >= 'a' && s1[1] <= 'f') *s2 |= s1[1]-'a'+10;
00145                         *s2 <<= 4;
00146                         if(s1[2] >= '0' && s1[2] <= '9') *s2 |= s1[2]-'0';
00147                         else if(s1[2] >= 'a' && s1[2] <= 'f') *s2 |= s1[2]-'a'+10;
00148                         s1 += 2;
00149                 }
00150                 else 
00151                 {
00152                         *s2 = *s1 == '+' && !fRaw ? ' ' : *s1;
00153                 }
00154 
00155                 s1++;
00156                 s2++;
00157         }
00158 
00159         str.ReleaseBuffer(s2 - s);
00160 
00161         return str;
00162 }
00163 
00164 CString ExtractTag(CString tag, CMapStringToString& attribs, bool& fClosing)
00165 {
00166         tag.Trim();
00167         attribs.RemoveAll();
00168 
00169         fClosing = !tag.IsEmpty() ? tag[0] == '/' : false;
00170         tag.TrimLeft('/');
00171 
00172         int i = tag.Find(' ');
00173         if(i < 0) i = tag.GetLength();
00174         CString type = tag.Left(i).MakeLower();
00175         tag = tag.Mid(i).Trim();
00176 
00177         while((i = tag.Find('=')) > 0)
00178         {
00179                 CString attrib = tag.Left(i).Trim().MakeLower();
00180                 tag = tag.Mid(i+1);
00181                 for(i = 0; i < tag.GetLength() && _istspace(tag[i]); i++);
00182                 tag = i < tag.GetLength() ? tag.Mid(i) : _T("");
00183                 if(!tag.IsEmpty() && tag[0] == '\"') {tag = tag.Mid(1); i = tag.Find('\"');}
00184                 else i = tag.Find(' ');
00185                 if(i < 0) i = tag.GetLength();
00186                 CString param = tag.Left(i).Trim();
00187                 if(!param.IsEmpty())
00188                         attribs[attrib] = param;
00189                 tag = i+1 < tag.GetLength() ? tag.Mid(i+1) : _T("");
00190         }
00191 
00192         return(type);
00193 }
00194 
00195 CList<CString>& MakeLower(CList<CString>& sl)
00196 {
00197         POSITION pos = sl.GetHeadPosition();
00198         while(pos) sl.GetNext(pos).MakeLower();
00199         return sl;
00200 }
00201 
00202 CList<CString>& MakeUpper(CList<CString>& sl)
00203 {
00204         POSITION pos = sl.GetHeadPosition();
00205         while(pos) sl.GetNext(pos).MakeUpper();
00206         return sl;
00207 }
00208 
00209 CList<CString>& RemoveStrings(CList<CString>& sl, int minlen, int maxlen)
00210 {
00211         POSITION pos = sl.GetHeadPosition();
00212         while(pos)
00213         {
00214                 POSITION tmp = pos;
00215                 CString& str = sl.GetNext(pos);
00216                 int len = str.GetLength();
00217                 if(len < minlen || len > maxlen) sl.RemoveAt(tmp);
00218         }
00219         return sl;
00220 }

Generated on Tue Dec 13 14:47:07 2005 for guliverkli by  doxygen 1.4.5