Fix typo in the comment
[maemo-rb.git] / tools / codepages.c
blob9c214397de9db61ae8de1ab917516d69d8a9ad94
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
10 * Copyright (C) 2005 by Frank Dischner
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "codepage_tables.h"
26 #define MAX_TABLE_SIZE 32768
28 static const int mini_index[6] = {
29 0, 1, 3, 6, 7, 8
32 static unsigned short iso_table[MAX_TABLE_SIZE];
34 unsigned short iso_decode(unsigned char *latin1, int cp, int count)
36 unsigned short ucs = 0;
38 /* cp tells us which codepage to convert from */
39 switch (cp) {
40 case 0x01: /* Greek (ISO-8859-7) */
41 while (count--) {
42 /* first convert to unicode */
43 if (*latin1 < 0xA1)
44 ucs = *latin1++;
45 else if (*latin1 > 0xB7)
46 ucs = *latin1++ + 0x02D0;
47 else
48 ucs = iso8859_7_to_uni[*latin1++ - 0xA1];
50 break;
52 case 0x02: /* Hebrew (ISO-8859-8) */
53 while (count--) {
54 /* first convert to unicode */
55 if (*latin1 == 0xAA) {
56 ucs = 0xD7;
57 latin1++;
58 } else if (*latin1 == 0xBA) {
59 ucs = 0xF7;
60 latin1++;
61 } else if (*latin1 == 0xDF) {
62 ucs = 0x2017;
63 latin1++;
64 } else if (*latin1 < 0xC0)
65 ucs = *latin1++;
66 else
67 ucs = *latin1++ + 0x04F0;
69 break;
71 case 0x03: /* Cyrillic (CP1251) */
72 while (count--) {
73 /* first convert to unicode */
74 if (*latin1 < 0x80)
75 ucs = *latin1++;
76 else if (*latin1 > 0xBF)
77 ucs = *latin1++ + 0x0350;
78 else
79 ucs = cp1251_to_uni[*latin1++ - 0x80];
81 break;
83 case 0x04: /* Thai (ISO-8859-11) */
84 while (count--) {
85 /* first convert to unicode */
86 if (*latin1 < 0xA1)
87 ucs = *latin1++;
88 else
89 ucs = *latin1++ + 0x0D60;
91 break;
93 case 0x05: /* Arabic (CP1256) */
94 while (count--) {
95 /* first convert to unicode */
96 if (*latin1 < 0x80)
97 ucs = *latin1++;
98 else
99 ucs = cp1256_to_uni[*latin1++ - 0x80];
101 break;
103 case 0x06: /* Turkish (ISO-8859-9) */
104 while (count--) {
105 /* first convert to unicode */
106 switch (*latin1) {
107 case 0xD0:
108 ucs = 0x011E;
109 break;
110 case 0xDD:
111 ucs = 0x0130;
112 break;
113 case 0xDE:
114 ucs = 0x015E;
115 break;
116 case 0xF0:
117 ucs = 0x011F;
118 break;
119 case 0xFD:
120 ucs = 0x0131;
121 break;
122 case 0xFE:
123 ucs = 0x015F;
124 break;
125 default:
126 ucs = *latin1;
127 break;
130 latin1++;
132 break;
134 case 0x07: /* Latin Extended (ISO-8859-2) */
135 while (count--) {
136 /* first convert to unicode */
137 if (*latin1 < 0xA1)
138 ucs = *latin1++;
139 else
140 ucs = iso8859_2_to_uni[*latin1++ - 0xA1];
142 break;
144 case 0x08: /* Central European (CP1250) */
145 while (count--) {
146 /* first convert to unicode */
147 if (*latin1 < 0x80)
148 ucs = *latin1++;
149 else
150 ucs = cp1250_to_uni[*latin1++ - 0x80];
152 break;
154 default:
155 break;
157 return ucs;
160 int writeshort(FILE *f, unsigned short s)
162 putc(s, f);
163 return putc(s>>8, f) != EOF;
166 void print_usage(void)
168 printf("Usage: codepages [-m]\n"
169 "\t-m Create isomini.cp only\n");
170 printf("build date: " __DATE__ "\n\n");
173 int main(int argc, char **argv)
175 int mini = 0;
176 int i, j;
177 unsigned char k;
178 unsigned short uni;
179 FILE *of;
181 for (i = 1;i < argc;i++)
183 if (argv[i][0] == '-')
185 switch (argv[i][1])
187 case 'm': /* create isomini.cp only */
188 mini = 1;
189 break;
191 case 'h': /* help */
192 case '?':
193 print_usage();
194 exit(1);
195 break;
197 default:
198 print_usage();
199 exit(1);
200 break;
205 for (i=0; i < MAX_TABLE_SIZE; i++)
206 iso_table[i] = 0;
208 if (mini) {
209 of = fopen("isomini.cp", "wb");
210 if (!of) return 1;
212 for (i=1; i<6; i++) {
214 for (j=0; j<128; j++) {
215 k = (unsigned char)j + 128;
216 uni = iso_decode(&k, mini_index[i], 1);
217 writeshort(of, uni);
220 fclose(of);
222 else {
223 of = fopen("iso.cp", "wb");
224 if (!of) return 1;
226 for (i=1; i<9; i++) {
228 for (j=0; j<128; j++) {
229 k = (unsigned char)j + 128;
230 uni = iso_decode(&k, i, 1);
231 writeshort(of, uni);
234 fclose(of);
236 of = fopen("932.cp", "wb");
237 if (!of) return 1;
238 for (i=0; i < MAX_TABLE_SIZE; i++)
239 writeshort(of, cp932_table[i]);
240 fclose(of);
242 of = fopen("936.cp", "wb");
243 if (!of) return 1;
244 for (i=0; i < MAX_TABLE_SIZE; i++)
245 writeshort(of, cp936_table[i]);
246 fclose(of);
248 of = fopen("949.cp", "wb");
249 if (!of) return 1;
250 for (i=0; i < MAX_TABLE_SIZE; i++)
251 writeshort(of, cp949_table[i]);
252 fclose(of);
254 of = fopen("950.cp", "wb");
255 if (!of) return 1;
256 for (i=0; i < MAX_TABLE_SIZE; i++)
257 writeshort(of, cp950_table[i]);
258 fclose(of);
261 return 0;