Removed some unused features from src/libpspp/i18n.c and src/libpspp/i18n.h
[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 <locale.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <iconv.h>
25 #include <errno.h>
26 #include "assertion.h"
27
28 #include "i18n.h"
29
30 #include <localcharset.h>
31 #include "xstrndup.h"
32
33 #if HAVE_NL_LANGINFO
34 #include <langinfo.h>
35 #endif
36
37
38 static char *locale;
39 static char *charset;
40
41
42 static iconv_t convertor[n_CONV];
43
44
45 /* A wrapper around iconv_open */
46 static iconv_t
47 create_iconv (const char* tocode, const char* fromcode)
48 {
49   iconv_t conv = iconv_open (tocode, fromcode);
50
51   /* I don't think it's safe to translate this string or to use messaging
52      as the convertors have not yet been set up */
53   if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
54     {
55       const int err = errno;
56       fprintf (stderr,
57         "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
58         fromcode, tocode, strerror (err));
59     }
60
61   return conv;
62 }
63
64 /* Return a string based on TEXT converted according to HOW.
65    If length is not -1, then it must be the number of bytes in TEXT.
66    The returned string must be freed when no longer required.
67 */
68 char *
69 recode_string (enum conv_id how,  const char *text, int length)
70 {
71   char *outbuf = 0;
72   size_t outbufferlength;
73   size_t result;
74   char *op ;
75   size_t inbytes = 0;
76   size_t outbytes ;
77
78   /* FIXME: Need to ensure that this char is valid in the target encoding */
79   const char fallbackchar = '?';
80
81   if ( text == NULL )
82     return NULL;
83
84   if ( length == -1 )
85      length = strlen(text);
86
87   assert (how < n_CONV);
88
89   if (convertor[how] == (iconv_t) -1)
90     return xstrndup (text, length);
91
92   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
93     if ( outbufferlength > length)
94       break;
95
96   outbuf = xmalloc(outbufferlength);
97   op = outbuf;
98
99   outbytes = outbufferlength;
100   inbytes = length;
101
102   do {
103     const char *ip = text;
104     result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
105                    &op, &outbytes);
106
107     if ( -1 == result )
108       {
109         int the_error = errno;
110
111         switch (the_error)
112           {
113           case EILSEQ:
114           case EINVAL:
115             if ( outbytes > 0 )
116               {
117                 *op++ = fallbackchar;
118                 outbytes--;
119                 text++;
120                 inbytes--;
121                 break;
122               }
123             /* Fall through */
124           case E2BIG:
125             free (outbuf);
126             outbufferlength <<= 1;
127             outbuf = xmalloc (outbufferlength);
128             op = outbuf;
129             outbytes = outbufferlength;
130             inbytes = length;
131             text = ip;
132             break;
133           default:
134             /* should never happen */
135             NOT_REACHED ();
136             break;
137           }
138       }
139   } while ( -1 == result );
140
141   if (outbytes == 0 )
142     {
143       char *const oldaddr = outbuf;
144       outbuf = xrealloc (outbuf, outbufferlength + 1);
145
146       op += (outbuf - oldaddr) ;
147     }
148
149   *op = '\0';
150
151   return outbuf;
152 }
153
154
155 /* Returns the current PSPP locale */
156 const char *
157 get_pspp_locale (void)
158 {
159   assert (locale);
160   return locale;
161 }
162
163
164 void
165 i18n_init (void)
166 {
167   assert (!locale) ;
168   locale = strdup (setlocale (LC_CTYPE, NULL));
169
170   setlocale (LC_CTYPE, locale);
171
172   free (charset);
173   charset = strdup (locale_charset ());
174
175   convertor[CONV_PSPP_TO_UTF8]   = create_iconv ("UTF-8", charset);
176   convertor[CONV_UTF8_TO_PSPP]   = create_iconv (charset, "UTF-8");
177 }
178
179
180 void
181 i18n_done (void)
182 {
183   int i;
184   free (locale);
185   locale = 0;
186
187   for(i = 0 ; i < n_CONV; ++i )
188     {
189       if ( (iconv_t) -1 == convertor[i] )
190         continue;
191       iconv_close (convertor[i]);
192     }
193 }
194
195
196
197
198 /* Return the system local's idea of the
199    decimal seperator character */
200 char
201 get_system_decimal (void)
202 {
203   char radix_char;
204
205   char *ol = strdup (setlocale (LC_NUMERIC, NULL));
206   setlocale (LC_NUMERIC, "");
207
208 #if HAVE_NL_LANGINFO
209   radix_char = nl_langinfo (RADIXCHAR)[0];
210 #else
211   {
212     char buf[10];
213     snprintf (buf, sizeof buf, "%f", 2.5);
214     radix_char = buf[1];
215   }
216 #endif
217
218   /* We MUST leave LC_NUMERIC untouched, since it would
219      otherwise interfere with data_{in,out} */
220   setlocale (LC_NUMERIC, ol);
221   free (ol);
222   return radix_char;
223 }
224