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 = '?';
61 length = strlen(text);
65 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
66 if ( outbufferlength > length)
69 outbuf = xmalloc(outbufferlength);
73 outbytes = outbufferlength;
77 result = iconv(convertor[how], &ip, &inbytes,
82 int the_error = errno;
99 outbufferlength <<= 1;
100 outbuf = xmalloc(outbufferlength);
103 outbytes = outbufferlength;
107 /* should never happen */
112 } while ( -1 == result );
116 char *const oldaddr = outbuf;
117 outbuf = xrealloc(outbuf, outbufferlength + 1);
119 op += (outbuf - oldaddr) ;
128 /* Returns the current PSPP locale */
130 get_pspp_locale(void)
136 /* Set the PSPP locale */
138 set_pspp_locale(const char *l)
140 char *current_locale;
141 const char *current_charset;
146 current_locale = setlocale(LC_CTYPE, 0);
147 current_charset = locale_charset();
148 setlocale(LC_CTYPE, locale);
150 charset = locale_charset();
151 setlocale(LC_CTYPE, current_locale);
153 iconv_close(convertor[CONV_PSPP_TO_UTF8]);
154 convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
156 iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
157 convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, current_charset);
164 locale = strdup(setlocale(LC_CTYPE, NULL));
166 setlocale(LC_CTYPE, locale);
167 charset = locale_charset();
169 convertor[CONV_PSPP_TO_UTF8] = iconv_open("UTF-8", charset);
170 convertor[CONV_SYSTEM_TO_PSPP] = iconv_open(charset, charset);
181 for(i = 0 ; i < n_CONV; ++i )
182 iconv_close(convertor[i]);