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);
41 char buf[2]={jis>>8, jis};
46 void Iso2022Jp::Encoder::sync()
52 void Iso2022Jp::Encoder::switch_mode(Mode m)
57 case ASCII: append("\033(B", 3); break;
58 case JISX0201: append("\033(J", 3); break;
59 case JISX0208: append("\033$B", 3); break;
63 Iso2022Jp::Decoder::Decoder():
65 dec(new Ascii::Decoder),
69 void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
75 escape=escape<<8 | static_cast<unsigned char>(*i);
76 if(*i>='@' && *i<='Z')
80 case 0x1B2842: switch_mode(ASCII); break; // ESC ( B
81 case 0x1B284A: switch_mode(JISX0201); break; // ESC ( J
82 case 0x1B2440: // ESC $ @
83 case 0x1B2442: switch_mode(JISX0208); break; // ESC $ B
84 default: throw CodecError("Invalid ISO-2022-JP escape sequence");
97 dec->decode_char(str, i);
103 void Iso2022Jp::Decoder::sync()
106 throw CodecError("Sync in middle of ISO-2022-JP escape sequence");
108 throw CodecError("Sync while not in ASCII mode");
113 void Iso2022Jp::Decoder::switch_mode(Mode m)
121 case ASCII: dec=new Ascii::Decoder; break;
122 case JISX0201: dec=new JisX0201::Decoder; break;
123 case JISX0208: dec=new JisX0208::Decoder; break;