Initialize the gmime for upstream
[platform/upstream/gmime.git] / iconv-detect.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 1999-2012 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <iconv.h>
25
26 enum {
27         ISO_UNSUPPORTED          = 0,
28         
29         /* iso-8859-1 */
30         ISO_DASH_UINT_DASH_UINT_LOWER  = (1 << 0),
31         ISO_DASH_UINT_DASH_UINT        = (1 << 1),
32         ISO_UINT_DASH_UINT             = (1 << 2),
33         ISO_UINT_UINT                  = (1 << 3),
34         ISO_UNDER_UINT_DASH_UINT       = (1 << 4),
35         NO_ISO_UINT_DASH_UINT          = (1 << 5),
36         
37         /* iso-10646-1 */
38         /*ISO_DASH_UINT_DASH_UINT_LOWER  = (1 << 0),*/
39         /*ISO_DASH_UINT_DASH_UINT        = (1 << 1),*/
40         /*ISO_UINT_DASH_UINT             = (1 << 2),*/
41         ISO_DASH_UINT_LOWER            = (1 << 3),
42         ISO_DASH_UINT                  = (1 << 4),
43         ISO_UINT                       = (1 << 5),
44         UCS4                           = (1 << 6),
45         
46         /* iso-2022-jp */
47         ISO_DASH_UINT_DASH_STR_LOWER   = (1 << 0),
48         ISO_DASH_UINT_DASH_STR         = (1 << 1),
49         ISO_UINT_DASH_STR              = (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-%u-%u", ISO_DASH_UINT_DASH_UINT_LOWER },
62         { "ISO-8859-1",  "ISO-%u-%u", ISO_DASH_UINT_DASH_UINT },
63         { "ISO8859-1",   "ISO%u-%u",  ISO_UINT_DASH_UINT },
64         { "ISO88591",    "ISO%u%u",   ISO_UINT_UINT },
65         { "ISO_8859-1",  "ISO_%u-%u", ISO_UNDER_UINT_DASH_UINT },
66         { "8859-1",      "%u-%u",     NO_ISO_UINT_DASH_UINT },
67 };
68
69 static int num_iso8859_tests = sizeof (iso8859_tests) / sizeof (CharInfo);
70
71 static CharInfo iso2022_tests[] = {
72         { "iso-2022-jp", "iso-%u-%s", ISO_DASH_UINT_DASH_STR_LOWER },
73         { "ISO-2022-JP", "ISO-%u-%s", ISO_DASH_UINT_DASH_STR },
74         { "ISO2022-JP",  "ISO%u-%s",  ISO_UINT_DASH_STR },
75 };
76
77 static int num_iso2022_tests = sizeof (iso2022_tests) / sizeof (CharInfo);
78
79 static CharInfo iso10646_tests[] = {
80         { "iso-10646-1", "iso-%u-%u",  ISO_DASH_UINT_DASH_UINT_LOWER },
81         { "ISO-10646-1", "ISO-%u-%u",  ISO_DASH_UINT_DASH_UINT },
82         { "ISO10646-1",  "ISO%u-%u",   ISO_UINT_DASH_UINT },
83         { "iso-10646",   "iso-%u",     ISO_DASH_UINT_LOWER },
84         { "ISO-10646",   "ISO-%u",     ISO_DASH_UINT },
85         { "ISO10646",    "ISO%u",      ISO_UINT },
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 iso8859, iso2022, iso10646;
95         CharInfo *info;
96         iconv_t cd;
97         FILE *fp;
98         int i;
99         
100         if (!(fp = fopen ("iconv-detect.h", "w")))
101                 return EXIT_FAILURE;
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_INT_FORMAT \"%s\"\n", info[0].format);
122 #ifdef CONFIGURE_IN
123                 return EXIT_FAILURE;
124 #endif
125         } else {
126                 fprintf (fp, "#define ICONV_ISO_INT_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_STR_FORMAT \"%s\"\n", info[0].format);
146 #ifdef CONFIGURE_IN
147                 return EXIT_FAILURE;
148 #endif
149         } else {
150                 fprintf (fp, "#define ICONV_ISO_STR_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_UINT_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                 return EXIT_FAILURE;
176 #endif
177         } else {
178                 fprintf (fp, "#define ICONV_10646 \"%s\"\n", info[i].charset);
179         }
180         
181         fclose (fp);
182         
183         return EXIT_SUCCESS;
184 }