3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
17 void Iso2022Jp::Encoder::encode_char(wchar_t c_)
19 // Win32 has typedef unsigned short wchar_t
22 if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E)
24 if(mode!=ASCII && mode!=JISX0201)
28 else if(c==0x5C || c==0x7E)
34 else if(c==0xA5 || c==0x203E)
37 switch_mode(JISX0201);
45 unsigned short jis=ucs_to_jisx0208(c);
47 error("Can't express character in ISO-2022-JP");
51 switch_mode(JISX0208);
53 char buf[2]={jis>>8, jis};
59 void Iso2022Jp::Encoder::sync()
65 void Iso2022Jp::Encoder::switch_mode(Mode m)
70 case ASCII: append("\033(B", 3); break;
71 case JISX0201: append("\033(J", 3); break;
72 case JISX0208: append("\033$B", 3); break;
76 void Iso2022Jp::Encoder::append_replacement()
83 Iso2022Jp::Decoder::Decoder(ErrorMode em):
84 StringCodec::Decoder(em),
86 dec(new Ascii::Decoder),
90 void Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
96 escape=escape<<8 | static_cast<unsigned char>(*i);
97 if(*i>='@' && *i<='Z')
101 case 0x1B2842: switch_mode(ASCII); break; // ESC ( B
102 case 0x1B284A: switch_mode(JISX0201); break; // ESC ( J
103 case 0x1B2440: // ESC $ @
104 case 0x1B2442: switch_mode(JISX0208); break; // ESC $ B
105 default: error("Invalid ISO-2022-JP escape sequence");
118 dec->decode_char(str, i);
124 void Iso2022Jp::Decoder::sync()
128 error("Sync in middle of ISO-2022-JP escape sequence");
134 error("Sync while not in ASCII mode");
144 void Iso2022Jp::Decoder::switch_mode(Mode m)
152 case ASCII: dec=new Ascii::Decoder; break;
153 case JISX0201: dec=new JisX0201::Decoder; break;
154 case JISX0208: dec=new JisX0208::Decoder; break;