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>
38 static char *locale = 0;
39 static const char *charset;
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 const char *current_charset;
173 current_locale = setlocale (LC_CTYPE, 0);
174 current_charset = locale_charset ();
175 setlocale (LC_CTYPE, locale);
177 charset = locale_charset ();
178 setlocale (LC_CTYPE, current_locale);
180 iconv_close (convertor[CONV_PSPP_TO_UTF8]);
181 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
183 iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
184 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
186 iconv_close (convertor[CONV_UTF8_TO_PSPP]);
187 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
194 locale = strdup (setlocale (LC_CTYPE, NULL));
196 setlocale (LC_CTYPE, locale);
197 charset = locale_charset ();
199 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
200 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
201 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
212 for(i = 0 ; i < n_CONV; ++i )
214 if ( (iconv_t) -1 == convertor[i] )
216 iconv_close (convertor[i]);
223 /* Return the system local's idea of the
224 decimal seperator character */
226 get_system_decimal (void)
228 char *radix_char = NULL;
230 char *ol = setlocale (LC_NUMERIC, NULL);
231 setlocale (LC_NUMERIC, "");
234 radix_char = nl_langinfo (RADIXCHAR);
237 char *buf = xmalloc (10);
238 snprintf (buf, 10, "%f", 2.5);
239 radix_char = &buf[1];
243 /* We MUST leave LC_NUMERIC untouched, since it would
244 otherwise interfere with data_{in,out} */
245 setlocale (LC_NUMERIC, ol);