Save all settings in talkfile dialog.
[maemo-rb.git] / tools / codepages.c
blobfb01c4dfb36ec52c82a0bc1528f9fc55178b15b6
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[7] = {
29 0, 1, 3, 6, 7, 8, 9
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 case 0x09: /* Western European (CP1252) */
155 while (count--) {
156 /* first convert to unicode */
157 if (*latin1 < 0x80 || *latin1 >= 0xa0)
158 ucs = *latin1++;
159 else
160 ucs = cp1252_to_uni[*latin1++ - 0x80];
162 break;
164 default:
165 break;
167 return ucs;
170 int writeshort(FILE *f, unsigned short s)
172 putc(s, f);
173 return putc(s>>8, f) != EOF;
176 void print_usage(void)
178 printf("Usage: codepages [-m]\n"
179 "\t-m Create isomini.cp only\n");
180 printf("build date: " __DATE__ "\n\n");
183 int main(int argc, char **argv)
185 int mini = 0;
186 int i, j;
187 unsigned char k;
188 unsigned short uni;
189 FILE *of;
191 for (i = 1;i < argc;i++)
193 if (argv[i][0] == '-')
195 switch (argv[i][1])
197 case 'm': /* create isomini.cp only */
198 mini = 1;
199 break;
201 case 'h': /* help */
202 case '?':
203 print_usage();
204 exit(1);
205 break;
207 default:
208 print_usage();
209 exit(1);
210 break;
215 for (i=0; i < MAX_TABLE_SIZE; i++)
216 iso_table[i] = 0;
218 if (mini) {
219 of = fopen("isomini.cp", "wb");
220 if (!of) return 1;
222 for (i=1; i<7; i++) {
224 for (j=0; j<128; j++) {
225 k = (unsigned char)j + 128;
226 uni = iso_decode(&k, mini_index[i], 1);
227 writeshort(of, uni);
230 fclose(of);
232 else {
233 of = fopen("iso.cp", "wb");
234 if (!of) return 1;
236 for (i=1; i<10; i++) {
238 for (j=0; j<128; j++) {
239 k = (unsigned char)j + 128;
240 uni = iso_decode(&k, i, 1);
241 writeshort(of, uni);
244 fclose(of);
246 of = fopen("932.cp", "wb");
247 if (!of) return 1;
248 for (i=0; i < MAX_TABLE_SIZE; i++)
249 writeshort(of, cp932_table[i]);
250 fclose(of);
252 of = fopen("936.cp", "wb");
253 if (!of) return 1;
254 for (i=0; i < MAX_TABLE_SIZE; i++)
255 writeshort(of, cp936_table[i]);
256 fclose(of);
258 of = fopen("949.cp", "wb");
259 if (!of) return 1;
260 for (i=0; i < MAX_TABLE_SIZE; i++)
261 writeshort(of, cp949_table[i]);
262 fclose(of);
264 of = fopen("950.cp", "wb");
265 if (!of) return 1;
266 for (i=0; i < MAX_TABLE_SIZE; i++)
267 writeshort(of, cp950_table[i]);
268 fclose(of);
271 return 0;