]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/iso2022jp.cpp
Fix warnings about certain narrowing conversions not being allowed in C++11
[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];
44                 jbuf[0] = jis.ku+0x20;
45                 jbuf[1] = jis.ten+0x20;
46                 buf.append(jbuf, 2);
47         }
48 }
49
50 void Iso2022Jp::Encoder::sync(string &buf)
51 {
52         if(mode!=ASCII)
53                 switch_mode(ASCII, buf);
54 }
55
56 void Iso2022Jp::Encoder::reset()
57 {
58         mode = ASCII;
59 }
60
61 void Iso2022Jp::Encoder::switch_mode(Mode m, string &buf)
62 {
63         mode = m;
64         switch(mode)
65         {
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");
70         }
71 }
72
73 void Iso2022Jp::Encoder::transliterate(unichar, string &buf)
74 {
75         if(mode!=ASCII)
76                 switch_mode(ASCII, buf);
77         buf += '?';
78 }
79
80
81 Iso2022Jp::Decoder::Decoder(ErrorMode em):
82         Codec::Decoder(em),
83         mode(ASCII),
84         dec(new Ascii::Decoder)
85 { }
86
87 unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterator &i)
88 {
89         if(i==str.end())
90                 return -1;
91
92         while(i!=str.end())
93         {
94                 string::const_iterator j = i;
95
96                 unichar result = -1;
97                 if(*j==033)
98                 {
99                         unsigned escape = 0;
100                         for(++j; j!=str.end(); ++j)
101                         {
102                                 escape = escape<<8 | static_cast<unsigned char>(*j);
103                                 if(*j>='@' && *j<='Z')
104                                         break;
105                         }
106
107                         bool ok = true;
108                         switch(escape)
109                         {
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
114                         default: ok = false;
115                         }
116
117                         if(ok)
118                                 i = j;
119                         else
120                                 result = *i;
121                         ++i;
122                 }
123                 else if(dec)
124                         return dec->decode_char(str, i);
125                 else
126                         throw logic_error("no sub-decoder");
127
128                 if(result>=0)
129                         return result;
130         }
131
132         return -1;
133 }
134
135 void Iso2022Jp::Decoder::reset()
136 {
137         delete dec;
138         mode = ASCII;
139         dec = new Ascii::Decoder;
140 }
141
142 void Iso2022Jp::Decoder::switch_mode(Mode m)
143 {
144         delete dec;
145
146         mode = m;
147         switch(mode)
148         {
149         case ASCII: dec = new Ascii::Decoder; break;
150         case JISX0201: dec = new JisX0201::Decoder; break;
151         case JISX0208: dec = new JisX0208::Decoder; break;
152         }
153 }
154
155 } // namespace StringCodec
156 } // namespace Msp