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