Last POSIX fix of the day. I think I'll never make that mistake again.
[Rockbox.git] / tools / codepages.c
blobe19d39c85a4568fa05e33b25d4fee17d75f5d690
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
10 * Copyright (C) 2005 by Frank Dischner
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "codepage_tables.h"
24 #define MAX_TABLE_SIZE 32768
26 static unsigned short iso_table[MAX_TABLE_SIZE];
28 unsigned short iso_decode(unsigned char *latin1, int cp, int count)
30 unsigned short ucs = 0;
32 /* cp tells us which codepage to convert from */
33 switch (cp) {
34 case 0x01: /* Greek (ISO-8859-7) */
35 while (count--) {
36 /* first convert to unicode */
37 if (*latin1 < 0xA1)
38 ucs = *latin1++;
39 else if (*latin1 > 0xB7)
40 ucs = *latin1++ + 0x02D0;
41 else
42 ucs = iso8859_7_to_uni[*latin1++ - 0xA1];
44 break;
46 case 0x02: /* Hebrew (ISO-8859-8) */
47 while (count--) {
48 /* first convert to unicode */
49 if (*latin1 == 0xAA) {
50 ucs = 0xD7;
51 latin1++;
52 } else if (*latin1 == 0xBA) {
53 ucs = 0xF7;
54 latin1++;
55 } else if (*latin1 == 0xDF) {
56 ucs = 0x2017;
57 latin1++;
58 } else if (*latin1 < 0xC0)
59 ucs = *latin1++;
60 else
61 ucs = *latin1++ + 0x04F0;
63 break;
65 case 0x03: /* Cyrillic (CP1251) */
66 while (count--) {
67 /* first convert to unicode */
68 if (*latin1 < 0x80)
69 ucs = *latin1++;
70 else if (*latin1 > 0xBF)
71 ucs = *latin1++ + 0x0350;
72 else
73 ucs = cp1251_to_uni[*latin1++ - 0x80];
75 break;
77 case 0x04: /* Thai (ISO-8859-11) */
78 while (count--) {
79 /* first convert to unicode */
80 if (*latin1 < 0xA1)
81 ucs = *latin1++;
82 else
83 ucs = *latin1++ + 0x0D60;
85 break;
87 case 0x05: /* Arabic (CP1256) */
88 while (count--) {
89 /* first convert to unicode */
90 if (*latin1 < 0x80)
91 ucs = *latin1++;
92 else
93 ucs = cp1256_to_uni[*latin1++ - 0x80];
95 break;
97 case 0x06: /* Turkish (ISO-8859-9) */
98 while (count--) {
99 /* first convert to unicode */
100 switch (*latin1) {
101 case 0xD0:
102 ucs = 0x011E;
103 break;
104 case 0xDD:
105 ucs = 0x0130;
106 break;
107 case 0xDE:
108 ucs = 0x015E;
109 break;
110 case 0xF0:
111 ucs = 0x011F;
112 break;
113 case 0xFD:
114 ucs = 0x0131;
115 break;
116 case 0xFE:
117 ucs = 0x015F;
118 break;
119 default:
120 ucs = *latin1;
121 break;
124 latin1++;
126 break;
128 case 0x07: /* Latin Extended (ISO-8859-2) */
129 while (count--) {
130 /* first convert to unicode */
131 if (*latin1 < 0xA1)
132 ucs = *latin1++;
133 else
134 ucs = iso8859_2_to_uni[*latin1++ - 0xA1];
136 break;
138 default:
139 break;
141 return ucs;
144 int writeshort(FILE *f, unsigned short s)
146 putc(s, f);
147 return putc(s>>8, f) != EOF;
150 int main(void)
153 int i, j;
154 unsigned char k;
155 unsigned short uni;
156 FILE *of;
158 for (i=0; i < MAX_TABLE_SIZE; i++)
159 iso_table[i] = 0;
161 of = fopen("iso.cp", "wb");
162 if (!of) return 1;
164 for (i=1; i<8; i++) {
166 for (j=0; j<128; j++) {
167 k = (unsigned char)j + 128;
168 uni = iso_decode(&k, i, 1);
169 writeshort(of, uni);
172 fclose(of);
174 of = fopen("932.cp", "wb");
175 if (!of) return 1;
176 for (i=0; i < MAX_TABLE_SIZE; i++)
177 writeshort(of, cp932_table[i]);
178 fclose(of);
180 of = fopen("936.cp", "wb");
181 if (!of) return 1;
182 for (i=0; i < MAX_TABLE_SIZE; i++)
183 writeshort(of, cp936_table[i]);
184 fclose(of);
186 of = fopen("949.cp", "wb");
187 if (!of) return 1;
188 for (i=0; i < MAX_TABLE_SIZE; i++)
189 writeshort(of, cp949_table[i]);
190 fclose(of);
192 of = fopen("950.cp", "wb");
193 if (!of) return 1;
194 for (i=0; i < MAX_TABLE_SIZE; i++)
195 writeshort(of, cp950_table[i]);
196 fclose(of);
198 return 0;