Initial upload
[libs/core.git] / source / jisx0201.cpp
1 #include "jisx0201.h"
2
3 using namespace std;
4
5 namespace Msp {
6
7 void JisX0201::Encoder::encode_char(wchar_t c)
8 {
9         if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E)
10                 append(c);
11         else if(c==0xA5)
12                 append(0x5C);
13         else if(c==0x203E)
14                 append(0x7E);
15         else if(c>=0xFF61 && c<=0xFF9F)
16                 append(c-0xFEC0);
17         else
18                 throw CodecError("Can't express character in JIS X 0201");
19 }
20
21 void JisX0201::Decoder::decode_char(const string &str, string::const_iterator &i)
22 {
23         if(i==str.end())
24                 return;
25
26         unsigned char c=*i++;
27         if(c==0x5C)
28                 append(0xA5);
29         else if(c==0x7E)
30                 append(0x203E);
31         else if(c<=0x7F)
32                 append(c);
33         else if(c>=0xA1 && c<=0xDF)
34                 append(c+0xFEC0);
35         else
36                 throw CodecError("Invalid JIS X 0201 string (undefined character)");
37 }
38
39 } // namespace Msp