Internationalisation.
[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 ( length == -1 ) 
58      length = strlen(text);
59
60   assert(how < n_CONV);
61
62   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
63     if ( outbufferlength > length) 
64       break;
65
66   outbuf = xmalloc(outbufferlength);
67   op = outbuf;
68   ip = (char *) text;
69
70   outbytes = outbufferlength;
71   inbytes = length;
72   
73   do {
74
75   
76     result = iconv(convertor[how], &ip, &inbytes, 
77                    &op, &outbytes);
78
79     if ( -1 == result ) 
80       {
81         int the_error = errno;
82
83         switch ( the_error)
84           {
85           case EILSEQ:
86           case EINVAL:
87             if ( outbytes > 0 ) 
88               {
89                 *op++ = fallbackchar;
90                 outbytes--;
91                 ip++;
92                 inbytes--;
93                 break;
94               }
95             /* Fall through */
96           case E2BIG:
97             free(outbuf);
98             outbufferlength <<= 1;
99             outbuf = xmalloc(outbufferlength);
100             op = outbuf;
101             ip = (char *) text;
102             outbytes = outbufferlength;
103             inbytes = length;
104             break;
105           default:
106             /* should never happen */
107             break;
108           }
109
110       }
111
112   } while ( -1 == result );
113
114   *op = '\0';
115   
116
117   return outbuf;
118 }
119
120
121 /* Returns the current PSPP locale */
122 const char *
123 get_pspp_locale(void)
124 {
125   assert ( locale);
126   return locale;
127 }
128
129 /* Set the PSPP locale */
130 void 
131 set_pspp_locale(const char *l)
132 {
133   char *current_locale;
134   const char *current_charset;
135
136   free(locale);
137   locale = strdup(l);
138
139   current_locale = setlocale(LC_CTYPE, 0);
140   current_charset = locale_charset();
141   setlocale(LC_CTYPE, locale);
142   
143   charset = locale_charset();
144   setlocale(LC_CTYPE, current_locale);
145
146   iconv_close(convertor[CONV_PSPP_TO_UTF8]);
147   convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
148
149   iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
150   convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, current_charset);
151 }
152
153 void
154 i18n_init(void)
155 {
156   assert ( ! locale) ;
157   locale = strdup(setlocale(LC_CTYPE, NULL));
158
159   setlocale(LC_CTYPE, locale);
160   charset = locale_charset();
161
162   convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
163   convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, charset);
164 }
165
166
167 void 
168 i18n_done(void)
169 {
170   int i;
171   free(locale);
172   locale = 0;
173
174   for(i = 0 ; i < n_CONV; ++i ) 
175     iconv_close(convertor[i]);
176 }