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