Fixed minor memory leak.
[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 <string.h>
25 #include <iconv.h>
26 #include <errno.h>
27
28 #include "i18n.h"
29
30 #include <localcharset.h>
31
32
33 static char *locale = 0;
34 static const char *charset;
35
36
37 static iconv_t convertor[n_CONV];
38
39 /* Return a string based on TEXT converted according to HOW.
40    If length is not -1, then it must be the number of bytes in TEXT.
41    The returned string must be freed when no longer required.
42 */
43 char *
44 recode_string(enum conv_id how,  const char *text, int length)
45 {
46   char *outbuf = 0;
47   size_t outbufferlength;
48   size_t result;
49   char *ip ;
50   char *op ;
51   size_t inbytes = 0;
52   size_t outbytes ;
53
54   /* FIXME: Need to ensure that this char is valid in the target encoding */
55   const char fallbackchar = '?';
56
57   if ( text == NULL ) 
58     return NULL;
59
60   if ( length == -1 ) 
61      length = strlen(text);
62
63   assert(how < n_CONV);
64
65   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
66     if ( outbufferlength > length) 
67       break;
68
69   outbuf = xmalloc(outbufferlength);
70   op = outbuf;
71   ip = (char *) text;
72
73   outbytes = outbufferlength;
74   inbytes = length;
75   
76   do {
77     result = iconv(convertor[how], &ip, &inbytes, 
78                    &op, &outbytes);
79
80     if ( -1 == result ) 
81       {
82         int the_error = errno;
83
84         switch ( the_error)
85           {
86           case EILSEQ:
87           case EINVAL:
88             if ( outbytes > 0 ) 
89               {
90                 *op++ = fallbackchar;
91                 outbytes--;
92                 ip++;
93                 inbytes--;
94                 break;
95               }
96             /* Fall through */
97           case E2BIG:
98             free(outbuf);
99             outbufferlength <<= 1;
100             outbuf = xmalloc(outbufferlength);
101             op = outbuf;
102             ip = (char *) text;
103             outbytes = outbufferlength;
104             inbytes = length;
105             break;
106           default:
107             /* should never happen */
108             break;
109           }
110
111       }
112   } while ( -1 == result );
113
114   if (outbytes == 0 ) 
115     {
116       char *const oldaddr = outbuf;
117       outbuf = xrealloc(outbuf, outbufferlength + 1);
118       
119       op += (outbuf - oldaddr) ;
120     }
121
122   *op = '\0';
123
124   return outbuf;
125 }
126
127
128 /* Returns the current PSPP locale */
129 const char *
130 get_pspp_locale(void)
131 {
132   assert ( locale);
133   return locale;
134 }
135
136 /* Set the PSPP locale */
137 void 
138 set_pspp_locale(const char *l)
139 {
140   char *current_locale;
141   const char *current_charset;
142
143   free(locale);
144   locale = strdup(l);
145
146   current_locale = setlocale(LC_CTYPE, 0);
147   current_charset = locale_charset();
148   setlocale(LC_CTYPE, locale);
149   
150   charset = locale_charset();
151   setlocale(LC_CTYPE, current_locale);
152
153   iconv_close(convertor[CONV_PSPP_TO_UTF8]);
154   convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
155
156   iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
157   convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, current_charset);
158 }
159
160 void
161 i18n_init(void)
162 {
163   assert ( ! locale) ;
164   locale = strdup(setlocale(LC_CTYPE, NULL));
165
166   setlocale(LC_CTYPE, locale);
167   charset = locale_charset();
168
169   convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
170   convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, charset);
171 }
172
173
174 void 
175 i18n_done(void)
176 {
177   int i;
178   free(locale);
179   locale = 0;
180
181   for(i = 0 ; i < n_CONV; ++i ) 
182     iconv_close(convertor[i]);
183 }