]> git.tdb.fi Git - libs/core.git/blob - source/jisx0201.cpp
More sophisticated error handling
[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         // Win32 has typedef unsigned short wchar_t
10         int c=c_;
11         if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E)
12                 append(c);
13         else if(c==0xA5)
14                 append(0x5C);
15         else if(c==0x203E)
16                 append(0x7E);
17         else if(c>=0xFF61 && c<=0xFF9F)
18                 append(c-0xFEC0);
19         else
20                 error("Can't express character in JIS X 0201");
21 }
22
23 void JisX0201::Decoder::decode_char(const string &str, string::const_iterator &i)
24 {
25         if(i==str.end())
26                 return;
27
28         unsigned char c=*i++;
29         if(c==0x5C)
30                 append(0xA5);
31         else if(c==0x7E)
32                 append(0x203E);
33         else if(c<=0x7F)
34                 append(c);
35         else if(c>=0xA1 && c<=0xDF)
36                 append(c+0xFEC0);
37         else
38                 error("Invalid JIS X 0201 string (undefined character)");
39 }
40
41 } // namespace Msp