1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by John Darrington <john@darrington.wattle.id.au>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 #include <localcharset.h>
33 static char *locale = 0;
34 static const char *charset;
37 static iconv_t convertor[n_CONV];
39 /* Return a string based on TEXT converted according to HOW.
40 If length is not -1, then it must be the number of bytes in TEXT.
41 The returned string must be freed when no longer required.
44 recode_string(enum conv_id how, const char *text, int length)
47 size_t outbufferlength;
54 /* FIXME: Need to ensure that this char is valid in the target encoding */
55 const char fallbackchar = '?';
58 length = strlen(text);
62 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
63 if ( outbufferlength > length)
66 outbuf = xmalloc(outbufferlength);
70 outbytes = outbufferlength;
76 result = iconv(convertor[how], &ip, &inbytes,
81 int the_error = errno;
98 outbufferlength <<= 1;
99 outbuf = xmalloc(outbufferlength);
102 outbytes = outbufferlength;
106 /* should never happen */
112 } while ( -1 == result );
121 /* Returns the current PSPP locale */
123 get_pspp_locale(void)
129 /* Set the PSPP locale */
131 set_pspp_locale(const char *l)
133 char *current_locale;
134 const char *current_charset;
139 current_locale = setlocale(LC_CTYPE, 0);
140 current_charset = locale_charset();
141 setlocale(LC_CTYPE, locale);
143 charset = locale_charset();
144 setlocale(LC_CTYPE, current_locale);
146 iconv_close(convertor[CONV_PSPP_TO_UTF8]);
147 convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
149 iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
150 convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, current_charset);
157 locale = strdup(setlocale(LC_CTYPE, NULL));
159 setlocale(LC_CTYPE, locale);
160 charset = locale_charset();
162 convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
163 convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, charset);
174 for(i = 0 ; i < n_CONV; ++i )
175 iconv_close(convertor[i]);