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>. */
39 /* Name of UCS-4 encoding with machine dependent endianness and alignment. */
40 # ifdef _LIBICONV_VERSION
41 # define UCS4_NAME "UCS-4-INTERNAL"
43 # define UCS4_NAME "INTERNAL"
51 # define _(Text) gettext (Text)
56 #include "unicodeio.h"
58 /* Use md5.h for its nice detection of unsigned 32-bit type. */
61 #define uint32_t md5_uint32
63 /* Outputs the Unicode character CODE to the output stream STREAM.
64 Assumes that the locale doesn't change between two calls. */
66 print_unicode_char (FILE *stream, unsigned int code)
69 static int initialized;
70 static iconv_t ucs4_to_local;
82 extern const char *locale_charset (void);
83 const char *charset = locale_charset ();
85 ucs4_to_local = (charset != NULL
86 ? iconv_open (charset, UCS4_NAME)
88 if (ucs4_to_local == (iconv_t)(-1))
90 /* For an unknown encoding, assume ASCII. */
91 ucs4_to_local = iconv_open ("ASCII", UCS4_NAME);
92 if (ucs4_to_local == (iconv_t)(-1))
93 error (1, 0, _("cannot output U+%04X: iconv function not usable"),
100 inptr = (char *) ∈
101 inbytesleft = sizeof (in);
103 outbytesleft = sizeof (outbuf);
105 /* Convert the character. */
106 res = iconv (ucs4_to_local, &inptr, &inbytesleft, &outptr, &outbytesleft);
107 if (inbytesleft > 0 || res == (size_t)(-1))
108 error (1, res == (size_t)(-1) ? errno : 0,
109 _("cannot convert U+%04X to local character set"), code);
111 /* Avoid glibc-2.1 bug. */
112 # if defined _LIBICONV_VERSION || !(__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1)
114 /* Get back to the initial shift state. */
115 res = iconv (ucs4_to_local, NULL, NULL, &outptr, &outbytesleft);
116 if (res == (size_t)(-1))
117 error (1, errno, _("cannot convert U+%04X to local character set"), code);
121 fwrite (outbuf, 1, outptr - outbuf, stream);
124 error (1, 0, _("cannot output U+%04X: iconv function not available"), code);