Add "x" prefix to calls to plain malloc(), calloc(), strdup(), realloc().
[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 "assertion.h"
28 #include "hmapx.h"
29 #include "hash-functions.h"
30
31 #include "i18n.h"
32
33 #include "version.h"
34
35 #include <localcharset.h>
36 #include "xstrndup.h"
37
38 #if HAVE_NL_LANGINFO
39 #include <langinfo.h>
40 #endif
41
42 static char *default_encoding;
43 static struct hmapx map;
44
45 /* A wrapper around iconv_open */
46 static iconv_t
47 create_iconv (const char* tocode, const char* fromcode)
48 {
49   iconv_t conv;
50   struct hmapx_node *node;
51   size_t hash ;
52   char *key = alloca (strlen (tocode) + strlen (fromcode) + 2);
53
54   strcpy (key, tocode);
55   strcat (key, "\n"); /* hopefully no encoding names contain '\n' */
56   strcat (key, fromcode);
57
58   hash = hsh_hash_string (key);
59
60   node = hmapx_first_with_hash (&map, hash);
61
62   if (!node)
63     {
64       conv = iconv_open (tocode, fromcode);
65
66       /* I don't think it's safe to translate this string or to use messaging
67          as the convertors have not yet been set up */
68       if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
69         {
70           const int err = errno;
71           fprintf (stderr,
72                    "Warning: "
73                    "cannot create a convertor for \"%s\" to \"%s\": %s\n",
74                    fromcode, tocode, strerror (err));
75         }
76
77       hmapx_insert (&map, conv, hash);
78     }
79   else
80     {
81       conv = hmapx_node_data (node);
82     }
83
84   return 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   do {
133     const char *ip = text;
134     result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
135                    &op, &outbytes);
136
137     if ( -1 == result )
138       {
139         int the_error = errno;
140
141         switch (the_error)
142           {
143           case EILSEQ:
144           case EINVAL:
145             if ( outbytes > 0 )
146               {
147                 *op++ = fallbackchar;
148                 outbytes--;
149                 text++;
150                 inbytes--;
151                 break;
152               }
153             /* Fall through */
154           case E2BIG:
155             free (outbuf);
156             outbufferlength <<= 1;
157             outbuf = xmalloc (outbufferlength);
158             op = outbuf;
159             outbytes = outbufferlength;
160             inbytes = length;
161             text = ip;
162             break;
163           default:
164             /* should never happen */
165             NOT_REACHED ();
166             break;
167           }
168       }
169   } while ( -1 == result );
170
171   if (outbytes == 0 )
172     {
173       char *const oldaddr = outbuf;
174       outbuf = xrealloc (outbuf, outbufferlength + 1);
175
176       op += (outbuf - oldaddr) ;
177     }
178
179   *op = '\0';
180
181   return outbuf;
182 }
183
184
185 void
186 i18n_init (void)
187 {
188 #if ENABLE_NLS
189   setlocale (LC_CTYPE, "");
190 #if HAVE_LC_MESSAGES
191   setlocale (LC_MESSAGES, "");
192 #endif
193 #if HAVE_LC_PAPER
194   setlocale (LC_PAPER, "");
195 #endif
196   bindtextdomain (PACKAGE, locale_dir);
197   textdomain (PACKAGE);
198 #endif /* ENABLE_NLS */
199
200   assert (default_encoding == NULL);
201   default_encoding = xstrdup (locale_charset ());
202
203   hmapx_init (&map);
204 }
205
206
207 const char *
208 get_default_encoding (void)
209 {
210   return default_encoding;
211 }
212
213 void
214 set_default_encoding (const char *enc)
215 {
216   free (default_encoding);
217   default_encoding = xstrdup (enc);
218 }
219
220
221 /* Attempts to set the encoding from a locale name
222    returns true if successfull.
223    This function does not (should not!) alter the current locale.
224 */
225 bool
226 set_encoding_from_locale (const char *loc)
227 {
228   bool ok = true;
229   char *c_encoding;
230   char *loc_encoding;
231   char *tmp = xstrdup (setlocale (LC_CTYPE, NULL));
232
233   setlocale (LC_CTYPE, "C");
234   c_encoding = xstrdup (locale_charset ());
235
236   setlocale (LC_CTYPE, loc);
237   loc_encoding = xstrdup (locale_charset ());
238
239
240   if ( 0 == strcmp (loc_encoding, c_encoding))
241     {
242       ok = false;
243     }
244
245
246   setlocale (LC_CTYPE, tmp);
247
248   free (tmp);
249
250   if (ok)
251     {
252       free (default_encoding);
253       default_encoding = loc_encoding;
254     }
255   else
256     free (loc_encoding);
257
258   free (c_encoding);
259
260   return ok;
261 }
262
263 void
264 i18n_done (void)
265 {
266   struct hmapx_node *node;
267   iconv_t conv;
268   HMAPX_FOR_EACH (conv, node, &map)
269     iconv_close (conv);
270
271   hmapx_destroy (&map);
272
273   free (default_encoding);
274   default_encoding = NULL;
275 }
276
277
278
279 bool
280 valid_encoding (const char *enc)
281 {
282   iconv_t conv = iconv_open ("UTF8", enc);
283
284   if ( conv == (iconv_t) -1)
285     return false;
286
287   iconv_close (conv);
288
289   return true;
290 }
291
292
293 /* Return the system local's idea of the
294    decimal seperator character */
295 char
296 get_system_decimal (void)
297 {
298   char radix_char;
299
300   char *ol = xstrdup (setlocale (LC_NUMERIC, NULL));
301   setlocale (LC_NUMERIC, "");
302
303 #if HAVE_NL_LANGINFO
304   radix_char = nl_langinfo (RADIXCHAR)[0];
305 #else
306   {
307     char buf[10];
308     snprintf (buf, sizeof buf, "%f", 2.5);
309     radix_char = buf[1];
310   }
311 #endif
312
313   /* We MUST leave LC_NUMERIC untouched, since it would
314      otherwise interfere with data_{in,out} */
315   setlocale (LC_NUMERIC, ol);
316   free (ol);
317   return radix_char;
318 }
319