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 <relocatable.h>
28 #include "assertion.h"
30 #include "hash-functions.h"
37 #include <localcharset.h>
51 static char *default_encoding;
52 static struct hmapx map;
54 /* A wrapper around iconv_open */
56 create_iconv (const char* tocode, const char* fromcode)
59 struct hmapx_node *node;
60 struct converter *converter;
63 hash = hash_string (tocode, hash_string (fromcode, 0));
64 HMAPX_FOR_EACH_WITH_HASH (converter, node, hash, &map)
65 if (!strcmp (tocode, converter->tocode)
66 && !strcmp (fromcode, converter->fromcode))
67 return converter->conv;
69 converter = xmalloc (sizeof *converter);
70 converter->tocode = xstrdup (tocode);
71 converter->fromcode = xstrdup (fromcode);
72 converter->conv = iconv_open (tocode, fromcode);
73 hmapx_insert (&map, converter, hash);
75 /* I don't think it's safe to translate this string or to use messaging
76 as the converters have not yet been set up */
77 if ( (iconv_t) -1 == converter->conv && 0 != strcmp (tocode, fromcode))
79 const int err = errno;
82 "cannot create a converter for \"%s\" to \"%s\": %s\n",
83 fromcode, tocode, strerror (err));
86 return converter->conv;
90 recode_string (const char *to, const char *from,
91 const char *text, int length)
93 return recode_string_pool (to, from, text, length, NULL);
97 /* Return a string based on TEXT which must be encoded using FROM.
98 The returned string will be encoded in TO.
99 If length is not -1, then it must be the number of bytes in TEXT.
100 The returned string must be freed when no longer required.
103 recode_string_pool (const char *to, const char *from,
104 const char *text, int length, struct pool *pool)
107 size_t outbufferlength;
114 /* FIXME: Need to ensure that this char is valid in the target encoding */
115 const char fallbackchar = '?';
121 length = strlen(text);
124 to = default_encoding;
127 from = default_encoding;
129 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
130 if ( outbufferlength > length)
133 outbuf = pool_malloc (pool, outbufferlength);
136 outbytes = outbufferlength;
140 conv = create_iconv (to, from);
142 if ( (iconv_t) -1 == conv )
143 return xstrdup (text);
146 const char *ip = text;
147 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
152 int the_error = errno;
160 *op++ = fallbackchar;
169 outbufferlength <<= 1;
170 outbuf = pool_malloc (pool, outbufferlength);
172 outbytes = outbufferlength;
177 /* should never happen */
178 fprintf (stderr, "Character conversion error: %s\n", strerror (the_error));
183 } while ( -1 == result );
187 char *const oldaddr = outbuf;
188 outbuf = pool_realloc (pool, outbuf, outbufferlength + 1);
190 op += (outbuf - oldaddr) ;
203 setlocale (LC_CTYPE, "");
205 setlocale (LC_MESSAGES, "");
208 setlocale (LC_PAPER, "");
210 bindtextdomain (PACKAGE, relocate(locale_dir));
211 textdomain (PACKAGE);
212 #endif /* ENABLE_NLS */
214 assert (default_encoding == NULL);
215 default_encoding = xstrdup (locale_charset ());
222 get_default_encoding (void)
224 return default_encoding;
228 set_default_encoding (const char *enc)
230 free (default_encoding);
231 default_encoding = xstrdup (enc);
235 /* Attempts to set the encoding from a locale name
236 returns true if successfull.
237 This function does not (should not!) alter the current locale.
240 set_encoding_from_locale (const char *loc)
245 char *tmp = xstrdup (setlocale (LC_CTYPE, NULL));
247 setlocale (LC_CTYPE, "C");
248 c_encoding = xstrdup (locale_charset ());
250 setlocale (LC_CTYPE, loc);
251 loc_encoding = xstrdup (locale_charset ());
254 if ( 0 == strcmp (loc_encoding, c_encoding))
260 setlocale (LC_CTYPE, tmp);
266 free (default_encoding);
267 default_encoding = loc_encoding;
280 struct hmapx_node *node;
281 struct converter *cvtr;
282 HMAPX_FOR_EACH (cvtr, node, &map)
284 iconv_close (cvtr->conv);
288 hmapx_destroy (&map);
290 free (default_encoding);
291 default_encoding = NULL;
297 valid_encoding (const char *enc)
299 iconv_t conv = iconv_open ("UTF8", enc);
301 if ( conv == (iconv_t) -1)
310 /* Return the system local's idea of the
311 decimal seperator character */
313 get_system_decimal (void)
317 char *ol = xstrdup (setlocale (LC_NUMERIC, NULL));
318 setlocale (LC_NUMERIC, "");
321 radix_char = nl_langinfo (RADIXCHAR)[0];
325 snprintf (buf, sizeof buf, "%f", 2.5);
330 /* We MUST leave LC_NUMERIC untouched, since it would
331 otherwise interfere with data_{in,out} */
332 setlocale (LC_NUMERIC, ol);