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>. */
46 /* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
47 installation; work around this configuration error. */
61 # define _(Text) gettext (Text)
66 #include "unicodeio.h"
68 #if __STDC_ISO_10646__ && HAVE_WCTOMB
70 /* Values of type wchar_t are Unicode code points. */
72 /* Place into BUF the locale-dependent representation of the character
73 CODE. Return the size of the result. If there is a conversion
74 error, return -1, setting errno appropriately. Assumes that the
75 locale doesn't change between two calls. */
77 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
81 /* Test for truncation before invoking wctomb. */
82 return wc == code ? wctomb (buf, wc) : -1;
87 /* When we pass a Unicode character to iconv(), we must pass it in a
88 suitable encoding. The standardized Unicode encodings are
89 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
90 UCS-2 supports only characters up to \U0000FFFF.
91 UTF-16 and variants support only characters up to \U0010FFFF.
92 UTF-7 is way too complex and not supported by glibc-2.1.
93 UCS-4 specification leaves doubts about endianness and byte order
94 mark. glibc currently interprets it as big endian without byte order
95 mark, but this is not backed by an RFC.
96 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
97 unambiguously defined. */
99 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
100 Returns the number of bytes stored, or -1 if wc is out of range. */
102 utf8_wctomb (unsigned char *r, unsigned int wc)
110 else if (wc < 0x10000)
112 else if (wc < 0x200000)
114 else if (wc < 0x4000000)
116 else if (wc <= 0x7fffffff)
123 /* Note: code falls through cases! */
124 case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
125 case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
126 case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
127 case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
128 case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
135 /* Luckily, the encoding's name is platform independent. */
136 # define UTF8_NAME "UTF-8"
138 /* Place into BUF the locale-dependent representation of the character
139 CODE. Return the size of the result. If there is a conversion
140 error, return -1, setting errno appropriately. Assumes that the
141 locale doesn't change between two calls. */
143 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
145 static int initialized;
148 static iconv_t utf8_to_local;
153 extern const char *locale_charset PARAMS ((void));
154 const char *charset = locale_charset ();
156 is_utf8 = (charset != NULL && !strcmp (charset, UTF8_NAME));
160 utf8_to_local = (charset != NULL
161 ? iconv_open (charset, UTF8_NAME)
163 if (utf8_to_local == (iconv_t) -1)
165 /* For an unknown encoding, assume ASCII. */
166 utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
167 if (utf8_to_local == (iconv_t) -1)
178 /* Convert the character to UTF-8. */
180 return utf8_wctomb ((unsigned char *) buf, code);
185 const char *inptr = inbuf;
186 size_t inbytesleft = utf8_wctomb ((unsigned char *) inbuf, code);
188 size_t outbytesleft = MB_LEN_MAX;
191 if (inbytesleft == (size_t) -1)
194 /* Convert the character from UTF-8 to the locale's charset. */
195 res = iconv (utf8_to_local, &inptr, &inbytesleft, &outptr, &outbytesleft);
196 if (inbytesleft > 0 || res == (size_t) -1
197 /* Irix iconv() inserts a NUL byte if it cannot convert. */
198 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
199 || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
204 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
205 # if defined _LIBICONV_VERSION \
206 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
208 /* Get back to the initial shift state. */
209 return iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
222 /* Output the Unicode character CODE to the output stream STREAM. */
224 print_unicode_char (FILE *stream, unsigned int code)
226 char buf[MB_LEN_MAX];
227 size_t s = convert_unicode_char (buf, code);
229 if (s == (size_t) -1)
230 error (1, errno, _("cannot convert U+%04X to local character set"), code);
232 fwrite (buf, 1, s, stream);