Refactor locale initialisation.
[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 = strdup (locale_charset ());
202
203   hmapx_init (&map);
204 }
205
206
207 void
208 i18n_done (void)
209 {
210   struct hmapx_node *node;
211   iconv_t conv;
212   HMAPX_FOR_EACH (conv, node, &map)
213     iconv_close (conv);
214
215   hmapx_destroy (&map);
216
217   free (default_encoding);
218   default_encoding = NULL;
219 }
220
221
222
223
224 /* Return the system local's idea of the
225    decimal seperator character */
226 char
227 get_system_decimal (void)
228 {
229   char radix_char;
230
231   char *ol = strdup (setlocale (LC_NUMERIC, NULL));
232   setlocale (LC_NUMERIC, "");
233
234 #if HAVE_NL_LANGINFO
235   radix_char = nl_langinfo (RADIXCHAR)[0];
236 #else
237   {
238     char buf[10];
239     snprintf (buf, sizeof buf, "%f", 2.5);
240     radix_char = buf[1];
241   }
242 #endif
243
244   /* We MUST leave LC_NUMERIC untouched, since it would
245      otherwise interfere with data_{in,out} */
246   setlocale (LC_NUMERIC, ol);
247   free (ol);
248   return radix_char;
249 }
250