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/>. */
26 #include "assertion.h"
28 #include "hash-functions.h"
32 #include <localcharset.h>
39 static char *default_encoding;
40 static struct hmapx map;
42 /* A wrapper around iconv_open */
44 create_iconv (const char* tocode, const char* fromcode)
47 struct hmapx_node *node;
49 char *key = alloca (strlen (tocode) + strlen (fromcode) + 2);
52 strcat (key, "\n"); /* hopefully no encoding names contain '\n' */
53 strcat (key, fromcode);
55 hash = hsh_hash_string (key);
57 node = hmapx_first_with_hash (&map, hash);
61 conv = iconv_open (tocode, fromcode);
63 /* I don't think it's safe to translate this string or to use messaging
64 as the convertors have not yet been set up */
65 if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
67 const int err = errno;
70 "cannot create a convertor for \"%s\" to \"%s\": %s\n",
71 fromcode, tocode, strerror (err));
74 hmapx_insert (&map, conv, hash);
78 conv = hmapx_node_data (node);
84 /* Return a string based on TEXT converted according to HOW.
85 If length is not -1, then it must be the number of bytes in TEXT.
86 The returned string must be freed when no longer required.
89 recode_string (const char *to, const char *from,
90 const char *text, int length)
93 size_t outbufferlength;
100 /* FIXME: Need to ensure that this char is valid in the target encoding */
101 const char fallbackchar = '?';
107 length = strlen(text);
111 to = default_encoding;
114 from = default_encoding;
116 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
117 if ( outbufferlength > length)
120 outbuf = xmalloc(outbufferlength);
123 outbytes = outbufferlength;
127 conv = create_iconv (to, from);
130 const char *ip = text;
131 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
136 int the_error = errno;
144 *op++ = fallbackchar;
153 outbufferlength <<= 1;
154 outbuf = xmalloc (outbufferlength);
156 outbytes = outbufferlength;
161 /* should never happen */
166 } while ( -1 == result );
170 char *const oldaddr = outbuf;
171 outbuf = xrealloc (outbuf, outbufferlength + 1);
173 op += (outbuf - oldaddr) ;
185 free (default_encoding);
186 default_encoding = strdup (locale_charset ());
195 struct hmapx_node *node;
197 HMAPX_FOR_EACH (conv, node, &map)
200 hmapx_destroy (&map);
202 free (default_encoding);
203 default_encoding = NULL;
209 /* Return the system local's idea of the
210 decimal seperator character */
212 get_system_decimal (void)
216 char *ol = strdup (setlocale (LC_NUMERIC, NULL));
217 setlocale (LC_NUMERIC, "");
220 radix_char = nl_langinfo (RADIXCHAR)[0];
224 snprintf (buf, sizeof buf, "%f", 2.5);
229 /* We MUST leave LC_NUMERIC untouched, since it would
230 otherwise interfere with data_{in,out} */
231 setlocale (LC_NUMERIC, ol);