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)
46 void Codec::Decoder::decode(const string &str, ustring &buf)
48 for(string::const_iterator i=str.begin(); i!=str.end();)
50 unichar c = decode_char(str, i);
56 ustring Codec::Decoder::decode(const string &str)
63 Codec *create_codec(const string &n)
66 string::const_iterator i;
67 for(i=n.begin(); i!=n.end(); ++i)
73 else if(islower(*i) || isdigit(*i))
77 ErrorMode em = THROW_ON_ERROR;
78 if(i!=n.end() && *i==':')
80 string em_str(i+1, n.end());
83 else if(em_str=="ignore")
85 else if(em_str=="trans" || em_str=="transliterate")
88 throw invalid_argument("invalid error mode");
91 if(name=="ascii") return new Ascii(em);
92 if(name=="iso2022jp") return new Iso2022Jp(em);
93 if(name=="iso646fi") return new Iso646Fi(em);
94 if(name=="iso88591" || name=="latin1") return new Iso88591(em);
95 if(name=="iso885915" || name=="latin9") return new Iso885915(em);
96 if(name=="jisx0201") return new JisX0201(em);
97 if(name=="jisx0208") return new JisX0208(em);
98 if(name=="utf8") return new Utf8(em);
99 if(name=="windows1252" || name=="cp1252") return new Windows1252(em);
100 throw invalid_argument("unknown string codec");
103 Codec *detect_codec(const string &str)
106 bool is_ascii = true;
107 bool is_latin1 = true;
108 unsigned utf8_mb = 0;
110 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
112 unsigned char c = *i;
125 else if((c&0xC0)==0xC0)
134 for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
152 return new Windows1252;
155 } // namespace StringCodec