Fixed constness problem in iconv, using the ICONV_CONST preprocessor
[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
28 #include "i18n.h"
29
30 #include <localcharset.h>
31 #include "xstrndup.h"
32
33
34 static char *locale = 0;
35 static const char *charset;
36
37
38 static iconv_t convertor[n_CONV];
39
40
41 /* A wrapper around iconv_open */
42 static iconv_t 
43 create_iconv (const char* tocode, const char* fromcode)
44 {
45   iconv_t conv = iconv_open (tocode, fromcode);
46
47   /* I don't think it's safe to translate this string or to use messaging
48      as the convertors have not yet been set up */
49   if ( (iconv_t) -1 == conv) 
50     {
51       const int err = errno;
52       fprintf (stderr, 
53         "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
54         fromcode, tocode, strerror (err));
55     }
56    
57   return conv;
58 }
59
60 /* Return a string based on TEXT converted according to HOW.
61    If length is not -1, then it must be the number of bytes in TEXT.
62    The returned string must be freed when no longer required.
63 */
64 char *
65 recode_string (enum conv_id how,  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
74   /* FIXME: Need to ensure that this char is valid in the target encoding */
75   const char fallbackchar = '?';
76
77   if ( text == NULL ) 
78     return NULL;
79
80   if ( length == -1 ) 
81      length = strlen(text);
82
83   assert (how < n_CONV);
84
85   if (convertor[how] == (iconv_t) -1) 
86     return xstrndup (text, length);
87
88   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
89     if ( outbufferlength > length) 
90       break;
91
92   outbuf = xmalloc(outbufferlength);
93   op = outbuf;
94
95   outbytes = outbufferlength;
96   inbytes = length;
97   
98   do {
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             break;
127           default:
128             /* should never happen */
129             break;
130           }
131       }
132   } while ( -1 == result );
133
134   if (outbytes == 0 ) 
135     {
136       char *const oldaddr = outbuf;
137       outbuf = xrealloc (outbuf, outbufferlength + 1);
138       
139       op += (outbuf - oldaddr) ;
140     }
141
142   *op = '\0';
143
144   return outbuf;
145 }
146
147
148 /* Returns the current PSPP locale */
149 const char *
150 get_pspp_locale (void)
151 {
152   assert (locale);
153   return locale;
154 }
155
156 /* Set the PSPP locale */
157 void 
158 set_pspp_locale (const char *l)
159 {
160   char *current_locale;
161   const char *current_charset;
162
163   free(locale);
164   locale = strdup(l);
165
166   current_locale = setlocale (LC_CTYPE, 0);
167   current_charset = locale_charset ();
168   setlocale (LC_CTYPE, locale);
169   
170   charset = locale_charset ();
171   setlocale (LC_CTYPE, current_locale);
172
173   iconv_close (convertor[CONV_PSPP_TO_UTF8]);
174   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
175
176   iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
177   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
178 }
179
180 void
181 i18n_init (void)
182 {
183   assert (!locale) ;
184   locale = strdup (setlocale (LC_CTYPE, NULL));
185
186   setlocale (LC_CTYPE, locale);
187   charset = locale_charset ();
188
189   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
190   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
191 }
192
193
194 void 
195 i18n_done (void)
196 {
197   int i;
198   free (locale);
199   locale = 0;
200
201   for(i = 0 ; i < n_CONV; ++i ) 
202     {
203       if ( (iconv_t) -1 == convertor[i] ) 
204         continue;
205       iconv_close (convertor[i]);
206     }
207 }
208