]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/jisx0208.cpp
Drop copyright and license notices from source files
[libs/core.git] / source / stringcodec / jisx0208.cpp
1 #include "jisx0208.h"
2
3 #include "jisx0208.table"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace Codecs {
9
10 void JisX0208::Encoder::encode_char(UnicodeChar ucs, string &buf)
11 {
12         unsigned short jis = ucs_to_jisx0208(ucs);
13         if(jis)
14         {
15                 char jbuf[2] = {jis>>8, jis};
16                 buf.append(jbuf, 2);
17         }
18         else
19                 error(ucs, buf, "Can't express character in JIS X 0208");
20 }
21
22 void JisX0208::Encoder::transliterate(UnicodeChar, string &buf)
23 {
24         buf.append("!)", 2);
25 }
26
27
28 UnicodeChar JisX0208::Decoder::decode_char(const string &str, string::const_iterator &i)
29 {
30         if(i==str.end())
31                 return error("No input");
32
33         string::const_iterator j = i;
34         Kuten jis;
35         jis.ku = *j++-0x20;
36
37         UnicodeChar result;
38         if(j==str.end())
39                 result = error("Incomplete JIS X 0208 character");
40         else
41         {
42                 jis.ten = *j++-0x20;
43                 result = jisx0208_to_ucs(jis);
44                 if(result==0)
45                         result = error("Undefined JIS X 0208 character");
46         }
47
48         i = j;
49         return result;
50 }
51
52
53 UnicodeChar jisx0208_to_ucs(Kuten jis)
54 {
55         if(jis.ku==0 || jis.ku>0x5E || jis.ten==0 || jis.ten>0x5E)
56                 return 0;
57
58         return jisx0208_to_ucs_table[jis.ku*94 + jis.ten - 95];
59 }
60
61 Kuten ucs_to_jisx0208(UnicodeChar c)
62 {
63         if(c<0 || c>0xFFFF)
64                 return Kuten();
65
66         unsigned i = 0;
67         for(unsigned bit=0x1000; bit; bit>>=1)
68         {
69                 if(i+bit>=ucs_to_jisx0208_table_size)
70                         continue;
71                 if(ucs_to_jisx0208_table[i+bit].ucs<=static_cast<unsigned short>(c))
72                         i += bit;
73         }
74
75         Kuten result;
76         if(ucs_to_jisx0208_table[i].ucs==static_cast<unsigned short>(c))
77         {
78                 result.ku = (ucs_to_jisx0208_table[i].jis>>8)+1;
79                 result.ten = ucs_to_jisx0208_table[i].jis+1;
80         }
81
82         return result;
83 }
84
85 } // namespace Codecs
86 } // namespace Msp