1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "assertion.h"
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;
75 /* FIXME: Need to ensure that this char is valid in the target encoding */
76 const char fallbackchar = '?';
82 length = strlen(text);
84 assert (how < n_CONV);
86 if (convertor[how] == (iconv_t) -1)
87 return xstrndup (text, length);
89 for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
90 if ( outbufferlength > length)
93 outbuf = xmalloc(outbufferlength);
96 outbytes = outbufferlength;
100 const char *ip = text;
101 result = iconv (convertor[how], (ICONV_CONST char **) &text, &inbytes,
106 int the_error = errno;
114 *op++ = fallbackchar;
123 outbufferlength <<= 1;
124 outbuf = xmalloc (outbufferlength);
126 outbytes = outbufferlength;
131 /* should never happen */
136 } while ( -1 == result );
140 char *const oldaddr = outbuf;
141 outbuf = xrealloc (outbuf, outbufferlength + 1);
143 op += (outbuf - oldaddr) ;
152 /* Returns the current PSPP locale */
154 get_pspp_locale (void)
160 /* Set the PSPP locale */
162 set_pspp_locale (const char *l)
164 char *current_locale;
165 const char *current_charset;
170 current_locale = setlocale (LC_CTYPE, 0);
171 current_charset = locale_charset ();
172 setlocale (LC_CTYPE, locale);
174 charset = locale_charset ();
175 setlocale (LC_CTYPE, current_locale);
177 iconv_close (convertor[CONV_PSPP_TO_UTF8]);
178 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
180 iconv_close (convertor[CONV_SYSTEM_TO_PSPP]);
181 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, current_charset);
188 locale = strdup (setlocale (LC_CTYPE, NULL));
190 setlocale (LC_CTYPE, locale);
191 charset = locale_charset ();
193 convertor[CONV_PSPP_TO_UTF8] = create_iconv ("UTF-8", charset);
194 convertor[CONV_SYSTEM_TO_PSPP] = create_iconv (charset, charset);
205 for(i = 0 ; i < n_CONV; ++i )
207 if ( (iconv_t) -1 == convertor[i] )
209 iconv_close (convertor[i]);