1 /* Unicode character output to streams with locale dependent encoding.
3 Copyright (C) 2000 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>. */
42 # define ENOTSUP EINVAL
49 /* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
50 installation; work around this configuration error. */
64 # define _(Text) gettext (Text)
69 #include "unicodeio.h"
71 #if __STDC_ISO_10646__ && HAVE_WCTOMB
73 /* Values of type wchar_t are Unicode code points. */
75 /* Place into BUF the locale-dependent representation of the character
76 CODE. Return the size of the result. If there is a conversion
77 error, return -1, setting errno appropriately. Assumes that the
78 locale doesn't change between two calls. */
80 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
84 /* Test for truncation before invoking wctomb. */
85 return wc == code ? wctomb (buf, wc) : -1;
90 /* When we pass a Unicode character to iconv(), we must pass it in a
91 suitable encoding. The standardized Unicode encodings are
92 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
93 UCS-2 supports only characters up to \U0000FFFF.
94 UTF-16 and variants support only characters up to \U0010FFFF.
95 UTF-7 is way too complex and not supported by glibc-2.1.
96 UCS-4 specification leaves doubts about endianness and byte order
97 mark. glibc currently interprets it as big endian without byte order
98 mark, but this is not backed by an RFC.
99 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
100 unambiguously defined. */
102 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
103 Returns the number of bytes stored, or -1 if wc is out of range. */
105 utf8_wctomb (unsigned char *r, unsigned int wc)
113 else if (wc < 0x10000)
115 else if (wc < 0x200000)
117 else if (wc < 0x4000000)
119 else if (wc <= 0x7fffffff)
126 /* Note: code falls through cases! */
127 case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
128 case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
129 case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
130 case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
131 case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
138 /* Luckily, the encoding's name is platform independent. */
139 # define UTF8_NAME "UTF-8"
141 /* Place into BUF the locale-dependent representation of the character
142 CODE. Return the size of the result. If there is a conversion
143 error, return -1, setting errno appropriately. Assumes that the
144 locale doesn't change between two calls. */
146 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
148 static int initialized;
151 static iconv_t utf8_to_local;
156 extern const char *locale_charset PARAMS ((void));
157 const char *charset = locale_charset ();
159 is_utf8 = (charset != NULL && !strcmp (charset, UTF8_NAME));
163 utf8_to_local = (charset != NULL
164 ? iconv_open (charset, UTF8_NAME)
166 if (utf8_to_local == (iconv_t) -1)
168 /* For an unknown encoding, assume ASCII. */
169 utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
170 if (utf8_to_local == (iconv_t) -1)
181 /* Convert the character to UTF-8. */
183 return utf8_wctomb ((unsigned char *) buf, code);
188 const char *inptr = inbuf;
189 size_t inbytesleft = utf8_wctomb ((unsigned char *) inbuf, code);
191 size_t outbytesleft = MB_LEN_MAX;
194 if (inbytesleft == (size_t) -1)
197 /* Convert the character from UTF-8 to the locale's charset. */
198 res = iconv (utf8_to_local, &inptr, &inbytesleft, &outptr, &outbytesleft);
199 if (inbytesleft > 0 || res == (size_t) -1
200 /* Irix iconv() inserts a NUL byte if it cannot convert. */
201 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
202 || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
207 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
208 # if defined _LIBICONV_VERSION \
209 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
211 /* Get back to the initial shift state. */
212 return iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
225 /* Output the Unicode character CODE to the output stream STREAM. */
227 print_unicode_char (FILE *stream, unsigned int code)
229 char buf[MB_LEN_MAX];
230 size_t s = convert_unicode_char (buf, code);
232 if (s == (size_t) -1)
233 error (1, errno, _("cannot convert U+%04X to local character set"), code);
235 fwrite (buf, 1, s, stream);