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>. */
57 # define _(Text) gettext (Text)
62 #include "unicodeio.h"
64 #if __STDC_ISO_10646__ && HAVE_WCTOMB
66 /* Values of type wchar_t are Unicode code points. */
68 /* Place into BUF the locale-dependent representation of the character
69 CODE. Return the size of the result. If there is a conversion
70 error, return -1, setting errno appropriately. Assumes that the
71 locale doesn't change between two calls. */
73 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
77 /* Test for truncation before invoking wctomb. */
78 return wc == code ? wctomb (buf, wc) : -1;
83 /* When we pass a Unicode character to iconv(), we must pass it in a
84 suitable encoding. The standardized Unicode encodings are
85 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
86 UCS-2 supports only characters up to \U0000FFFF.
87 UTF-16 and variants support only characters up to \U0010FFFF.
88 UTF-7 is way too complex and not supported by glibc-2.1.
89 UCS-4 specification leaves doubts about endianness and byte order
90 mark. glibc currently interprets it as big endian without byte order
91 mark, but this is not backed by an RFC.
92 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
93 unambiguously defined. */
95 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
96 Returns the number of bytes stored, or -1 if wc is out of range. */
98 utf8_wctomb (unsigned char *r, unsigned int wc)
106 else if (wc < 0x10000)
108 else if (wc < 0x200000)
110 else if (wc < 0x4000000)
112 else if (wc <= 0x7fffffff)
119 /* Note: code falls through cases! */
120 case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
121 case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
122 case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
123 case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
124 case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
131 /* Luckily, the encoding's name is platform independent. */
132 # define UTF8_NAME "UTF-8"
134 /* Place into BUF the locale-dependent representation of the character
135 CODE. Return the size of the result. If there is a conversion
136 error, return -1, setting errno appropriately. Assumes that the
137 locale doesn't change between two calls. */
139 convert_unicode_char (char buf[MB_LEN_MAX], unsigned int code)
141 static int initialized;
144 static iconv_t utf8_to_local;
149 extern const char *locale_charset PARAMS ((void));
150 const char *charset = locale_charset ();
152 is_utf8 = (charset != NULL && !strcmp (charset, UTF8_NAME));
156 utf8_to_local = (charset != NULL
157 ? iconv_open (charset, UTF8_NAME)
159 if (utf8_to_local == (iconv_t) -1)
161 /* For an unknown encoding, assume ASCII. */
162 utf8_to_local = iconv_open ("ASCII", UTF8_NAME);
163 if (utf8_to_local == (iconv_t) -1)
174 /* Convert the character to UTF-8. */
176 return utf8_wctomb ((unsigned char *) buf, code);
181 const char *inptr = inbuf;
182 size_t inbytesleft = utf8_wctomb ((unsigned char *) inbuf, code);
184 size_t outbytesleft = MB_LEN_MAX;
187 if (inbytesleft == (size_t) -1)
190 /* Convert the character from UTF-8 to the locale's charset. */
191 res = iconv (utf8_to_local, &inptr, &inbytesleft, &outptr, &outbytesleft);
192 if (inbytesleft > 0 || res == (size_t) -1
193 /* Irix iconv() inserts a NUL byte if it cannot convert. */
194 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
195 || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
200 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
201 # if defined _LIBICONV_VERSION \
202 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
204 /* Get back to the initial shift state. */
205 return iconv (utf8_to_local, NULL, NULL, &outptr, &outbytesleft);
218 /* Output the Unicode character CODE to the output stream STREAM. */
220 print_unicode_char (FILE *stream, unsigned int code)
222 char buf[MB_LEN_MAX];
223 size_t s = convert_unicode_char (buf, code);
225 if (s == (size_t) -1)
226 error (1, errno, _("cannot convert U+%04X to local character set"), code);
228 fwrite (buf, 1, s, stream);