1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 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"
30 #include <localcharset.h>
37 static char *default_encoding;
40 /* A wrapper around iconv_open */
42 create_iconv (const char* tocode, const char* fromcode)
44 iconv_t conv = iconv_open (tocode, fromcode);
46 /* I don't think it's safe to translate this string or to use messaging
47 as the convertors have not yet been set up */
48 if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
50 const int err = errno;
52 "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
53 fromcode, tocode, strerror (err));
59 /* Return a string based on TEXT converted according to HOW.
60 If length is not -1, then it must be the number of bytes in TEXT.
61 The returned string must be freed when no longer required.
64 recode_string (const char *to, const char *from,
65 const char *text, int length)
68 size_t outbufferlength;
75 /* FIXME: Need to ensure that this char is valid in the target encoding */
76 const char fallbackchar = '?';
82 length = strlen(text);
86 to = default_encoding;
89 from = default_encoding;
91 if ( 0 == strcmp (to, from))
92 return xstrndup (text, length);
94 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
95 if ( outbufferlength > length)
98 outbuf = xmalloc(outbufferlength);
101 outbytes = outbufferlength;
105 conv = create_iconv (to, from);
108 const char *ip = text;
109 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
114 int the_error = errno;
122 *op++ = fallbackchar;
131 outbufferlength <<= 1;
132 outbuf = xmalloc (outbufferlength);
134 outbytes = outbufferlength;
139 /* should never happen */
144 } while ( -1 == result );
151 char *const oldaddr = outbuf;
152 outbuf = xrealloc (outbuf, outbufferlength + 1);
154 op += (outbuf - oldaddr) ;
168 free (default_encoding);
169 default_encoding = strdup (locale_charset ());
176 free (default_encoding);
177 default_encoding = NULL;
183 /* Return the system local's idea of the
184 decimal seperator character */
186 get_system_decimal (void)
190 char *ol = strdup (setlocale (LC_NUMERIC, NULL));
191 setlocale (LC_NUMERIC, "");
194 radix_char = nl_langinfo (RADIXCHAR)[0];
198 snprintf (buf, sizeof buf, "%f", 2.5);
203 /* We MUST leave LC_NUMERIC untouched, since it would
204 otherwise interfere with data_{in,out} */
205 setlocale (LC_NUMERIC, ol);