Prevent file descriptor leakage or overuse
[Rockbox.git] / tools / codepages.c
blob07d95111de47c057dfac4e9d5da37630d454d672
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 const int mini_index[6] = {
27 0, 1, 3, 6, 7, 8
30 static unsigned short iso_table[MAX_TABLE_SIZE];
32 unsigned short iso_decode(unsigned char *latin1, int cp, int count)
34 unsigned short ucs = 0;
36 /* cp tells us which codepage to convert from */
37 switch (cp) {
38 case 0x01: /* Greek (ISO-8859-7) */
39 while (count--) {
40 /* first convert to unicode */
41 if (*latin1 < 0xA1)
42 ucs = *latin1++;
43 else if (*latin1 > 0xB7)
44 ucs = *latin1++ + 0x02D0;
45 else
46 ucs = iso8859_7_to_uni[*latin1++ - 0xA1];
48 break;
50 case 0x02: /* Hebrew (ISO-8859-8) */
51 while (count--) {
52 /* first convert to unicode */
53 if (*latin1 == 0xAA) {
54 ucs = 0xD7;
55 latin1++;
56 } else if (*latin1 == 0xBA) {
57 ucs = 0xF7;
58 latin1++;
59 } else if (*latin1 == 0xDF) {
60 ucs = 0x2017;
61 latin1++;
62 } else if (*latin1 < 0xC0)
63 ucs = *latin1++;
64 else
65 ucs = *latin1++ + 0x04F0;
67 break;
69 case 0x03: /* Cyrillic (CP1251) */
70 while (count--) {
71 /* first convert to unicode */
72 if (*latin1 < 0x80)
73 ucs = *latin1++;
74 else if (*latin1 > 0xBF)
75 ucs = *latin1++ + 0x0350;
76 else
77 ucs = cp1251_to_uni[*latin1++ - 0x80];
79 break;
81 case 0x04: /* Thai (ISO-8859-11) */
82 while (count--) {
83 /* first convert to unicode */
84 if (*latin1 < 0xA1)
85 ucs = *latin1++;
86 else
87 ucs = *latin1++ + 0x0D60;
89 break;
91 case 0x05: /* Arabic (CP1256) */
92 while (count--) {
93 /* first convert to unicode */
94 if (*latin1 < 0x80)
95 ucs = *latin1++;
96 else
97 ucs = cp1256_to_uni[*latin1++ - 0x80];
99 break;
101 case 0x06: /* Turkish (ISO-8859-9) */
102 while (count--) {
103 /* first convert to unicode */
104 switch (*latin1) {
105 case 0xD0:
106 ucs = 0x011E;
107 break;
108 case 0xDD:
109 ucs = 0x0130;
110 break;
111 case 0xDE:
112 ucs = 0x015E;
113 break;
114 case 0xF0:
115 ucs = 0x011F;
116 break;
117 case 0xFD:
118 ucs = 0x0131;
119 break;
120 case 0xFE:
121 ucs = 0x015F;
122 break;
123 default:
124 ucs = *latin1;
125 break;
128 latin1++;
130 break;
132 case 0x07: /* Latin Extended (ISO-8859-2) */
133 while (count--) {
134 /* first convert to unicode */
135 if (*latin1 < 0xA1)
136 ucs = *latin1++;
137 else
138 ucs = iso8859_2_to_uni[*latin1++ - 0xA1];
140 break;
142 case 0x08: /* Central European (CP1250) */
143 while (count--) {
144 /* first convert to unicode */
145 if (*latin1 < 0x80)
146 ucs = *latin1++;
147 else
148 ucs = cp1250_to_uni[*latin1++ - 0x80];
150 break;
152 default:
153 break;
155 return ucs;
158 int writeshort(FILE *f, unsigned short s)
160 putc(s, f);
161 return putc(s>>8, f) != EOF;
164 void print_usage(void)
166 printf("Usage: codepages [-m]\n"
167 "\t-m Create isomini.cp only\n");
168 printf("build date: " __DATE__ "\n\n");
171 int main(int argc, char **argv)
173 int mini = 0;
174 int i, j;
175 unsigned char k;
176 unsigned short uni;
177 FILE *of;
179 for (i = 1;i < argc;i++)
181 if (argv[i][0] == '-')
183 switch (argv[i][1])
185 case 'm': /* create isomini.cp only */
186 mini = 1;
187 break;
189 case 'h': /* help */
190 case '?':
191 print_usage();
192 exit(1);
193 break;
195 default:
196 print_usage();
197 exit(1);
198 break;
203 for (i=0; i < MAX_TABLE_SIZE; i++)
204 iso_table[i] = 0;
206 if (mini) {
207 of = fopen("isomini.cp", "wb");
208 if (!of) return 1;
210 for (i=1; i<6; i++) {
212 for (j=0; j<128; j++) {
213 k = (unsigned char)j + 128;
214 uni = iso_decode(&k, mini_index[i], 1);
215 writeshort(of, uni);
218 fclose(of);
220 else {
221 of = fopen("iso.cp", "wb");
222 if (!of) return 1;
224 for (i=1; i<9; i++) {
226 for (j=0; j<128; j++) {
227 k = (unsigned char)j + 128;
228 uni = iso_decode(&k, i, 1);
229 writeshort(of, uni);
232 fclose(of);
234 of = fopen("932.cp", "wb");
235 if (!of) return 1;
236 for (i=0; i < MAX_TABLE_SIZE; i++)
237 writeshort(of, cp932_table[i]);
238 fclose(of);
240 of = fopen("936.cp", "wb");
241 if (!of) return 1;
242 for (i=0; i < MAX_TABLE_SIZE; i++)
243 writeshort(of, cp936_table[i]);
244 fclose(of);
246 of = fopen("949.cp", "wb");
247 if (!of) return 1;
248 for (i=0; i < MAX_TABLE_SIZE; i++)
249 writeshort(of, cp949_table[i]);
250 fclose(of);
252 of = fopen("950.cp", "wb");
253 if (!of) return 1;
254 for (i=0; i < MAX_TABLE_SIZE; i++)
255 writeshort(of, cp950_table[i]);
256 fclose(of);
259 return 0;