1 /* Unicode character output to streams with locale dependent encoding.
3 Copyright (C) 2000-2002 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Written by Bruno Haible <haible@clisp.cons.org>. */
22 /* Note: This file requires the locale_charset() function. See in
23 libiconv-1.8/libcharset/INTEGRATE for how to obtain it. */
30 #include "unicodeio.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
54 /* When we pass a Unicode character to iconv(), we must pass it in a
55 suitable encoding. The standardized Unicode encodings are
56 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
57 UCS-2 supports only characters up to \U0000FFFF.
58 UTF-16 and variants support only characters up to \U0010FFFF.
59 UTF-7 is way too complex and not supported by glibc-2.1.
60 UCS-4 specification leaves doubts about endianness and byte order
61 mark. glibc currently interprets it as big endian without byte order
62 mark, but this is not backed by an RFC.
63 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
64 unambiguously defined. */
66 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
67 Returns the number of bytes stored, or -1 if wc is out of range. */
69 utf8_wctomb (unsigned char *r, unsigned int wc)
77 else if (wc < 0x10000)
79 else if (wc < 0x200000)
81 else if (wc < 0x4000000)
83 else if (wc <= 0x7fffffff)
90 /* Note: code falls through cases! */
91 case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
92 case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
93 case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
94 case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
95 case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
102 /* Luckily, the encoding's name is platform independent. */
103 #define UTF8_NAME "UTF-8"
105 /* Converts the Unicode character CODE to its multibyte representation
106 in the current locale and calls the SUCCESS callback on the resulting
107 byte sequence. If an error occurs, invokes the FAILURE callback instead,
108 passing it CODE and an English error string.
109 Returns whatever the callback returned.
110 Assumes that the locale doesn't change between two calls. */
112 unicode_to_mb (unsigned int code,
113 long (*success) PARAMS ((const char *buf, size_t buflen,
114 void *callback_arg)),
115 long (*failure) PARAMS ((unsigned int code, const char *msg,
116 void *callback_arg)),
119 static int initialized;
122 static iconv_t utf8_to_local;
130 extern const char *locale_charset PARAMS ((void));
131 const char *charset = locale_charset ();
133 is_utf8 = !strcmp (charset, UTF8_NAME);
137 utf8_to_local = iconv_open (charset, UTF8_NAME);
138 if (utf8_to_local == (iconv_t)(-1))
139 /* For an unknown encoding, assume ASCII. */
140 utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
146 /* Test whether the utf8_to_local converter is available at all. */
150 if (utf8_to_local == (iconv_t)(-1))
151 return failure (code, N_("iconv function not usable"), callback_arg);
153 return failure (code, N_("iconv function not available"), callback_arg);
157 /* Convert the character to UTF-8. */
158 count = utf8_wctomb ((unsigned char *) inbuf, code);
160 return failure (code, N_("character out of range"), callback_arg);
175 outbytesleft = sizeof (outbuf);
177 /* Convert the character from UTF-8 to the locale's charset. */
178 res = iconv (utf8_to_local,
179 (ICONV_CONST char **)&inptr, &inbytesleft,
180 &outptr, &outbytesleft);
181 if (inbytesleft > 0 || res == (size_t)(-1)
182 /* Irix iconv() inserts a NUL byte if it cannot convert. */
183 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
184 || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
187 return failure (code, NULL, callback_arg);
189 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
190 # if defined _LIBICONV_VERSION \
191 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
193 /* Get back to the initial shift state. */
194 res = iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
195 if (res == (size_t)(-1))
196 return failure (code, NULL, callback_arg);
199 return success (outbuf, outptr - outbuf, callback_arg);
203 /* At this point, is_utf8 is true, so no conversion is needed. */
204 return success (inbuf, count, callback_arg);
207 /* Simple success callback that outputs the converted string.
208 The STREAM is passed as callback_arg. */
210 fwrite_success_callback (const char *buf, size_t buflen, void *callback_arg)
212 FILE *stream = (FILE *) callback_arg;
214 fwrite (buf, 1, buflen, stream);
218 /* Simple failure callback that displays an error and exits. */
220 exit_failure_callback (unsigned int code, const char *msg, void *callback_arg)
223 error (1, 0, _("cannot convert U+%04X to local character set"), code);
225 error (1, 0, _("cannot convert U+%04X to local character set: %s"), code,
230 /* Simple failure callback that displays a fallback representation in plain
231 ASCII, using the same notation as ISO C99 strings. */
233 fallback_failure_callback (unsigned int code, const char *msg, void *callback_arg)
235 FILE *stream = (FILE *) callback_arg;
238 fprintf (stream, "\\u%04X", code);
240 fprintf (stream, "\\U%08X", code);
244 /* Outputs the Unicode character CODE to the output stream STREAM.
245 Upon failure, exit if exit_on_error is true, otherwise output a fallback
248 print_unicode_char (FILE *stream, unsigned int code, int exit_on_error)
250 unicode_to_mb (code, fwrite_success_callback,
252 ? exit_failure_callback
253 : fallback_failure_callback,