Update Hungarian translation
[evolution.git] / iconv-detect.c
blobbc7d872a1967da849a3cdc98049b0a702be8a1b1
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * This library is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Authors: Jeffrey Stedfast <fejj@ximian.com>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <iconv.h>
25 enum {
26 ISO_UNSUPPORTED = 0,
28 /* iso-8859-1 */
29 ISO_DASH_D_DASH_D_LOWER = (1 << 0),
30 ISO_DASH_D_DASH_D = (1 << 1),
31 ISO_D_DASH_D = (1 << 2),
32 ISO_D_D = (1 << 3),
33 ISO_UNDER_D_DASH_D = (1 << 4),
34 NO_ISO_D_DASH_D = (1 << 5),
36 /* iso-10646-1 */
37 /*ISO_DASH_D_DASH_D_LOWER = (1 << 0),*/
38 /*ISO_DASH_D_DASH_D = (1 << 1),*/
39 /*ISO_D_DASH_D = (1 << 2),*/
40 ISO_DASH_D_LOWER = (1 << 3),
41 ISO_DASH_D = (1 << 4),
42 ISO_D = (1 << 5),
43 UCS4 = (1 << 6),
45 /* iso-2022-jp */
46 ISO_DASH_D_DASH_S_LOWER = (1 << 0),
47 ISO_DASH_D_DASH_S = (1 << 1),
48 ISO_D_DASH_S = (1 << 2),
52 typedef struct {
53 char *charset;
54 char *format;
55 int id;
56 } CharInfo;
59 static CharInfo iso8859_tests[] = {
60 { "iso-8859-1", "iso-%d-%d", ISO_DASH_D_DASH_D_LOWER },
61 { "ISO-8859-1", "ISO-%d-%d", ISO_DASH_D_DASH_D },
62 { "ISO8859-1", "ISO%d-%d", ISO_D_DASH_D },
63 { "ISO88591", "ISO%d%d", ISO_D_D },
64 { "ISO_8859-1", "ISO_%d-%d", ISO_UNDER_D_DASH_D },
65 { "8859-1", "%d-%d", NO_ISO_D_DASH_D },
68 static int num_iso8859_tests = sizeof (iso8859_tests) / sizeof (CharInfo);
70 static CharInfo iso2022_tests[] = {
71 { "iso-2022-jp", "iso-%d-%s", ISO_DASH_D_DASH_S_LOWER },
72 { "ISO-2022-JP", "ISO-%d-%s", ISO_DASH_D_DASH_S },
73 { "ISO2022-JP", "ISO%d-%s", ISO_D_DASH_S },
76 static int num_iso2022_tests = sizeof (iso2022_tests) / sizeof (CharInfo);
78 static CharInfo iso10646_tests[] = {
79 { "iso-10646-1", "iso-%d-%d", ISO_DASH_D_DASH_D_LOWER },
80 { "ISO-10646-1", "ISO-%d-%d", ISO_DASH_D_DASH_D },
81 { "ISO10646-1", "ISO%d-%d", ISO_D_DASH_D },
82 { "iso-10646", "iso-%d", ISO_DASH_D_LOWER },
83 { "ISO-10646", "ISO-%d", ISO_DASH_D },
84 { "ISO10646", "ISO%d", ISO_D },
85 { "UCS-4BE", "UCS-4BE", UCS4 },
88 static int num_iso10646_tests = sizeof (iso10646_tests) / sizeof (CharInfo);
90 static int
91 test_iconv (void)
93 char *jp = "\x1B\x24\x42\x46\x7C\x4B\x5C\x38\x6C";
94 char *utf8 = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E";
95 char *transbuf = malloc (10), *trans = transbuf;
96 iconv_t cd;
97 size_t jp_len = strlen (jp), utf8_len = 10;
98 size_t utf8_real_len = strlen (utf8);
100 cd = iconv_open ("UTF-8", "ISO-2022-JP");
101 if (cd == (iconv_t) -1)
102 return 0;
104 if (iconv (cd, &jp, &jp_len, &trans, &utf8_len) == -1 || jp_len != 0) {
105 iconv_close (cd);
106 return 0;
108 if (memcmp (utf8, transbuf, utf8_real_len) != 0) {
109 iconv_close (cd);
110 return 0;
113 iconv_close (cd);
115 return 1;
119 main (int argc,
120 char **argv)
122 unsigned int iso8859, iso2022, iso10646;
123 CharInfo *info;
124 iconv_t cd;
125 FILE *fp;
126 int i;
128 if (!test_iconv ())
129 return 1;
131 fp = fopen ("iconv-detect.h", "w");
132 if (fp == NULL)
133 return 255;
135 fprintf (fp, "/* This is an auto-generated header, DO NOT EDIT! */\n\n");
137 iso8859 = ISO_UNSUPPORTED;
138 info = iso8859_tests;
139 /*printf ("#define DEFAULT_ISO_FORMAT(iso,codepage)\t");*/
140 for (i = 0; i < num_iso8859_tests; i++) {
141 cd = iconv_open (info[i].charset, "UTF-8");
142 if (cd != (iconv_t) -1) {
143 iconv_close (cd);
144 /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
145 fprintf (stderr, "System prefers %s\n", info[i].charset);
146 iso8859 = info[i].id;
147 break;
151 if (iso8859 == ISO_UNSUPPORTED) {
152 fprintf (stderr, "System doesn't support any ISO-8859-1 formats\n");
153 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[0].format);
154 } else {
155 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[i].format);
158 iso2022 = ISO_UNSUPPORTED;
159 info = iso2022_tests;
160 /*printf ("#define ISO_2022_FORMAT(iso,codepage)\t");*/
161 for (i = 0; i < num_iso2022_tests; i++) {
162 cd = iconv_open (info[i].charset, "UTF-8");
163 if (cd != (iconv_t) -1) {
164 iconv_close (cd);
165 /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
166 fprintf (stderr, "System prefers %s\n", info[i].charset);
167 iso2022 = info[i].id;
168 break;
172 if (iso2022 == ISO_UNSUPPORTED) {
173 fprintf (stderr, "System doesn't support any ISO-2022 formats\n");
174 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[0].format);
175 } else {
176 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[i].format);
179 iso10646 = ISO_UNSUPPORTED;
180 info = iso10646_tests;
181 /*printf ("#define ISO_10646_FORMAT(iso,codepage)\t");*/
182 for (i = 0; i < num_iso10646_tests; i++) {
183 cd = iconv_open (info[i].charset, "UTF-8");
184 if (cd != (iconv_t) -1) {
185 iconv_close (cd);
186 /*if (info[i].id < ISO_DASH_D_LOWER)
187 printf ("(\"%s\", (iso), (codepage))\n", info[i].format);
188 else
189 printf ("(\"%s\", (iso))\n", info[i].format);*/
190 fprintf (stderr, "System prefers %s\n", info[i].charset);
191 iso10646 = info[i].id;
192 break;
196 /* we don't need a printf format for iso-10646 because there is only 1 */
197 if (iso10646 == ISO_UNSUPPORTED) {
198 fprintf (stderr, "System doesn't support any ISO-10646-1 formats\n");
199 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[0].charset);
200 } else {
201 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[i].charset);
204 fclose (fp);
206 return 0;