]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/iso2022jp.cpp
14a4b99f83196b835b44eba00afb51082760d37a
[libs/core.git] / source / stringcodec / iso2022jp.cpp
1 #include "ascii.h"
2 #include "iso2022jp.h"
3 #include "jisx0201.h"
4 #include "jisx0208.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace StringCodec {
10
11 void Iso2022Jp::Encoder::encode_char(unichar ch, string &buf)
12 {
13         if(ch>=0 && ch<=0x7F && ch!=0x5C && ch!=0x7E)
14         {
15                 if(mode!=ASCII && mode!=JISX0201)
16                         switch_mode(ASCII, buf);
17                 buf += ch;
18         }
19         else if(ch==0x5C || ch==0x7E)
20         {
21                 if(mode!=ASCII)
22                         switch_mode(ASCII, buf);
23                 buf += ch;
24         }
25         else if(ch==0xA5 || ch==0x203E)
26         {
27                 if(mode!=JISX0201)
28                         switch_mode(JISX0201, buf);
29                 if(ch==0xA5)
30                         buf += 0x5C;
31                 else if(ch==0x203E)
32                         buf += 0x7E;
33         }
34         else
35         {
36                 Kuten jis = ucs_to_jisx0208(ch);
37                 if(!jis)
38                         return error(ch, buf, invalid_character(ch, "ISO-2022-JP"));
39
40                 if(mode!=JISX0208)
41                         switch_mode(JISX0208, buf);
42
43                 char jbuf[2] = { jis.ku+0x20, jis.ten+0x20 };
44                 buf.append(jbuf, 2);
45         }
46 }
47
48 void Iso2022Jp::Encoder::sync(string &buf)
49 {
50         if(mode!=ASCII)
51                 switch_mode(ASCII, buf);
52 }
53
54 void Iso2022Jp::Encoder::reset()
55 {
56         mode = ASCII;
57 }
58
59 void Iso2022Jp::Encoder::switch_mode(Mode m, string &buf)
60 {
61         mode = m;
62         switch(mode)
63         {
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 invalid_argument("Iso2022Jp::Encoder::switch_mode");
68         }
69 }
70
71 void Iso2022Jp::Encoder::transliterate(unichar, string &buf)
72 {
73         if(mode!=ASCII)
74                 switch_mode(ASCII, buf);
75         buf += '?';
76 }
77
78
79 Iso2022Jp::Decoder::Decoder(ErrorMode em):
80         Codec::Decoder(em),
81         mode(ASCII),
82         dec(new Ascii::Decoder)
83 { }
84
85 unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
86 {
87         if(i==str.end())
88                 return -1;
89
90         while(i!=str.end())
91         {
92                 string::const_iterator j = i;
93
94                 unichar result = -1;
95                 if(*j==033)
96                 {
97                         unsigned escape = 0;
98                         for(++j; j!=str.end(); ++j)
99                         {
100                                 escape = escape<<8 | static_cast<unsigned char>(*j);
101                                 if(*j>='@' && *j<='Z')
102                                         break;
103                         }
104
105                         bool ok = true;
106                         switch(escape)
107                         {
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
112                         default: ok = false;
113                         }
114
115                         if(ok)
116                                 i = j;
117                         else
118                                 result = *i;
119                         ++i;
120                 }
121                 else if(dec)
122                         return dec->decode_char(str, i);
123                 else
124                         throw logic_error("no sub-decoder");
125
126                 if(result>=0)
127                         return result;
128         }
129
130         return -1;
131 }
132
133 void Iso2022Jp::Decoder::reset()
134 {
135         delete dec;
136         mode = ASCII;
137         dec = new Ascii::Decoder;
138 }
139
140 void Iso2022Jp::Decoder::switch_mode(Mode m)
141 {
142         delete dec;
143
144         mode = m;
145         switch(mode)
146         {
147         case ASCII: dec = new Ascii::Decoder; break;
148         case JISX0201: dec = new JisX0201::Decoder; break;
149         case JISX0208: dec = new JisX0208::Decoder; break;
150         }
151 }
152
153 } // namespace StringCodec
154 } // namespace Msp