3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
17 void Iso2022Jp::Encoder::encode_char(UnicodeChar ch, string &buf)
19 if(ch>=0 && ch<=0x7F && ch!=0x5C && ch!=0x7E)
21 if(mode!=ASCII && mode!=JISX0201)
22 switch_mode(ASCII, buf);
25 else if(ch==0x5C || ch==0x7E)
28 switch_mode(ASCII, buf);
31 else if(ch==0xA5 || ch==0x203E)
34 switch_mode(JISX0201, buf);
42 Kuten jis = ucs_to_jisx0208(ch);
44 return error(ch, buf, "Can't express character in ISO-2022-JP");
47 switch_mode(JISX0208, buf);
49 char jbuf[2] = {jis.ku+0x20, jis.ten+0x20};
54 void Iso2022Jp::Encoder::sync(string &buf)
57 switch_mode(ASCII, buf);
60 void Iso2022Jp::Encoder::reset()
65 void Iso2022Jp::Encoder::switch_mode(Mode m, string &buf)
70 case ASCII: buf.append("\033(B", 3); break;
71 case JISX0201: buf.append("\033(J", 3); break;
72 case JISX0208: buf.append("\033$B", 3); break;
73 default: throw CodecError("WTF? Invalid mode in Iso2022Jp::Encoder::switch_mode");
77 void Iso2022Jp::Encoder::transliterate(UnicodeChar, string &buf)
80 switch_mode(ASCII, buf);
85 Iso2022Jp::Decoder::Decoder(ErrorMode em):
88 dec(new Ascii::Decoder)
91 UnicodeChar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
94 return error("No input");
98 string::const_iterator j = i;
100 UnicodeChar result = -1;
104 for(++j; j!=str.end(); ++j)
106 escape = escape<<8 | static_cast<unsigned char>(*j);
107 if(*j>='@' && *j<='Z')
114 case 0x2842: switch_mode(ASCII); break; // ESC ( B
115 case 0x284A: switch_mode(JISX0201); break; // ESC ( J
116 case 0x2440: // ESC $ @
117 case 0x2442: switch_mode(JISX0208); break; // ESC $ B
128 return dec->decode_char(str, i);
130 throw CodecError("WTF? No sub-decoder for Iso2022Jp::Decoder");
139 void Iso2022Jp::Decoder::reset()
143 dec = new Ascii::Decoder;
146 void Iso2022Jp::Decoder::switch_mode(Mode m)
153 case ASCII: dec = new Ascii::Decoder; break;
154 case JISX0201: dec = new JisX0201::Decoder; break;
155 case JISX0208: dec = new JisX0208::Decoder; break;
159 } // namespace Codecs