Read dictionary encoding from data files.
[pspp-builds.git] / src / libpspp / i18n.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 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 <iconv.h>
25 #include <errno.h>
26 #include "assertion.h"
27
28 #include "i18n.h"
29
30 #include <localcharset.h>
31 #include "xstrndup.h"
32
33 #if HAVE_NL_LANGINFO
34 #include <langinfo.h>
35 #endif
36
37 static char *default_encoding;
38
39
40 /* A wrapper around iconv_open */
41 static iconv_t
42 create_iconv (const char* tocode, const char* fromcode)
43 {
44   iconv_t conv = iconv_open (tocode, fromcode);
45
46   /* I don't think it's safe to translate this string or to use messaging
47      as the convertors have not yet been set up */
48   if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
49     {
50       const int err = errno;
51       fprintf (stderr,
52         "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
53         fromcode, tocode, strerror (err));
54     }
55
56   return conv;
57 }
58
59 /* Return a string based on TEXT converted according to HOW.
60    If length is not -1, then it must be the number of bytes in TEXT.
61    The returned string must be freed when no longer required.
62 */
63 char *
64 recode_string (const char *to, const char *from,
65                const char *text, int length)
66 {
67   char *outbuf = 0;
68   size_t outbufferlength;
69   size_t result;
70   char *op ;
71   size_t inbytes = 0;
72   size_t outbytes ;
73   iconv_t conv ;
74
75   /* FIXME: Need to ensure that this char is valid in the target encoding */
76   const char fallbackchar = '?';
77
78   if ( text == NULL )
79     return NULL;
80
81   if ( length == -1 )
82      length = strlen(text);
83
84
85   if (to == NULL)
86     to = default_encoding;
87
88   if (from == NULL)
89     from = default_encoding;
90
91   if ( 0 == strcmp (to, from))
92     return xstrndup (text, length);
93
94   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
95     if ( outbufferlength > length)
96       break;
97
98   outbuf = xmalloc(outbufferlength);
99   op = outbuf;
100
101   outbytes = outbufferlength;
102   inbytes = length;
103
104
105   conv = create_iconv (to, from);
106
107   do {
108     const char *ip = text;
109     result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
110                    &op, &outbytes);
111
112     if ( -1 == result )
113       {
114         int the_error = errno;
115
116         switch (the_error)
117           {
118           case EILSEQ:
119           case EINVAL:
120             if ( outbytes > 0 )
121               {
122                 *op++ = fallbackchar;
123                 outbytes--;
124                 text++;
125                 inbytes--;
126                 break;
127               }
128             /* Fall through */
129           case E2BIG:
130             free (outbuf);
131             outbufferlength <<= 1;
132             outbuf = xmalloc (outbufferlength);
133             op = outbuf;
134             outbytes = outbufferlength;
135             inbytes = length;
136             text = ip;
137             break;
138           default:
139             /* should never happen */
140             NOT_REACHED ();
141             break;
142           }
143       }
144   } while ( -1 == result );
145
146
147   iconv_close (conv);
148
149   if (outbytes == 0 )
150     {
151       char *const oldaddr = outbuf;
152       outbuf = xrealloc (outbuf, outbufferlength + 1);
153
154       op += (outbuf - oldaddr) ;
155     }
156
157   *op = '\0';
158
159   return outbuf;
160 }
161
162
163
164
165 void
166 i18n_init (void)
167 {
168   free (default_encoding);
169   default_encoding = strdup (locale_charset ());
170 }
171
172
173 void
174 i18n_done (void)
175 {
176   free (default_encoding);
177   default_encoding = NULL;
178 }
179
180
181
182
183 /* Return the system local's idea of the
184    decimal seperator character */
185 char
186 get_system_decimal (void)
187 {
188   char radix_char;
189
190   char *ol = strdup (setlocale (LC_NUMERIC, NULL));
191   setlocale (LC_NUMERIC, "");
192
193 #if HAVE_NL_LANGINFO
194   radix_char = nl_langinfo (RADIXCHAR)[0];
195 #else
196   {
197     char buf[10];
198     snprintf (buf, sizeof buf, "%f", 2.5);
199     radix_char = buf[1];
200   }
201 #endif
202
203   /* We MUST leave LC_NUMERIC untouched, since it would
204      otherwise interfere with data_{in,out} */
205   setlocale (LC_NUMERIC, ol);
206   free (ol);
207   return radix_char;
208 }
209