updated changelog
[platform/upstream/evolution-data-server.git] / iconv-detect.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6  *
7  * This library is free software you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <iconv.h>
24
25 enum {
26         ISO_UNSUPPORTED          = 0,
27
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),
35
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),
44
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),
49 };
50
51
52 typedef struct {
53         char *charset;
54         char *format;
55         int id;
56 } CharInfo;
57
58
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 },
66 };
67
68 static int num_iso8859_tests = sizeof (iso8859_tests) / sizeof (CharInfo);
69
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 },
74 };
75
76 static int num_iso2022_tests = sizeof (iso2022_tests) / sizeof (CharInfo);
77
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 },
86 };
87
88 static int num_iso10646_tests = sizeof (iso10646_tests) / sizeof (CharInfo);
89
90
91 int main (int argc, char **argv)
92 {
93         unsigned int iso8859, iso2022, iso10646;
94         CharInfo *info;
95         iconv_t cd;
96         FILE *fp;
97         int i;
98
99         fp = fopen ("iconv-detect.h", "w");
100         if (fp == NULL)
101                 exit (255);
102
103         fprintf (fp, "/* This is an auto-generated header, DO NOT EDIT! */\n\n");
104
105         iso8859 = ISO_UNSUPPORTED;
106         info = iso8859_tests;
107         /*printf ("#define DEFAULT_ISO_FORMAT(iso,codepage)\t");*/
108         for (i = 0; i < num_iso8859_tests; i++) {
109                 cd = iconv_open (info[i].charset, "UTF-8");
110                 if (cd != (iconv_t) -1) {
111                         iconv_close (cd);
112                         /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
113                         fprintf (stderr, "System prefers %s\n", info[i].charset);
114                         iso8859 = info[i].id;
115                         break;
116                 }
117         }
118
119         if (iso8859 == ISO_UNSUPPORTED) {
120                 fprintf (stderr, "System doesn't support any ISO-8859-1 formats\n");
121                 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[0].format);
122 #ifdef CONFIGURE_IN
123                 exit (1);
124 #endif
125         } else {
126                 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[i].format);
127         }
128
129         iso2022 = ISO_UNSUPPORTED;
130         info = iso2022_tests;
131         /*printf ("#define ISO_2022_FORMAT(iso,codepage)\t");*/
132         for (i = 0; i < num_iso2022_tests; i++) {
133                 cd = iconv_open (info[i].charset, "UTF-8");
134                 if (cd != (iconv_t) -1) {
135                         iconv_close (cd);
136                         /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
137                         fprintf (stderr, "System prefers %s\n", info[i].charset);
138                         iso2022 = info[i].id;
139                         break;
140                 }
141         }
142
143         if (iso2022 == ISO_UNSUPPORTED) {
144                 fprintf (stderr, "System doesn't support any ISO-2022 formats\n");
145                 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[0].format);
146 #ifdef CONFIGURE_IN
147                 exit (3);
148 #endif
149         } else {
150                 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[i].format);
151         }
152
153         iso10646 = ISO_UNSUPPORTED;
154         info = iso10646_tests;
155         /*printf ("#define ISO_10646_FORMAT(iso,codepage)\t");*/
156         for (i = 0; i < num_iso10646_tests; i++) {
157                 cd = iconv_open (info[i].charset, "UTF-8");
158                 if (cd != (iconv_t) -1) {
159                         iconv_close (cd);
160                         /*if (info[i].id < ISO_DASH_D_LOWER)
161                                 printf ("(\"%s\", (iso), (codepage))\n", info[i].format);
162                         else
163                         printf ("(\"%s\", (iso))\n", info[i].format);*/
164                         fprintf (stderr, "System prefers %s\n", info[i].charset);
165                         iso10646 = info[i].id;
166                         break;
167                 }
168         }
169
170         /* we don't need a printf format for iso-10646 because there is only 1 */
171         if (iso10646 == ISO_UNSUPPORTED) {
172                 fprintf (stderr, "System doesn't support any ISO-10646-1 formats\n");
173                 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[0].charset);
174 #ifdef CONFIGURE_IN
175                 exit (2);
176 #endif
177         } else {
178                 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[i].charset);
179         }
180
181         fclose (fp);
182
183         exit (0);
184 }