9 namespace StringCodec {
11 void Iso2022Jp::Encoder::encode_char(UnicodeChar ch, string &buf)
13 if(ch>=0 && ch<=0x7F && ch!=0x5C && ch!=0x7E)
15 if(mode!=ASCII && mode!=JISX0201)
16 switch_mode(ASCII, buf);
19 else if(ch==0x5C || ch==0x7E)
22 switch_mode(ASCII, buf);
25 else if(ch==0xA5 || ch==0x203E)
28 switch_mode(JISX0201, buf);
36 Kuten jis = ucs_to_jisx0208(ch);
38 return error(ch, buf, "Can't express character in ISO-2022-JP");
41 switch_mode(JISX0208, buf);
43 char jbuf[2] = {jis.ku+0x20, jis.ten+0x20};
48 void Iso2022Jp::Encoder::sync(string &buf)
51 switch_mode(ASCII, buf);
54 void Iso2022Jp::Encoder::reset()
59 void Iso2022Jp::Encoder::switch_mode(Mode m, string &buf)
64 case ASCII: buf.append("\033(B", 3); break;
65 case JISX0201: buf.append("\033(J", 3); break;
66 case JISX0208: buf.append("\033$B", 3); break;
67 default: throw CodecError("WTF? Invalid mode in Iso2022Jp::Encoder::switch_mode");
71 void Iso2022Jp::Encoder::transliterate(UnicodeChar, string &buf)
74 switch_mode(ASCII, buf);
79 Iso2022Jp::Decoder::Decoder(ErrorMode em):
82 dec(new Ascii::Decoder)
85 UnicodeChar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
88 return error("No input");
92 string::const_iterator j = i;
94 UnicodeChar result = -1;
98 for(++j; j!=str.end(); ++j)
100 escape = escape<<8 | static_cast<unsigned char>(*j);
101 if(*j>='@' && *j<='Z')
108 case 0x2842: switch_mode(ASCII); break; // ESC ( B
109 case 0x284A: switch_mode(JISX0201); break; // ESC ( J
110 case 0x2440: // ESC $ @
111 case 0x2442: switch_mode(JISX0208); break; // ESC $ B
122 return dec->decode_char(str, i);
124 throw CodecError("WTF? No sub-decoder for Iso2022Jp::Decoder");
133 void Iso2022Jp::Decoder::reset()
137 dec = new Ascii::Decoder;
140 void Iso2022Jp::Decoder::switch_mode(Mode m)
147 case ASCII: dec = new Ascii::Decoder; break;
148 case JISX0201: dec = new JisX0201::Decoder; break;
149 case JISX0208: dec = new JisX0208::Decoder; break;
153 } // namespace StringCodec