3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
10 #include "iso2022jp.h"
13 #include "iso885915.h"
17 #include "windows1252.h"
24 bool Codec::detect(const string &str) const
26 Decoder *dec=create_decoder();
30 for(string::const_iterator i=str.begin(); i!=str.end(); )
31 dec->decode_char(str, i);
33 catch(const CodecError &)
43 void Codec::Encoder::encode(const ustring &str, string &buf)
45 for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
49 string Codec::Encoder::encode(const ustring &str)
57 void Codec::Encoder::error(UnicodeChar ch, string &buf, const string &msg)
62 transliterate(ch, buf);
66 throw CodecError(msg);
71 void Codec::Decoder::decode(const string &str, ustring &buf)
73 for(string::const_iterator i=str.begin(); i!=str.end();)
75 UnicodeChar c=decode_char(str, i);
81 ustring Codec::Decoder::decode(const string &str)
88 UnicodeChar Codec::Decoder::error(const string &msg)
97 throw CodecError(msg);
101 Codec *create_codec(const string &n)
104 for(string::const_iterator i=n.begin(); i!=n.end(); ++i)
108 else if(islower(*i) || isdigit(*i))
112 if(name=="ascii") return new Ascii;
113 if(name=="iso2022jp") return new Iso2022Jp;
114 if(name=="iso646fi") return new Iso646Fi;
115 if(name=="iso88591" || name=="latin1") return new Iso88591;
116 if(name=="iso885915" || name=="latin9") return new Iso885915;
117 if(name=="jisx0201") return new JisX0201;
118 if(name=="jisx0208") return new JisX0208;
119 if(name=="utf8") return new Utf8;
120 if(name=="windows1252" || name=="cp1252") return new Windows1252;
121 throw InvalidParameterValue("Unknown string codec");
124 Codec *detect_codec(const string &str)
131 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
146 else if((c&0xC0)==0xC0)
155 for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
173 return new Windows1252;
176 } // namespace Codecs