Convert to utf8 in data_out function.
[pspp-builds.git] / src / libpspp / i18n.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <xalloc.h>
19 #include <assert.h>
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <libintl.h>
25 #include <iconv.h>
26 #include <errno.h>
27 #include <relocatable.h>
28 #include "assertion.h"
29 #include "hmapx.h"
30 #include "hash-functions.h"
31 #include "pool.h"
32
33 #include "i18n.h"
34
35 #include "version.h"
36
37 #include <localcharset.h>
38 #include "xstrndup.h"
39
40 #if HAVE_NL_LANGINFO
41 #include <langinfo.h>
42 #endif
43
44 struct converter
45   {
46     const char *tocode;
47     const char *fromcode;
48     iconv_t conv;
49   };
50
51 static char *default_encoding;
52 static struct hmapx map;
53
54 /* A wrapper around iconv_open */
55 static iconv_t
56 create_iconv (const char* tocode, const char* fromcode)
57 {
58   size_t hash;
59   struct hmapx_node *node;
60   struct converter *converter;
61   assert (fromcode);
62
63   hash = hash_string (tocode, hash_string (fromcode, 0));
64   HMAPX_FOR_EACH_WITH_HASH (converter, node, hash, &map)
65     if (!strcmp (tocode, converter->tocode)
66         && !strcmp (fromcode, converter->fromcode))
67       return converter->conv;
68
69   converter = xmalloc (sizeof *converter);
70   converter->tocode = xstrdup (tocode);
71   converter->fromcode = xstrdup (fromcode);
72   converter->conv = iconv_open (tocode, fromcode);
73   hmapx_insert (&map, converter, hash);
74
75   /* I don't think it's safe to translate this string or to use messaging
76      as the convertors have not yet been set up */
77   if ( (iconv_t) -1 == converter->conv && 0 != strcmp (tocode, fromcode))
78     {
79       const int err = errno;
80       fprintf (stderr,
81                "Warning: "
82                "cannot create a convertor for \"%s\" to \"%s\": %s\n",
83                fromcode, tocode, strerror (err));
84     }
85
86   return converter->conv;
87 }
88
89 char *
90 recode_string (const char *to, const char *from,
91                const char *text, int length)
92 {
93   return recode_string_pool (to, from, text, length, NULL);
94 }
95
96
97 /* Return a string based on TEXT converted according to HOW.
98    If length is not -1, then it must be the number of bytes in TEXT.
99    The returned string must be freed when no longer required.
100 */
101 char *
102 recode_string_pool (const char *to, const char *from,
103                const char *text, int length, struct pool *pool)
104 {
105   char *outbuf = 0;
106   size_t outbufferlength;
107   size_t result;
108   char *op ;
109   size_t inbytes = 0;
110   size_t outbytes ;
111   iconv_t conv ;
112
113   /* FIXME: Need to ensure that this char is valid in the target encoding */
114   const char fallbackchar = '?';
115
116   if ( text == NULL )
117     return NULL;
118
119   if ( length == -1 )
120      length = strlen(text);
121
122
123   if (to == NULL)
124     to = default_encoding;
125
126   if (from == NULL)
127     from = default_encoding;
128
129   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
130     if ( outbufferlength > length)
131       break;
132
133   outbuf = pool_malloc (pool, outbufferlength);
134   op = outbuf;
135
136   outbytes = outbufferlength;
137   inbytes = length;
138
139
140   conv = create_iconv (to, from);
141
142   if ( (iconv_t) -1 == conv )
143         return xstrdup (text);
144
145   do {
146     const char *ip = text;
147     result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
148                    &op, &outbytes);
149
150     if ( -1 == result )
151       {
152         int the_error = errno;
153
154         switch (the_error)
155           {
156           case EILSEQ:
157           case EINVAL:
158             if ( outbytes > 0 )
159               {
160                 *op++ = fallbackchar;
161                 outbytes--;
162                 text++;
163                 inbytes--;
164                 break;
165               }
166             /* Fall through */
167           case E2BIG:
168             free (outbuf);
169             outbufferlength <<= 1;
170             outbuf = pool_malloc (pool, outbufferlength);
171             op = outbuf;
172             outbytes = outbufferlength;
173             inbytes = length;
174             text = ip;
175             break;
176           default:
177             /* should never happen */
178             fprintf (stderr, "Character conversion error: %s\n", strerror (the_error));
179             NOT_REACHED ();
180             break;
181           }
182       }
183   } while ( -1 == result );
184
185   if (outbytes == 0 )
186     {
187       char *const oldaddr = outbuf;
188       outbuf = pool_realloc (pool, outbuf, outbufferlength + 1);
189
190       op += (outbuf - oldaddr) ;
191     }
192
193   *op = '\0';
194
195   return outbuf;
196 }
197
198
199 void
200 i18n_init (void)
201 {
202 #if ENABLE_NLS
203   setlocale (LC_CTYPE, "");
204 #ifdef LC_MESSAGES
205   setlocale (LC_MESSAGES, "");
206 #endif
207 #if HAVE_LC_PAPER
208   setlocale (LC_PAPER, "");
209 #endif
210   bindtextdomain (PACKAGE, relocate(locale_dir));
211   textdomain (PACKAGE);
212 #endif /* ENABLE_NLS */
213
214   assert (default_encoding == NULL);
215   default_encoding = xstrdup (locale_charset ());
216
217   hmapx_init (&map);
218 }
219
220
221 const char *
222 get_default_encoding (void)
223 {
224   return default_encoding;
225 }
226
227 void
228 set_default_encoding (const char *enc)
229 {
230   free (default_encoding);
231   default_encoding = xstrdup (enc);
232 }
233
234
235 /* Attempts to set the encoding from a locale name
236    returns true if successfull.
237    This function does not (should not!) alter the current locale.
238 */
239 bool
240 set_encoding_from_locale (const char *loc)
241 {
242   bool ok = true;
243   char *c_encoding;
244   char *loc_encoding;
245   char *tmp = xstrdup (setlocale (LC_CTYPE, NULL));
246
247   setlocale (LC_CTYPE, "C");
248   c_encoding = xstrdup (locale_charset ());
249
250   setlocale (LC_CTYPE, loc);
251   loc_encoding = xstrdup (locale_charset ());
252
253
254   if ( 0 == strcmp (loc_encoding, c_encoding))
255     {
256       ok = false;
257     }
258
259
260   setlocale (LC_CTYPE, tmp);
261
262   free (tmp);
263
264   if (ok)
265     {
266       free (default_encoding);
267       default_encoding = loc_encoding;
268     }
269   else
270     free (loc_encoding);
271
272   free (c_encoding);
273
274   return ok;
275 }
276
277 void
278 i18n_done (void)
279 {
280   struct hmapx_node *node;
281   struct converter *cvtr;
282   HMAPX_FOR_EACH (cvtr, node, &map)
283     {
284       iconv_close (cvtr->conv);
285       free (cvtr);
286     }
287
288   hmapx_destroy (&map);
289
290   free (default_encoding);
291   default_encoding = NULL;
292 }
293
294
295
296 bool
297 valid_encoding (const char *enc)
298 {
299   iconv_t conv = iconv_open ("UTF8", enc);
300
301   if ( conv == (iconv_t) -1)
302     return false;
303
304   iconv_close (conv);
305
306   return true;
307 }
308
309
310 /* Return the system local's idea of the
311    decimal seperator character */
312 char
313 get_system_decimal (void)
314 {
315   char radix_char;
316
317   char *ol = xstrdup (setlocale (LC_NUMERIC, NULL));
318   setlocale (LC_NUMERIC, "");
319
320 #if HAVE_NL_LANGINFO
321   radix_char = nl_langinfo (RADIXCHAR)[0];
322 #else
323   {
324     char buf[10];
325     snprintf (buf, sizeof buf, "%f", 2.5);
326     radix_char = buf[1];
327   }
328 #endif
329
330   /* We MUST leave LC_NUMERIC untouched, since it would
331      otherwise interfere with data_{in,out} */
332   setlocale (LC_NUMERIC, ol);
333   free (ol);
334   return radix_char;
335 }
336