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>
34 static char *locale = 0;
35 static const char *charset;
38 static iconv_t convertor[n_CONV];
41 /* A wrapper around iconv_open */
43 create_iconv (const char* tocode, const char* fromcode)
45 iconv_t conv = iconv_open (tocode, fromcode);
47 /* I don't think it's safe to translate this string or to use messaging
48 as the convertors have not yet been set up */
49 if ( (iconv_t) -1 == conv && 0 != strcmp (tocode, fromcode))
51 const int err = errno;
53 "Warning: cannot create a convertor for \"%s\" to \"%s\": %s\n",
54 fromcode, tocode, strerror (err));
60 /* Return a string based on TEXT converted according to HOW.
61 If length is not -1, then it must be the number of bytes in TEXT.
62 The returned string must be freed when no longer required.
65 recode_string (enum conv_id how, const char *text, int length)
68 size_t outbufferlength;
74 /* FIXME: Need to ensure that this char is valid in the target encoding */
75 const char fallbackchar = '?';
81 length = strlen(text);
83 assert (how < n_CONV);
85 if (convertor[how] == (iconv_t) -1)
86 return xstrndup (text, length);
88 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
89 if ( outbufferlength > length)
92 outbuf = xmalloc(outbufferlength);
95 outbytes = outbufferlength;
99 const char *ip = text;
100 result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
105 int the_error = errno;
113 *op++ = fallbackchar;
122 outbufferlength <<= 1;
123 outbuf = xmalloc (outbufferlength);
125 outbytes = outbufferlength;
130 /* should never happen */
135 } while ( -1 == result );
139 char *const oldaddr = outbuf;
140 outbuf = xrealloc (outbuf, outbufferlength + 1);
142 op += (outbuf - oldaddr) ;
151 /* Returns the current PSPP locale */
153 get_pspp_locale (void)
159 /* Set the PSPP locale */
161 set_pspp_locale (const char *l)
163 char *current_locale;
164 const char *current_charset;
169 current_locale = setlocale (LC_CTYPE, 0);
170 current_charset = locale_charset ();
171 setlocale (LC_CTYPE, locale);
173 charset = locale_charset ();
174 setlocale (LC_CTYPE, current_locale);
176 iconv_close (convertor[CONV_PSPP_TO_UTF8]);
177 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
179 iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
180 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
182 iconv_close (convertor[CONV_UTF8_TO_PSPP]);
183 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
190 locale = strdup (setlocale (LC_CTYPE, NULL));
192 setlocale (LC_CTYPE, locale);
193 charset = locale_charset ();
195 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
196 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
197 convertor[CONV_UTF8_TO_PSPP] = create_iconv (charset, "UTF-8");
208 for(i = 0 ; i < n_CONV; ++i )
210 if ( (iconv_t) -1 == convertor[i] )
212 iconv_close (convertor[i]);