simplify a bit.
[Rockbox.git] / tools / codepages.c
blob651a99c429099e930027324bf5d1421a3b5a6ea6
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[5] = {
27 0, 1, 3, 6, 7
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 default:
143 break;
145 return ucs;
148 int writeshort(FILE *f, unsigned short s)
150 putc(s, f);
151 return putc(s>>8, f) != EOF;
154 void print_usage(void)
156 printf("Usage: codepages [-m]\n"
157 "\t-m Create isomini.cp only\n");
158 printf("build date: " __DATE__ "\n\n");
161 int main(int argc, char **argv)
163 int mini = 0;
164 int i, j;
165 unsigned char k;
166 unsigned short uni;
167 FILE *of;
169 for (i = 1;i < argc;i++)
171 if (argv[i][0] == '-')
173 switch (argv[i][1])
175 case 'm': /* create isomini.cp only */
176 mini = 1;
177 break;
179 case 'h': /* help */
180 case '?':
181 print_usage();
182 exit(1);
183 break;
185 default:
186 print_usage();
187 exit(1);
188 break;
193 for (i=0; i < MAX_TABLE_SIZE; i++)
194 iso_table[i] = 0;
196 if (mini) {
197 of = fopen("isomini.cp", "wb");
198 if (!of) return 1;
200 for (i=1; i<5; i++) {
202 for (j=0; j<128; j++) {
203 k = (unsigned char)j + 128;
204 uni = iso_decode(&k, mini_index[i], 1);
205 writeshort(of, uni);
208 fclose(of);
210 else {
211 of = fopen("iso.cp", "wb");
212 if (!of) return 1;
214 for (i=1; i<8; i++) {
216 for (j=0; j<128; j++) {
217 k = (unsigned char)j + 128;
218 uni = iso_decode(&k, i, 1);
219 writeshort(of, uni);
222 fclose(of);
224 of = fopen("932.cp", "wb");
225 if (!of) return 1;
226 for (i=0; i < MAX_TABLE_SIZE; i++)
227 writeshort(of, cp932_table[i]);
228 fclose(of);
230 of = fopen("936.cp", "wb");
231 if (!of) return 1;
232 for (i=0; i < MAX_TABLE_SIZE; i++)
233 writeshort(of, cp936_table[i]);
234 fclose(of);
236 of = fopen("949.cp", "wb");
237 if (!of) return 1;
238 for (i=0; i < MAX_TABLE_SIZE; i++)
239 writeshort(of, cp949_table[i]);
240 fclose(of);
242 of = fopen("950.cp", "wb");
243 if (!of) return 1;
244 for (i=0; i < MAX_TABLE_SIZE; i++)
245 writeshort(of, cp950_table[i]);
246 fclose(of);
249 return 0;