added iconv format checker.
[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 2002 Ximian, Inc. (www.ximian.com)
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <iconv.h>
25
26 enum {
27         ISO_UNSUPPORTED          = 0,
28         
29         /* iso-8859-1 */
30         ISO_DASH_D_DASH_D_LOWER  = (1 << 0),
31         ISO_DASH_D_DASH_D        = (1 << 1),
32         ISO_D_DASH_D             = (1 << 2),
33         ISO_D_D                  = (1 << 3),
34         ISO_UNDER_D_DASH_D       = (1 << 4),
35         NO_ISO_D_DASH_D          = (1 << 5),
36         
37         /* iso-10646-1 */
38         /*ISO_DASH_D_DASH_D_LOWER  = (1 << 0),*/
39         /*ISO_DASH_D_DASH_D        = (1 << 1),*/
40         /*ISO_D_DASH_D             = (1 << 2),*/
41         ISO_DASH_D_LOWER           = (1 << 3),
42         ISO_DASH_D                 = (1 << 4),
43         ISO_D                      = (1 << 5),
44         UCS4                       = (1 << 6),
45         
46         /* iso-2022-jp */
47         ISO_DASH_D_DASH_S_LOWER  = (1 << 0),
48         ISO_DASH_D_DASH_S        = (1 << 1),
49         ISO_D_DASH_S             = (1 << 2),
50 };
51
52
53 typedef struct {
54         char *charset;
55         char *format;
56         int id;
57 } CharInfo;
58
59
60 static CharInfo iso8859_tests[] = {
61         { "iso-8859-1",  "iso-%d-%d", ISO_DASH_D_DASH_D_LOWER },
62         { "ISO-8859-1",  "ISO-%d-%d", ISO_DASH_D_DASH_D },
63         { "ISO8859-1",   "ISO%d-%d",  ISO_D_DASH_D },
64         { "ISO88591",    "ISO%d%d",   ISO_D_D },
65         { "ISO_8859-1",  "ISO_%d-%d", ISO_UNDER_D_DASH_D },
66         { "8859-1",      "%d-%d",     NO_ISO_D_DASH_D },
67 };
68
69 static int num_iso8859_tests = sizeof (iso8859_tests) / sizeof (CharInfo);
70
71 static CharInfo iso2022_tests[] = {
72         { "iso-2022-jp", "iso-%d-%s", ISO_DASH_D_DASH_S_LOWER },
73         { "ISO-2022-JP", "ISO-%d-%s", ISO_DASH_D_DASH_S },
74         { "ISO2022-JP",  "ISO%d-%s",  ISO_D_DASH_S },
75 };
76
77 static int num_iso2022_tests = sizeof (iso2022_tests) / sizeof (CharInfo);
78
79 static CharInfo iso10646_tests[] = {
80         { "iso-10646-1", "iso-%d-%d",  ISO_DASH_D_DASH_D_LOWER },
81         { "ISO-10646-1", "ISO-%d-%d",  ISO_DASH_D_DASH_D },
82         { "ISO10646-1",  "ISO%d-%d",   ISO_D_DASH_D },
83         { "iso-10646",   "iso-%d",     ISO_DASH_D_LOWER },
84         { "ISO-10646",   "ISO-%d",     ISO_DASH_D },
85         { "ISO10646",    "ISO%d",      ISO_D },
86         { "UCS-4BE",     "UCS-4BE",    UCS4 },
87 };
88
89 static int num_iso10646_tests = sizeof (iso10646_tests) / sizeof (CharInfo);
90
91
92 int main (int argc, char **argv)
93 {
94         unsigned int bits, iso8859, iso2022, iso10646;
95         CharInfo *info;
96         iconv_t cd;
97         FILE *fp;
98         int i;
99         
100         fp = fopen ("iconv-detect.h", "w");
101         if (fp == NULL)
102                 exit (255);
103         
104         fprintf (fp, "/* This is an auto-generated header, DO NOT EDIT! */\n\n");
105         
106         iso8859 = ISO_UNSUPPORTED;
107         info = iso8859_tests;
108         /*printf ("#define DEFAULT_ISO_FORMAT(iso,codepage)\t");*/
109         for (i = 0; i < num_iso8859_tests; i++) {
110                 cd = iconv_open (info[i].charset, "UTF-8");
111                 if (cd != (iconv_t) -1) {
112                         iconv_close (cd);
113                         /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
114                         fprintf (stderr, "System prefers %s\n", info[i].charset);
115                         iso8859 = info[i].id;
116                         break;
117                 }
118         }
119         
120         if (iso8859 == ISO_UNSUPPORTED) {
121                 fprintf (stderr, "System doesn't support any ISO-8859-1 formats\n");
122                 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[0].format);
123 #ifdef CONFIGURE_IN
124                 exit (1);
125 #endif
126         } else {
127                 fprintf (fp, "#define ICONV_ISO_D_FORMAT \"%s\"\n", info[i].format);
128         }
129         
130         iso2022 = ISO_UNSUPPORTED;
131         info = iso2022_tests;
132         /*printf ("#define ISO_2022_FORMAT(iso,codepage)\t");*/
133         for (i = 0; i < num_iso2022_tests; i++) {
134                 cd = iconv_open (info[i].charset, "UTF-8");
135                 if (cd != (iconv_t) -1) {
136                         iconv_close (cd);
137                         /*printf ("(\"%s\", (iso), (codepage))\n", info[i].format);*/
138                         fprintf (stderr, "System prefers %s\n", info[i].charset);
139                         iso2022 = info[i].id;
140                         break;
141                 }
142         }
143         
144         if (iso2022 == ISO_UNSUPPORTED) {
145                 fprintf (stderr, "System doesn't support any ISO-2022 formats\n");
146                 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[0].format);
147 #ifdef CONFIGURE_IN
148                 exit (3);
149 #endif
150         } else {
151                 fprintf (fp, "#define ICONV_ISO_S_FORMAT \"%s\"\n", info[i].format);
152         }
153         
154         iso10646 = ISO_UNSUPPORTED;
155         info = iso10646_tests;
156         /*printf ("#define ISO_10646_FORMAT(iso,codepage)\t");*/
157         for (i = 0; i < num_iso10646_tests; i++) {
158                 cd = iconv_open (info[i].charset, "UTF-8");
159                 if (cd != (iconv_t) -1) {
160                         iconv_close (cd);
161                         /*if (info[i].id < ISO_DASH_D_LOWER)
162                                 printf ("(\"%s\", (iso), (codepage))\n", info[i].format);
163                         else
164                         printf ("(\"%s\", (iso))\n", info[i].format);*/
165                         fprintf (stderr, "System prefers %s\n", info[i].charset);
166                         iso10646 = info[i].id;
167                         break;
168                 }
169         }
170         
171         /* we don't need a printf format for iso-10646 because there is only 1 */
172         if (iso10646 == ISO_UNSUPPORTED) {
173                 fprintf (stderr, "System doesn't support any ISO-10646-1 formats\n");
174                 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[0].charset);
175 #ifdef CONFIGURE_IN
176                 exit (2);
177 #endif
178         } else {
179                 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[i].charset);
180         }
181         
182         fclose (fp);
183         
184         exit (0);
185 }