Fixed problem deallocating iconv structs.
[pspp-builds.git] / src / libpspp / i18n.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <xalloc.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <iconv.h>
27 #include <errno.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 *ip ;
72   char *op ;
73   size_t inbytes = 0;
74   size_t outbytes ;
75
76   /* FIXME: Need to ensure that this char is valid in the target encoding */
77   const char fallbackchar = '?';
78
79   if ( text == NULL ) 
80     return NULL;
81
82   if ( length == -1 ) 
83      length = strlen(text);
84
85   assert(how < n_CONV);
86
87   if (convertor[how] == (iconv_t) -1) 
88     return xstrndup (text, length);
89
90   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
91     if ( outbufferlength > length) 
92       break;
93
94   outbuf = xmalloc(outbufferlength);
95   op = outbuf;
96   ip = (char *) text;
97
98   outbytes = outbufferlength;
99   inbytes = length;
100   
101   do {
102     result = iconv(convertor[how], &ip, &inbytes, 
103                    &op, &outbytes);
104
105     if ( -1 == result ) 
106       {
107         int the_error = errno;
108
109         switch ( the_error)
110           {
111           case EILSEQ:
112           case EINVAL:
113             if ( outbytes > 0 ) 
114               {
115                 *op++ = fallbackchar;
116                 outbytes--;
117                 ip++;
118                 inbytes--;
119                 break;
120               }
121             /* Fall through */
122           case E2BIG:
123             free(outbuf);
124             outbufferlength <<= 1;
125             outbuf = xmalloc(outbufferlength);
126             op = outbuf;
127             ip = (char *) text;
128             outbytes = outbufferlength;
129             inbytes = length;
130             break;
131           default:
132             /* should never happen */
133             break;
134           }
135
136       }
137   } while ( -1 == result );
138
139   if (outbytes == 0 ) 
140     {
141       char *const oldaddr = outbuf;
142       outbuf = xrealloc(outbuf, outbufferlength + 1);
143       
144       op += (outbuf - oldaddr) ;
145     }
146
147   *op = '\0';
148
149   return outbuf;
150 }
151
152
153 /* Returns the current PSPP locale */
154 const char *
155 get_pspp_locale(void)
156 {
157   assert ( locale);
158   return locale;
159 }
160
161 /* Set the PSPP locale */
162 void 
163 set_pspp_locale(const char *l)
164 {
165   char *current_locale;
166   const char *current_charset;
167
168   free(locale);
169   locale = strdup(l);
170
171   current_locale = setlocale(LC_CTYPE, 0);
172   current_charset = locale_charset();
173   setlocale(LC_CTYPE, locale);
174   
175   charset = locale_charset();
176   setlocale(LC_CTYPE, current_locale);
177
178   iconv_close(convertor[CONV_PSPP_TO_UTF8]);
179   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
180
181   iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
182   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
183 }
184
185 void
186 i18n_init(void)
187 {
188   assert ( ! locale) ;
189   locale = strdup(setlocale(LC_CTYPE, NULL));
190
191   setlocale(LC_CTYPE, locale);
192   charset = locale_charset();
193
194   convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
195   convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
196 }
197
198
199 void 
200 i18n_done(void)
201 {
202   int i;
203   free(locale);
204   locale = 0;
205
206   for(i = 0 ; i < n_CONV; ++i ) 
207     {
208       if ( (iconv_t) -1 == convertor[i] ) 
209         continue;
210       iconv_close(convertor[i]);
211     }
212 }
213