OSDN Git Service

Original character encoding converter is implemented for cp932.
[fukui-no-namari/dialektos.git] / src / convert / cp932_table.hxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
7  * Dialektos is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dialektos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dialektos.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef CP932_TABLE_HXX
22 #define CP932_TABLE_HXX
23
24 namespace dialektos {
25
26 namespace convert {
27
28
29 struct CP932Table {
30   CP932Table();
31   unsigned short to_bmp(unsigned char ch) {
32     if (ch >= 0xa1 && ch <= 0xdf)
33       return codemap_a1[index_a1(ch)];
34     return 0;
35   }
36   unsigned short to_bmp(unsigned char ch1, unsigned char ch2) {
37     if (ch1 >= 0x81 && ch1 <= 0x9f) {
38       const int index = index_81(ch1, ch2);
39       return index == -1 ? 0 : codemap_81[index];
40     } else if (ch1 >= 0xe0 && ch1 <= 0xea) {
41       const int index = index_e0(ch1, ch2);
42       return index == -1 ? 0 : codemap_e0[index];
43     } else if (ch1 >= 0xed && ch1 <= 0xee) {
44       const int index = index_ed(ch1, ch2);
45       return index == -1 ? 0 : codemap_ed[index];
46     }
47     return 0;
48   }
49 private:
50   int index_81(unsigned char ku, unsigned char ten) {
51     if (ku >= 0x81 && ku <= 0x9f && ten >= 0x40 && ten <= 0xfc) {
52       // from 1 ku to 62 ku.
53       return (ku-0x81)*(0xfc-0x40+1) + (ten-0x40);
54     }
55     return -1;
56   }
57
58   int index_e0(unsigned char ku, unsigned char ten) {
59     if (ku >= 0xe0 && ku <= 0xea && ten >= 0x40 && ten <= 0xfc) {
60       // from 63 ku to 84 ku.
61       return (ku-0xe0)*(0xfc-0x40+1) + (ten-0x40);
62     }
63     return -1;
64   }
65
66   int index_ed(unsigned char ku, unsigned char ten) {
67     if (ku >= 0xed && ku <= 0xee && ten >= 0x40 && ten <= 0xfc) {
68       // from 63 ku to 84 ku.
69       return (ku-0xed)*(0xfc-0x40+1) + (ten-0x40);
70     }
71     return -1;
72   }
73
74   int index_a1(unsigned char ch) {
75     if (ch >= 0xa1 && ch <= 0xdf) {
76       return ch - 0xa1;
77     }
78     return -1;
79   }
80
81   static const unsigned short codemap_81[(0xa0-0x81)*(0xfd-0x40)];
82   static const unsigned short codemap_e0[(0xeb-0xe0)*(0xfd-0x40)];
83   static const unsigned short codemap_ed[(0xef-0xed)*(0xfd-0x40)];
84   static const unsigned short codemap_a1[0xe0-0xa1];
85 };
86
87
88 } // namespace convert
89
90 } // namespace dialektos
91
92 #endif