10 #include "windows1252.h"
15 namespace StringCodec {
17 bool Codec::detect(const string &str) const
19 Decoder *dec = create_decoder(IGNORE_ERRORS);
22 for(string::const_iterator i=str.begin(); (result && i!=str.end()); )
23 result = (dec->decode_char(str, i)!=-1);
30 void Codec::Encoder::encode(const ustring &str, string &buf)
32 for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
36 string Codec::Encoder::encode(const ustring &str)
44 void Codec::Encoder::error(unichar ch, string &buf, const string &msg)
49 transliterate(ch, buf);
53 throw CodecError(msg);
58 void Codec::Decoder::decode(const string &str, ustring &buf)
60 for(string::const_iterator i=str.begin(); i!=str.end();)
62 unichar c = decode_char(str, i);
68 ustring Codec::Decoder::decode(const string &str)
75 unichar Codec::Decoder::error(const string &msg)
84 throw CodecError(msg);
88 Codec *create_codec(const string &n)
91 for(string::const_iterator i=n.begin(); i!=n.end(); ++i)
95 else if(islower(*i) || isdigit(*i))
99 if(name=="ascii") return new Ascii;
100 if(name=="iso2022jp") return new Iso2022Jp;
101 if(name=="iso646fi") return new Iso646Fi;
102 if(name=="iso88591" || name=="latin1") return new Iso88591;
103 if(name=="iso885915" || name=="latin9") return new Iso885915;
104 if(name=="jisx0201") return new JisX0201;
105 if(name=="jisx0208") return new JisX0208;
106 if(name=="utf8") return new Utf8;
107 if(name=="windows1252" || name=="cp1252") return new Windows1252;
108 throw InvalidParameterValue("Unknown string codec");
111 Codec *detect_codec(const string &str)
114 bool is_ascii = true;
115 bool is_latin1 = true;
116 unsigned utf8_mb = 0;
118 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
120 unsigned char c = *i;
133 else if((c&0xC0)==0xC0)
142 for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
160 return new Windows1252;
163 } // namespace StringCodec