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>
42 static iconv_t convertor[n_CONV];
45 /* A wrapper around iconv_open */
47 create_iconv (const char* tocode, const char* fromcode)
49 iconv_t conv = iconv_open (tocode, fromcode);
51 /* I don't think it's safe to translate this string or to use messaging
52 as the convertors have not yet been set up */
53 if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
55 const int err = errno;
57 "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
58 fromcode, tocode, strerror (err));
64 /* Return a string based on TEXT converted according to HOW.
65 If length is not -1, then it must be the number of bytes in TEXT.
66 The returned string must be freed when no longer required.
69 recode_string (enum conv_id how, const char *text, int length)
72 size_t outbufferlength;
78 /* FIXME: Need to ensure that this char is valid in the target encoding */
79 const char fallbackchar = '?';
85 length = strlen(text);
87 assert (how < n_CONV);
89 if (convertor[how] == (iconv_t) -1)
90 return xstrndup (text, length);
92 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
93 if ( outbufferlength > length)
96 outbuf = xmalloc(outbufferlength);
99 outbytes = outbufferlength;
103 const char *ip = text;
104 result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
109 int the_error = errno;
117 *op++ = fallbackchar;
126 outbufferlength <<= 1;
127 outbuf = xmalloc (outbufferlength);
129 outbytes = outbufferlength;
134 /* should never happen */
139 } while ( -1 == result );
143 char *const oldaddr = outbuf;
144 outbuf = xrealloc (outbuf, outbufferlength + 1);
146 op += (outbuf - oldaddr) ;
155 /* Returns the current PSPP locale */
157 get_pspp_locale (void)
163 /* Set the PSPP locale */
165 set_pspp_locale (const char *l)
167 char *current_locale;
168 char *current_charset;
173 current_locale = strdup (setlocale (LC_CTYPE, 0));
174 current_charset = strdup (locale_charset ());
175 setlocale (LC_CTYPE, locale);
178 charset = strdup (locale_charset ());
179 setlocale (LC_CTYPE, current_locale);
181 iconv_close (convertor[CONV_PSPP_TO_UTF8]);
182 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
184 iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
185 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
187 iconv_close (convertor[CONV_UTF8_TO_PSPP]);
188 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
190 free (current_locale);
191 free (current_charset);
198 locale = strdup (setlocale (LC_CTYPE, NULL));
200 setlocale (LC_CTYPE, locale);
203 charset = strdup (locale_charset ());
205 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
206 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
207 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
218 for(i = 0 ; i < n_CONV; ++i )
220 if ( (iconv_t) -1 == convertor[i] )
222 iconv_close (convertor[i]);
229 /* Return the system local's idea of the
230 decimal seperator character */
232 get_system_decimal (void)
236 char *ol = strdup (setlocale (LC_NUMERIC, NULL));
237 setlocale (LC_NUMERIC, "");
240 radix_char = nl_langinfo (RADIXCHAR)[0];
244 snprintf (buf, sizeof buf, "%f", 2.5);
249 /* We MUST leave LC_NUMERIC untouched, since it would
250 otherwise interfere with data_{in,out} */
251 setlocale (LC_NUMERIC, ol);