43056c8b7431b7e7f22da99c11d459380b411880
[pspp-builds.git] / src / libpspp / i18n.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <xalloc.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <iconv.h>
26 #include <errno.h>
27 #include "assertion.h"
28
29 #include "i18n.h"
30
31 #include <localcharset.h>
32 #include "xstrndup.h"
33
34
35 static char *locale = 0;
36 static const char *charset;
37
38
39 static iconv_t convertor[n_CONV];
40
41
42 /* A wrapper around iconv_open */
43 static iconv_t
44 create_iconv (const char* tocode, const char* fromcode)
45 {
46   iconv_t conv = iconv_open (tocode, fromcode);
47
48   /* I don't think it's safe to translate this string or to use messaging
49      as the convertors have not yet been set up */
50   if ( (iconv_t) -1 == conv)
51     {
52       const int err = errno;
53       fprintf (stderr,
54         "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
55         fromcode, tocode, strerror (err));
56     }
57
58   return conv;
59 }
60
61 /* Return a string based on TEXT converted according to HOW.
62    If length is not -1, then it must be the number of bytes in TEXT.
63    The returned string must be freed when no longer required.
64 */
65 char *
66 recode_string (enum conv_id how,  const char *text, int length)
67 {
68   char *outbuf = 0;
69   size_t outbufferlength;
70   size_t result;
71   char *op ;
72   size_t inbytes = 0;
73   size_t outbytes ;
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   assert (how < n_CONV);
85
86   if (convertor[how] == (iconv_t) -1)
87     return xstrndup (text, length);
88
89   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
90     if ( outbufferlength > length)
91       break;
92
93   outbuf = xmalloc(outbufferlength);
94   op = outbuf;
95
96   outbytes = outbufferlength;
97   inbytes = length;
98
99   do {
100     const char *ip = text;
101     result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
102                    &op, &outbytes);
103
104     if ( -1 == result )
105       {
106         int the_error = errno;
107
108         switch (the_error)
109           {
110           case EILSEQ:
111           case EINVAL:
112             if ( outbytes > 0 )
113               {
114                 *op++ = fallbackchar;
115                 outbytes--;
116                 text++;
117                 inbytes--;
118                 break;
119               }
120             /* Fall through */
121           case E2BIG:
122             free (outbuf);
123             outbufferlength <<= 1;
124             outbuf = xmalloc (outbufferlength);
125             op = outbuf;
126             outbytes = outbufferlength;
127             inbytes = length;
128             text = ip;
129             break;
130           default:
131             /* should never happen */
132             NOT_REACHED ();
133             break;
134           }
135       }
136   } while ( -1 == result );
137
138   if (outbytes == 0 )
139     {
140       char *const oldaddr = outbuf;
141       outbuf = xrealloc (outbuf, outbufferlength + 1);
142
143       op += (outbuf - oldaddr) ;
144     }
145
146   *op = '\0';
147
148   return outbuf;
149 }
150
151
152 /* Returns the current PSPP locale */
153 const char *
154 get_pspp_locale (void)
155 {
156   assert (locale);
157   return locale;
158 }
159
160 /* Set the PSPP locale */
161 void
162 set_pspp_locale (const char *l)
163 {
164   char *current_locale;
165   const char *current_charset;
166
167   free(locale);
168   locale = strdup(l);
169
170   current_locale = setlocale (LC_CTYPE, 0);
171   current_charset = locale_charset ();
172   setlocale (LC_CTYPE, locale);
173
174   charset = locale_charset ();
175   setlocale (LC_CTYPE, current_locale);
176
177   iconv_close (convertor[CONV_PSPP_TO_UTF8]);
178   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
179
180   iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
181   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
182 }
183
184 void
185 i18n_init (void)
186 {
187   assert (!locale) ;
188   locale = strdup (setlocale (LC_CTYPE, NULL));
189
190   setlocale (LC_CTYPE, locale);
191   charset = locale_charset ();
192
193   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
194   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
195 }
196
197
198 void
199 i18n_done (void)
200 {
201   int i;
202   free (locale);
203   locale = 0;
204
205   for(i = 0 ; i < n_CONV; ++i )
206     {
207       if ( (iconv_t) -1 == convertor[i] )
208         continue;
209       iconv_close (convertor[i]);
210     }
211 }
212