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
31 #include <localcharset.h>
35 static char *locale = 0;
36 static const char *charset;
39 static iconv_t convertor[n_CONV];
42 /* A wrapper around iconv_open */
44 create_iconv (const char* tocode, const char* fromcode)
46 iconv_t conv = iconv_open (tocode, fromcode);
48 /* I don't think it's safe to translate this string or to use messaging
49 as the convertors have not yet been set up */
50 if ( (iconv_t) -1 == conv)
52 const int err = errno;
54 "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
55 fromcode, tocode, strerror (err));
61 /* Return a string based on TEXT converted according to HOW.
62 If length is not -1, then it must be the number of bytes in TEXT.
63 The returned string must be freed when no longer required.
66 recode_string(enum conv_id how, const char *text, int length)
69 size_t outbufferlength;
76 /* FIXME: Need to ensure that this char is valid in the target encoding */
77 const char fallbackchar = '?';
83 length = strlen(text);
87 if (convertor[how] == (iconv_t) -1)
88 return xstrndup (text, length);
90 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
91 if ( outbufferlength > length)
94 outbuf = xmalloc(outbufferlength);
98 outbytes = outbufferlength;
102 result = iconv(convertor[how], &ip, &inbytes,
107 int the_error = errno;
115 *op++ = fallbackchar;
124 outbufferlength <<= 1;
125 outbuf = xmalloc(outbufferlength);
128 outbytes = outbufferlength;
132 /* should never happen */
137 } while ( -1 == result );
141 char *const oldaddr = outbuf;
142 outbuf = xrealloc(outbuf, outbufferlength + 1);
144 op += (outbuf - oldaddr) ;
153 /* Returns the current PSPP locale */
155 get_pspp_locale(void)
161 /* Set the PSPP locale */
163 set_pspp_locale(const char *l)
165 char *current_locale;
166 const char *current_charset;
171 current_locale = setlocale(LC_CTYPE, 0);
172 current_charset = locale_charset();
173 setlocale(LC_CTYPE, locale);
175 charset = locale_charset();
176 setlocale(LC_CTYPE, current_locale);
178 iconv_close(convertor[CONV_PSPP_TO_UTF8]);
179 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
181 iconv_close(convertor[CONV_SYSTEM_TO_PSPP]);
182 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
189 locale = strdup(setlocale(LC_CTYPE, NULL));
191 setlocale(LC_CTYPE, locale);
192 charset = locale_charset();
194 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
195 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
206 for(i = 0 ; i < n_CONV; ++i )
208 if ( (iconv_t) -1 == convertor[i] )
210 iconv_close(convertor[i]);