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>. */
50 # define _(Text) gettext (Text)
55 #include "unicodeio.h"
57 /* When we pass a Unicode character to iconv(), we must pass it in a
58 suitable encoding. The standardized Unicode encodings are
59 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
60 UCS-2 supports only characters up to \U0000FFFF.
61 UTF-16 and variants support only characters up to \U0010FFFF.
62 UTF-7 is way too complex and not supported by glibc-2.1.
63 UCS-4 specification leaves doubts about endianness and byte order
64 mark. glibc currently interprets it as big endian without byte order
65 mark, but this is not backed by an RFC.
66 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
67 unambiguously defined. */
69 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
70 Returns the number of bytes stored, or -1 if wc is out of range. */
72 utf8_wctomb (unsigned char *r
, unsigned int wc
)
80 else if (wc
< 0x10000)
82 else if (wc
< 0x200000)
84 else if (wc
< 0x4000000)
86 else if (wc
<= 0x7fffffff)
93 /* Note: code falls through cases! */
94 case 6: r
[5] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x4000000;
95 case 5: r
[4] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x200000;
96 case 4: r
[3] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x10000;
97 case 3: r
[2] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x800;
98 case 2: r
[1] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0xc0;
105 /* Luckily, the encoding's name is platform independent. */
106 #define UTF8_NAME "UTF-8"
108 /* Outputs the Unicode character CODE to the output stream STREAM.
109 Assumes that the locale doesn't change between two calls. */
111 print_unicode_char (FILE *stream
, unsigned int code
)
113 static int initialized
;
116 static iconv_t utf8_to_local
;
124 extern const char *locale_charset
PARAMS ((void));
125 const char *charset
= locale_charset ();
127 is_utf8
= (charset
!= NULL
&& !strcmp (charset
, UTF8_NAME
));
131 utf8_to_local
= (charset
!= NULL
132 ? iconv_open (charset
, UTF8_NAME
)
134 if (utf8_to_local
== (iconv_t
)(-1))
136 /* For an unknown encoding, assume ASCII. */
137 utf8_to_local
= iconv_open ("ASCII", UTF8_NAME
);
138 if (utf8_to_local
== (iconv_t
)(-1))
140 _("cannot output U+%04X: iconv function not usable"),
148 /* Convert the character to UTF-8. */
149 count
= utf8_wctomb ((unsigned char *) inbuf
, code
);
151 error (1, 0, _("U+%04X: character out of range"), code
);
155 fwrite (inbuf
, 1, count
, stream
);
170 outbytesleft
= sizeof (outbuf
);
172 /* Convert the character from UTF-8 to the locale's charset. */
173 res
= iconv (utf8_to_local
, &inptr
, &inbytesleft
, &outptr
, &outbytesleft
);
174 if (inbytesleft
> 0 || res
== (size_t)(-1)
175 /* Irix iconv() inserts a NUL byte if it cannot convert. */
176 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
177 || (res
> 0 && code
!= 0 && outptr
- outbuf
== 1 && *outbuf
== '\0')
180 error (1, res
== (size_t)(-1) ? errno
: 0,
181 _("cannot convert U+%04X to local character set"), code
);
183 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
184 # if defined _LIBICONV_VERSION \
185 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
187 /* Get back to the initial shift state. */
188 res
= iconv (utf8_to_local
, NULL
, NULL
, &outptr
, &outbytesleft
);
189 if (res
== (size_t)(-1))
190 error (1, errno
, _("cannot convert U+%04X to local character set"),
194 fwrite (outbuf
, 1, outptr
- outbuf
, stream
);
196 error (1, 0, _("cannot output U+%04X: iconv function not available"),