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