9 namespace StringCodec {
11 void Iso2022Jp::Encoder::encode_char(unichar 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, invalid_character(ch, "ISO-2022-JP"));
41 switch_mode(JISX0208, buf);
44 jbuf[0] = jis.ku+0x20;
45 jbuf[1] = jis.ten+0x20;
50 void Iso2022Jp::Encoder::sync(string &buf)
53 switch_mode(ASCII, buf);
56 void Iso2022Jp::Encoder::reset()
61 void Iso2022Jp::Encoder::switch_mode(Mode m, string &buf)
66 case ASCII: buf.append("\033(B", 3); break;
67 case JISX0201: buf.append("\033(J", 3); break;
68 case JISX0208: buf.append("\033$B", 3); break;
69 default: throw invalid_argument("Iso2022Jp::Encoder::switch_mode");
73 void Iso2022Jp::Encoder::transliterate(unichar, string &buf)
76 switch_mode(ASCII, buf);
81 Iso2022Jp::Decoder::Decoder(ErrorMode em):
84 dec(new Ascii::Decoder)
87 unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
94 string::const_iterator j = i;
100 for(++j; j!=str.end(); ++j)
102 escape = escape<<8 | static_cast<unsigned char>(*j);
103 if(*j>='@' && *j<='Z')
110 case 0x2842: switch_mode(ASCII); break; // ESC ( B
111 case 0x284A: switch_mode(JISX0201); break; // ESC ( J
112 case 0x2440: // ESC $ @
113 case 0x2442: switch_mode(JISX0208); break; // ESC $ B
124 return dec->decode_char(str, i);
126 throw logic_error("no sub-decoder");
135 void Iso2022Jp::Decoder::reset()
139 dec = new Ascii::Decoder;
142 void Iso2022Jp::Decoder::switch_mode(Mode m)
149 case ASCII: dec = new Ascii::Decoder; break;
150 case JISX0201: dec = new JisX0201::Decoder; break;
151 case JISX0208: dec = new JisX0208::Decoder; break;
155 } // namespace StringCodec