now-generated
[gnulib.git] / lib / unicodeio.c
index 4f14adf..9f90366 100644 (file)
@@ -1,6 +1,6 @@
 /* Unicode character output to streams with locale dependent encoding.
 
-   Copyright (C) 2000 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published
 extern int errno;
 #endif
 
-#if HAVE_WCHAR_H
-# include <wchar.h>
-#endif
-
-/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
-#if HAVE_WCRTOMB && defined mbstate_t
-# define wcrtomb(s, wc, ps) (wcrtomb) (s, wc, 0)
-#endif
-
 #if HAVE_ICONV
 # include <iconv.h>
 #endif
@@ -63,39 +54,6 @@ extern int errno;
 
 #include "unicodeio.h"
 
-#if __STDC_ISO_10646__ && HAVE_WCRTOMB
-
-/* Values of type wchar_t are Unicode code points.  */
-
-/* Outputs the Unicode character CODE to the output stream STREAM.
-   Assumes that the locale doesn't change between two calls.  */
-void
-print_unicode_char (FILE *stream, unsigned int code)
-{
-  wchar_t wc = (wchar_t) code;
-
-  /* Test for truncation.  */
-  if (wc == code)
-    {
-      /* Convert from wide character to multibyte representation.  */
-      char buf[64]; /* Assume MB_LEN_MAX <= 64.  */
-      mbstate_t state;
-      size_t res;
-
-      memset (&state, 0, sizeof (mbstate_t));
-      res = wcrtomb (buf, wc, &state);
-      if (res != (size_t)(-1))
-       fwrite (buf, 1, res, stream);
-      else
-       error (1, errno,
-              _("cannot convert U+%04X to local character set"), code);
-    }
-  else
-    error (1, 0, _("cannot convert U+%04X to local character set"), code);
-}
-
-#else
-
 /* When we pass a Unicode character to iconv(), we must pass it in a
    suitable encoding. The standardized Unicode encodings are
    UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
@@ -145,7 +103,7 @@ utf8_wctomb (unsigned char *r, unsigned int wc)
 }
 
 /* Luckily, the encoding's name is platform independent.  */
-# define UTF8_NAME "UTF-8"
+#define UTF8_NAME "UTF-8"
 
 /* Outputs the Unicode character CODE to the output stream STREAM.
    Assumes that the locale doesn't change between two calls.  */
@@ -154,9 +112,9 @@ print_unicode_char (FILE *stream, unsigned int code)
 {
   static int initialized;
   static int is_utf8;
-# if HAVE_ICONV
+#if HAVE_ICONV
   static iconv_t utf8_to_local;
-# endif
+#endif
 
   char inbuf[6];
   int count;
@@ -166,13 +124,11 @@ print_unicode_char (FILE *stream, unsigned int code)
       extern const char *locale_charset PARAMS ((void));
       const char *charset = locale_charset ();
 
-      is_utf8 = (charset != NULL && !strcmp (charset, UTF8_NAME));
-# if HAVE_ICONV
+      is_utf8 = !strcmp (charset, UTF8_NAME);
+#if HAVE_ICONV
       if (!is_utf8)
        {
-         utf8_to_local = (charset != NULL
-                          ? iconv_open (charset, UTF8_NAME)
-                          : (iconv_t)(-1));
+         utf8_to_local = iconv_open (charset, UTF8_NAME);
          if (utf8_to_local == (iconv_t)(-1))
            {
              /* For an unknown encoding, assume ASCII.  */
@@ -183,7 +139,7 @@ print_unicode_char (FILE *stream, unsigned int code)
                       code);
            }
        }
-# endif
+#endif
       initialized = 1;
     }
 
@@ -198,7 +154,7 @@ print_unicode_char (FILE *stream, unsigned int code)
     }
   else
     {
-# if HAVE_ICONV
+#if HAVE_ICONV
       char outbuf[25];
       const char *inptr;
       size_t inbytesleft;
@@ -212,18 +168,20 @@ print_unicode_char (FILE *stream, unsigned int code)
       outbytesleft = sizeof (outbuf);
 
       /* Convert the character from UTF-8 to the locale's charset.  */
-      res = iconv (utf8_to_local, &inptr, &inbytesleft, &outptr, &outbytesleft);
+      res = iconv (utf8_to_local,
+                  (ICONV_CONST char **)&inptr, &inbytesleft,
+                  &outptr, &outbytesleft);
       if (inbytesleft > 0 || res == (size_t)(-1)
          /* Irix iconv() inserts a NUL byte if it cannot convert. */
-#  if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
+# if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
          || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
-#  endif
+# endif
          )
        error (1, res == (size_t)(-1) ? errno : 0,
               _("cannot convert U+%04X to local character set"), code);
 
       /* Avoid glibc-2.1 bug and Solaris 2.7 bug.  */
-#  if defined _LIBICONV_VERSION \
+# if defined _LIBICONV_VERSION \
     || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
 
       /* Get back to the initial shift state.  */
@@ -231,14 +189,12 @@ print_unicode_char (FILE *stream, unsigned int code)
       if (res == (size_t)(-1))
        error (1, errno, _("cannot convert U+%04X to local character set"),
               code);
-#  endif
+# endif
 
       fwrite (outbuf, 1, outptr - outbuf, stream);
-# else
+#else
       error (1, 0, _("cannot output U+%04X: iconv function not available"),
             code);
-# endif
+#endif
     }
 }
-
-#endif