Change license from GPLv2+ to GPLv3+.
[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 <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <iconv.h>
24 #include <errno.h>
25 #include "assertion.h"
26
27 #include "i18n.h"
28
29 #include <localcharset.h>
30 #include "xstrndup.h"
31
32
33 static char *locale = 0;
34 static const char *charset;
35
36
37 static iconv_t convertor[n_CONV];
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)
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 (enum conv_id how,  const char *text, int length)
65 {
66   char *outbuf = 0;
67   size_t outbufferlength;
68   size_t result;
69   char *op ;
70   size_t inbytes = 0;
71   size_t outbytes ;
72
73   /* FIXME: Need to ensure that this char is valid in the target encoding */
74   const char fallbackchar = '?';
75
76   if ( text == NULL )
77     return NULL;
78
79   if ( length == -1 )
80      length = strlen(text);
81
82   assert (how < n_CONV);
83
84   if (convertor[how] == (iconv_t) -1)
85     return xstrndup (text, length);
86
87   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
88     if ( outbufferlength > length)
89       break;
90
91   outbuf = xmalloc(outbufferlength);
92   op = outbuf;
93
94   outbytes = outbufferlength;
95   inbytes = length;
96
97   do {
98     const char *ip = text;
99     result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
100                    &op, &outbytes);
101
102     if ( -1 == result )
103       {
104         int the_error = errno;
105
106         switch (the_error)
107           {
108           case EILSEQ:
109           case EINVAL:
110             if ( outbytes > 0 )
111               {
112                 *op++ = fallbackchar;
113                 outbytes--;
114                 text++;
115                 inbytes--;
116                 break;
117               }
118             /* Fall through */
119           case E2BIG:
120             free (outbuf);
121             outbufferlength <<= 1;
122             outbuf = xmalloc (outbufferlength);
123             op = outbuf;
124             outbytes = outbufferlength;
125             inbytes = length;
126             text = ip;
127             break;
128           default:
129             /* should never happen */
130             NOT_REACHED ();
131             break;
132           }
133       }
134   } while ( -1 == result );
135
136   if (outbytes == 0 )
137     {
138       char *const oldaddr = outbuf;
139       outbuf = xrealloc (outbuf, outbufferlength + 1);
140
141       op += (outbuf - oldaddr) ;
142     }
143
144   *op = '\0';
145
146   return outbuf;
147 }
148
149
150 /* Returns the current PSPP locale */
151 const char *
152 get_pspp_locale (void)
153 {
154   assert (locale);
155   return locale;
156 }
157
158 /* Set the PSPP locale */
159 void
160 set_pspp_locale (const char *l)
161 {
162   char *current_locale;
163   const char *current_charset;
164
165   free(locale);
166   locale = strdup(l);
167
168   current_locale = setlocale (LC_CTYPE, 0);
169   current_charset = locale_charset ();
170   setlocale (LC_CTYPE, locale);
171
172   charset = locale_charset ();
173   setlocale (LC_CTYPE, current_locale);
174
175   iconv_close (convertor[CONV_PSPP_TO_UTF8]);
176   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
177
178   iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
179   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
180 }
181
182 void
183 i18n_init (void)
184 {
185   assert (!locale) ;
186   locale = strdup (setlocale (LC_CTYPE, NULL));
187
188   setlocale (LC_CTYPE, locale);
189   charset = locale_charset ();
190
191   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
192   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
193 }
194
195
196 void
197 i18n_done (void)
198 {
199   int i;
200   free (locale);
201   locale = 0;
202
203   for(i = 0 ; i < n_CONV; ++i )
204     {
205       if ( (iconv_t) -1 == convertor[i] )
206         continue;
207       iconv_close (convertor[i]);
208     }
209 }
210