11 #include "windows1252.h"
16 namespace StringCodec {
18 bool Codec::detect(const string &str) const
20 Decoder *dec = create_decoder(IGNORE_ERRORS);
23 for(string::const_iterator i=str.begin(); (result && i!=str.end()); )
24 result = (dec->decode_char(str, i)!=-1);
31 void Codec::Encoder::encode(const ustring &str, string &buf)
33 for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
37 string Codec::Encoder::encode(const ustring &str)
47 void Codec::Decoder::decode(const string &str, ustring &buf)
49 for(string::const_iterator i=str.begin(); i!=str.end();)
51 unichar c = decode_char(str, i);
57 ustring Codec::Decoder::decode(const string &str)
64 Codec *create_codec(const string &n)
67 string::const_iterator i;
68 for(i=n.begin(); i!=n.end(); ++i)
74 else if(islower(*i) || isdigit(*i))
78 ErrorMode em = DEFAULT;
79 if(i!=n.end() && *i==':')
81 string em_str(i+1, n.end());
84 else if(em_str=="ignore")
86 else if(em_str=="trans" || em_str=="transliterate")
89 throw invalid_argument("invalid error mode");
92 if(name=="ascii") return new Ascii(em);
93 if(name=="iso2022jp") return new Iso2022Jp(em);
94 if(name=="iso646fi") return new Iso646Fi(em);
95 if(name=="iso88591" || name=="latin1") return new Iso88591(em);
96 if(name=="iso885915" || name=="latin9") return new Iso885915(em);
97 if(name=="jisx0201") return new JisX0201(em);
98 if(name=="jisx0208") return new JisX0208(em);
99 if(name=="utf8") return new Utf8(em);
100 if(name=="utf16") return new Utf16(em, Utf16::AUTO);
101 if(name=="utf16be") return new Utf16(em, Utf16::BIG);
102 if(name=="utf16le") return new Utf16(em, Utf16::LITTLE);
103 if(name=="windows1252" || name=="cp1252") return new Windows1252(em);
104 throw invalid_argument("unknown string codec");
107 Codec *detect_codec(const string &str)
110 bool is_ascii = true;
111 bool is_latin1 = true;
112 unsigned utf8_mb = 0;
114 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
116 unsigned char c = *i;
129 else if((c&0xC0)==0xC0)
138 for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
156 return new Windows1252;
159 } // namespace StringCodec