10 void Iso2022Jp::Encoder::encode_char(wchar_t c)
12 if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E)
14 if(mode!=ASCII && mode!=JISX0201)
18 else if(c==0x5C || c==0x7E)
24 else if(c==0xA5 || c==0x203E)
27 switch_mode(JISX0201);
35 unsigned short jis=ucs_to_jisx0208(c);
37 throw CodecError("Can't express character in ISO-2022-JP");
39 switch_mode(JISX0208);
43 void Iso2022Jp::Encoder::switch_mode(Mode m)
48 case ASCII: append("\033(B", 3); break;
49 case JISX0201: append("\033(J", 3); break;
50 case JISX0208: append("\033$B", 3); break;
54 Iso2022Jp::Decoder::Decoder():
56 dec(new Ascii::Decoder),
60 void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
66 escape=escape<<8 | (unsigned char)*i;
67 if(*i>='@' && *i<='Z')
71 case 0x1B2842: switch_mode(ASCII); break; // ESC ( B
72 case 0x1B284A: switch_mode(JISX0201); break; // ESC ( J
73 case 0x1B2440: // ESC $ @
74 case 0x1B2442: switch_mode(JISX0208); break; // ESC $ B
75 default: throw CodecError("Invalid ISO-2022-JP escape sequence");
88 dec->decode_char(str, i);
94 void Iso2022Jp::Decoder::sync()
97 throw CodecError("Sync in middle of ISO-2022-JP escape sequence");
99 throw CodecError("Sync while not in ASCII mode");
104 void Iso2022Jp::Decoder::switch_mode(Mode m)
112 case ASCII: dec=new Ascii::Decoder; break;
113 case JISX0201: dec=new JisX0201::Decoder; break;
114 case JISX0208: dec=new JisX0208::Decoder; break;