]> git.tdb.fi Git - libs/core.git/blob - source/jisx0201.cpp
49f8b00240463af63b761220df1460c8c079270c
[libs/core.git] / source / jisx0201.cpp
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2006-2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #include "jisx0201.h"
9
10 using namespace std;
11
12 namespace Msp {
13
14 void JisX0201::Encoder::encode_char(wchar_t c_)
15 {
16         // Win32 has typedef unsigned short wchar_t
17         int c=c_;
18         if(c>=0 && c<=0x7F && c!=0x5C && c!=0x7E)
19                 append(c);
20         else if(c==0xA5)
21                 append(0x5C);
22         else if(c==0x203E)
23                 append(0x7E);
24         else if(c>=0xFF61 && c<=0xFF9F)
25                 append(c-0xFEC0);
26         else
27                 error("Can't express character in JIS X 0201");
28 }
29
30 void JisX0201::Decoder::decode_char(const string &str, string::const_iterator &i)
31 {
32         if(i==str.end())
33                 return;
34
35         unsigned char c=*i++;
36         if(c==0x5C)
37                 append(0xA5);
38         else if(c==0x7E)
39                 append(0x203E);
40         else if(c<=0x7F)
41                 append(c);
42         else if(c>=0xA1 && c<=0xDF)
43                 append(c+0xFEC0);
44         else
45                 error("Invalid JIS X 0201 string (undefined character)");
46 }
47
48 } // namespace Msp