(<errno.h>): Include it.
[gnulib.git] / lib / unicodeio.c
1 /* Unicode character output to streams with locale dependent encoding.
2
3    Copyright (C) 2000 Free Software Foundation, Inc.
4
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)
8    any later version.
9
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.
14
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,
18    USA.  */
19
20 /* Written by Bruno Haible <haible@clisp.cons.org>.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #if HAVE_STDDEF_H
27 # include <stddef.h>
28 #endif
29
30 #include <stdio.h>
31
32 #include <errno.h>
33 #ifndef errno
34 extern int errno;
35 #endif
36
37 #if HAVE_ICONV
38 # include <iconv.h>
39 /* Name of UCS-4 encoding with machine dependent endianness and alignment.  */
40 # ifdef _LIBICONV_VERSION
41 #  define UCS4_NAME "UCS-4-INTERNAL"
42 # else
43 #  define UCS4_NAME "INTERNAL"
44 # endif
45 #endif
46
47 #include <error.h>
48
49 #if ENABLE_NLS
50 # include <libintl.h>
51 # define _(Text) gettext (Text)
52 #else
53 # define _(Text) Text
54 #endif
55
56 #include "unicodeio.h"
57
58 /* Use md5.h for its nice detection of unsigned 32-bit type.  */
59 #include "md5.h"
60 #undef uint32_t
61 #define uint32_t md5_uint32
62
63 /* Outputs the Unicode character CODE to the output stream STREAM.
64    Assumes that the locale doesn't change between two calls.  */
65 void
66 print_unicode_char (FILE *stream, unsigned int code)
67 {
68 #if HAVE_ICONV
69   static int initialized;
70   static iconv_t ucs4_to_local;
71
72   uint32_t in;
73   char outbuf[25];
74   const char *inptr;
75   size_t inbytesleft;
76   char *outptr;
77   size_t outbytesleft;
78   size_t res;
79
80   if (!initialized)
81     {
82       extern const char *locale_charset (void);
83       const char *charset = locale_charset ();
84
85       ucs4_to_local = (charset != NULL
86                        ? iconv_open (charset, UCS4_NAME)
87                        : (iconv_t)(-1));
88       if (ucs4_to_local == (iconv_t)(-1))
89         {
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"),
94                    code);
95         }
96       initialized = 1;
97     }
98
99   in = code;
100   inptr = (char *) &in;
101   inbytesleft = sizeof (in);
102   outptr = outbuf;
103   outbytesleft = sizeof (outbuf);
104
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);
110
111   /* Avoid glibc-2.1 bug.  */
112 # if defined _LIBICONV_VERSION || !(__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1)
113
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);
118
119 # endif
120
121   fwrite (outbuf, 1, outptr - outbuf, stream);
122
123 #else
124   error (1, 0, _("cannot output U+%04X: iconv function not available"), code);
125 #endif
126 }