10 #include "windows1252.h"
15 namespace StringCodec {
17 bool Codec::detect(const string &str) const
19 Decoder *dec = create_decoder();
23 for(string::const_iterator i=str.begin(); i!=str.end(); )
24 dec->decode_char(str, i);
26 catch(const CodecError &)
36 void Codec::Encoder::encode(const ustring &str, string &buf)
38 for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
42 string Codec::Encoder::encode(const ustring &str)
50 void Codec::Encoder::error(UnicodeChar ch, string &buf, const string &msg)
55 transliterate(ch, buf);
59 throw CodecError(msg);
64 void Codec::Decoder::decode(const string &str, ustring &buf)
66 for(string::const_iterator i=str.begin(); i!=str.end();)
68 UnicodeChar c = decode_char(str, i);
74 ustring Codec::Decoder::decode(const string &str)
81 UnicodeChar Codec::Decoder::error(const string &msg)
90 throw CodecError(msg);
94 Codec *create_codec(const string &n)
97 for(string::const_iterator i=n.begin(); i!=n.end(); ++i)
101 else if(islower(*i) || isdigit(*i))
105 if(name=="ascii") return new Ascii;
106 if(name=="iso2022jp") return new Iso2022Jp;
107 if(name=="iso646fi") return new Iso646Fi;
108 if(name=="iso88591" || name=="latin1") return new Iso88591;
109 if(name=="iso885915" || name=="latin9") return new Iso885915;
110 if(name=="jisx0201") return new JisX0201;
111 if(name=="jisx0208") return new JisX0208;
112 if(name=="utf8") return new Utf8;
113 if(name=="windows1252" || name=="cp1252") return new Windows1252;
114 throw InvalidParameterValue("Unknown string codec");
117 Codec *detect_codec(const string &str)
120 bool is_ascii = true;
121 bool is_latin1 = true;
122 unsigned utf8_mb = 0;
124 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
126 unsigned char c = *i;
139 else if((c&0xC0)==0xC0)
148 for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
166 return new Windows1252;
169 } // namespace StringCodec