1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
27 #include "assertion.h"
29 #include "hash-functions.h"
35 #include <localcharset.h>
49 static char *default_encoding;
50 static struct hmapx map;
52 /* A wrapper around iconv_open */
54 create_iconv (const char* tocode, const char* fromcode)
57 struct hmapx_node *node;
58 struct converter *converter;
60 hash = hsh_hash_string (tocode) ^ hsh_hash_string (fromcode);
61 HMAPX_FOR_EACH_WITH_HASH (converter, node, hash, &map)
62 if (!strcmp (tocode, converter->tocode)
63 && !strcmp (fromcode, converter->fromcode))
64 return converter->conv;
66 converter = xmalloc (sizeof *converter);
67 converter->tocode = xstrdup (tocode);
68 converter->fromcode = xstrdup (fromcode);
69 converter->conv = iconv_open (tocode, fromcode);
70 hmapx_insert (&map, converter, hash);
72 /* I don't think it's safe to translate this string or to use messaging
73 as the convertors have not yet been set up */
74 if ( (iconv_t) -1 == converter->conv && 0 != strcmp (tocode, fromcode))
76 const int err = errno;
79 "cannot create a convertor for \"%s\" to \"%s\": %s\n",
80 fromcode, tocode, strerror (err));
83 return converter->conv;
86 /* Return a string based on TEXT converted according to HOW.
87 If length is not -1, then it must be the number of bytes in TEXT.
88 The returned string must be freed when no longer required.
91 recode_string (const char *to, const char *from,
92 const char *text, int length)
95 size_t outbufferlength;
102 /* FIXME: Need to ensure that this char is valid in the target encoding */
103 const char fallbackchar = '?';
109 length = strlen(text);
113 to = default_encoding;
116 from = default_encoding;
118 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
119 if ( outbufferlength > length)
122 outbuf = xmalloc(outbufferlength);
125 outbytes = outbufferlength;
129 conv = create_iconv (to, from);
132 const char *ip = text;
133 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
138 int the_error = errno;
146 *op++ = fallbackchar;
155 outbufferlength <<= 1;
156 outbuf = xmalloc (outbufferlength);
158 outbytes = outbufferlength;
163 /* should never happen */
168 } while ( -1 == result );
172 char *const oldaddr = outbuf;
173 outbuf = xrealloc (outbuf, outbufferlength + 1);
175 op += (outbuf - oldaddr) ;
188 setlocale (LC_CTYPE, "");
190 setlocale (LC_MESSAGES, "");
193 setlocale (LC_PAPER, "");
195 bindtextdomain (PACKAGE, locale_dir);
196 textdomain (PACKAGE);
197 #endif /* ENABLE_NLS */
199 assert (default_encoding == NULL);
200 default_encoding = xstrdup (locale_charset ());
207 get_default_encoding (void)
209 return default_encoding;
213 set_default_encoding (const char *enc)
215 free (default_encoding);
216 default_encoding = xstrdup (enc);
220 /* Attempts to set the encoding from a locale name
221 returns true if successfull.
222 This function does not (should not!) alter the current locale.
225 set_encoding_from_locale (const char *loc)
230 char *tmp = xstrdup (setlocale (LC_CTYPE, NULL));
232 setlocale (LC_CTYPE, "C");
233 c_encoding = xstrdup (locale_charset ());
235 setlocale (LC_CTYPE, loc);
236 loc_encoding = xstrdup (locale_charset ());
239 if ( 0 == strcmp (loc_encoding, c_encoding))
245 setlocale (LC_CTYPE, tmp);
251 free (default_encoding);
252 default_encoding = loc_encoding;
265 struct hmapx_node *node;
266 struct converter *cvtr;
267 HMAPX_FOR_EACH (cvtr, node, &map)
269 iconv_close (cvtr->conv);
273 hmapx_destroy (&map);
275 free (default_encoding);
276 default_encoding = NULL;
282 valid_encoding (const char *enc)
284 iconv_t conv = iconv_open ("UTF8", enc);
286 if ( conv == (iconv_t) -1)
295 /* Return the system local's idea of the
296 decimal seperator character */
298 get_system_decimal (void)
302 char *ol = xstrdup (setlocale (LC_NUMERIC, NULL));
303 setlocale (LC_NUMERIC, "");
306 radix_char = nl_langinfo (RADIXCHAR)[0];
310 snprintf (buf, sizeof buf, "%f", 2.5);
315 /* We MUST leave LC_NUMERIC untouched, since it would
316 otherwise interfere with data_{in,out} */
317 setlocale (LC_NUMERIC, ol);