]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/jisx0201.cpp
d3fe635ce90beda61e2f99cc8aef5fc88e0794e0
[libs/core.git] / source / stringcodec / 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 namespace Codecs {
14
15 void JisX0201::Encoder::encode_char(UnicodeChar ch, string &buf)
16 {
17         if(ch>=0 && ch<=0x7F && ch!=0x5C && ch!=0x7E)
18                 buf += ch;
19         else if(ch==0xA5)
20                 buf += 0x5C;
21         else if(ch==0x203E)
22                 buf += 0x7E;
23         else if(ch>=0xFF61 && ch<=0xFF9F)
24                 buf += ch-0xFEC0;
25         else
26                 error(ch, buf, "Can't express character in JIS X 0201");
27 }
28
29 void JisX0201::Encoder::transliterate(UnicodeChar, string &buf)
30 {
31         buf += '?';
32 }
33
34
35 UnicodeChar JisX0201::Decoder::decode_char(const string &str, string::const_iterator &i)
36 {
37         if(i==str.end())
38                 return error("No input");
39
40         unsigned char ch = *i;
41         UnicodeChar result;
42         if(ch==0x5C)
43                 result = 0xA5;
44         else if(ch==0x7E)
45                 result = 0x203E;
46         else if(ch<=0x7F)
47                 result = ch;
48         else if(ch>=0xA1 && ch<=0xDF)
49                 result = ch+0xFEC0;
50         else
51                 result = error("Undefined JIS X 0201 character");
52
53         ++i;
54         return result;
55 }
56
57 } // namespace Codecs
58 } // namespace Msp