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;
92 fprintf (stderr, "from: %s; to %s\n", from, to);
94 if ( 0 == strcmp (to, from))
95 return xstrndup (text, length);
97 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
98 if ( outbufferlength > length)
101 outbuf = xmalloc(outbufferlength);
104 outbytes = outbufferlength;
108 conv = create_iconv (to, from);
111 const char *ip = text;
112 result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
117 int the_error = errno;
125 *op++ = fallbackchar;
134 outbufferlength <<= 1;
135 outbuf = xmalloc (outbufferlength);
137 outbytes = outbufferlength;
142 /* should never happen */
147 } while ( -1 == result );
154 char *const oldaddr = outbuf;
155 outbuf = xrealloc (outbuf, outbufferlength + 1);
157 op += (outbuf - oldaddr) ;
171 free (default_encoding);
172 default_encoding = strdup (locale_charset ());
179 free (default_encoding);
180 default_encoding = NULL;
186 /* Return the system local's idea of the
187 decimal seperator character */
189 get_system_decimal (void)
193 char *ol = strdup (setlocale (LC_NUMERIC, NULL));
194 setlocale (LC_NUMERIC, "");
197 radix_char = nl_langinfo (RADIXCHAR)[0];
201 snprintf (buf, sizeof buf, "%f", 2.5);
206 /* We MUST leave LC_NUMERIC untouched, since it would
207 otherwise interfere with data_{in,out} */
208 setlocale (LC_NUMERIC, ol);