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>
42 static char *default_encoding;
43 static struct hmapx map;
45 /* A wrapper around iconv_open */
47 create_iconv (const char* tocode, const char* fromcode)
50 struct hmapx_node *node;
52 char *key = alloca (strlen (tocode) + strlen (fromcode) + 2);
55 strcat (key, "\n"); /* hopefully no encoding names contain '\n' */
56 strcat (key, fromcode);
58 hash = hsh_hash_string (key);
60 node = hmapx_first_with_hash (&map, hash);
64 conv = iconv_open (tocode, fromcode);
66 /* I don't think it's safe to translate this string or to use messaging
67 as the convertors have not yet been set up */
68 if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
70 const int err = errno;
73 "cannot create a convertor for \"%s\" to \"%s\": %s\n",
74 fromcode, tocode, strerror (err));
77 hmapx_insert (&map, conv, hash);
81 conv = hmapx_node_data (node);
87 /* Return a string based on TEXT converted according to HOW.
88 If length is not -1, then it must be the number of bytes in TEXT.
89 The returned string must be freed when no longer required.
92 recode_string (const char *to, const char *from,
93 const char *text, int length)
96 size_t outbufferlength;
103 /* FIXME: Need to ensure that this char is valid in the target encoding */
104 const char fallbackchar = '?';
110 length = strlen(text);
114 to = default_encoding;
117 from = default_encoding;
119 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
120 if ( outbufferlength > length)
123 outbuf = xmalloc(outbufferlength);
126 outbytes = outbufferlength;
130 conv = create_iconv (to, from);
133 const char *ip = text;
134 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
139 int the_error = errno;
147 *op++ = fallbackchar;
156 outbufferlength <<= 1;
157 outbuf = xmalloc (outbufferlength);
159 outbytes = outbufferlength;
164 /* should never happen */
169 } while ( -1 == result );
173 char *const oldaddr = outbuf;
174 outbuf = xrealloc (outbuf, outbufferlength + 1);
176 op += (outbuf - oldaddr) ;
189 setlocale (LC_CTYPE, "");
191 setlocale (LC_MESSAGES, "");
194 setlocale (LC_PAPER, "");
196 bindtextdomain (PACKAGE, locale_dir);
197 textdomain (PACKAGE);
198 #endif /* ENABLE_NLS */
200 assert (default_encoding == NULL);
201 default_encoding = strdup (locale_charset ());
208 get_default_encoding (void)
210 return default_encoding;
214 set_default_encoding (const char *enc)
216 free (default_encoding);
217 default_encoding = strdup (enc);
221 /* Attempts to set the encoding from a locale name
222 returns true if successfull.
223 This function does not (should not!) alter the current locale.
226 set_encoding_from_locale (const char *loc)
231 char *tmp = strdup (setlocale (LC_CTYPE, NULL));
233 setlocale (LC_CTYPE, "C");
234 c_encoding = strdup (locale_charset ());
236 setlocale (LC_CTYPE, loc);
237 loc_encoding = strdup (locale_charset ());
240 if ( 0 == strcmp (loc_encoding, c_encoding))
246 setlocale (LC_CTYPE, tmp);
252 free (default_encoding);
253 default_encoding = loc_encoding;
266 struct hmapx_node *node;
268 HMAPX_FOR_EACH (conv, node, &map)
271 hmapx_destroy (&map);
273 free (default_encoding);
274 default_encoding = NULL;
280 valid_encoding (const char *enc)
282 iconv_t conv = iconv_open ("UTF8", enc);
284 if ( conv == (iconv_t) -1)
293 /* Return the system local's idea of the
294 decimal seperator character */
296 get_system_decimal (void)
300 char *ol = strdup (setlocale (LC_NUMERIC, NULL));
301 setlocale (LC_NUMERIC, "");
304 radix_char = nl_langinfo (RADIXCHAR)[0];
308 snprintf (buf, sizeof buf, "%f", 2.5);
313 /* We MUST leave LC_NUMERIC untouched, since it would
314 otherwise interfere with data_{in,out} */
315 setlocale (LC_NUMERIC, ol);