Convert encoding even if encodings are identical.
[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 <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   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
92     if ( outbufferlength > length)
93       break;
94
95   outbuf = xmalloc(outbufferlength);
96   op = outbuf;
97
98   outbytes = outbufferlength;
99   inbytes = length;
100
101
102   conv = create_iconv (to, from);
103
104   do {
105     const char *ip = text;
106     result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
107                    &op, &outbytes);
108
109     if ( -1 == result )
110       {
111         int the_error = errno;
112
113         switch (the_error)
114           {
115           case EILSEQ:
116           case EINVAL:
117             if ( outbytes > 0 )
118               {
119                 *op++ = fallbackchar;
120                 outbytes--;
121                 text++;
122                 inbytes--;
123                 break;
124               }
125             /* Fall through */
126           case E2BIG:
127             free (outbuf);
128             outbufferlength <<= 1;
129             outbuf = xmalloc (outbufferlength);
130             op = outbuf;
131             outbytes = outbufferlength;
132             inbytes = length;
133             text = ip;
134             break;
135           default:
136             /* should never happen */
137             NOT_REACHED ();
138             break;
139           }
140       }
141   } while ( -1 == result );
142
143
144   iconv_close (conv);
145
146   if (outbytes == 0 )
147     {
148       char *const oldaddr = outbuf;
149       outbuf = xrealloc (outbuf, outbufferlength + 1);
150
151       op += (outbuf - oldaddr) ;
152     }
153
154   *op = '\0';
155
156   return outbuf;
157 }
158
159
160
161
162 void
163 i18n_init (void)
164 {
165   free (default_encoding);
166   default_encoding = strdup (locale_charset ());
167 }
168
169
170 void
171 i18n_done (void)
172 {
173   free (default_encoding);
174   default_encoding = NULL;
175 }
176
177
178
179
180 /* Return the system local's idea of the
181    decimal seperator character */
182 char
183 get_system_decimal (void)
184 {
185   char radix_char;
186
187   char *ol = strdup (setlocale (LC_NUMERIC, NULL));
188   setlocale (LC_NUMERIC, "");
189
190 #if HAVE_NL_LANGINFO
191   radix_char = nl_langinfo (RADIXCHAR)[0];
192 #else
193   {
194     char buf[10];
195     snprintf (buf, sizeof buf, "%f", 2.5);
196     radix_char = buf[1];
197   }
198 #endif
199
200   /* We MUST leave LC_NUMERIC untouched, since it would
201      otherwise interfere with data_{in,out} */
202   setlocale (LC_NUMERIC, ol);
203   free (ol);
204   return radix_char;
205 }
206