New module 'wcrtomb'.
authorBruno Haible <bruno@clisp.org>
Sun, 21 Dec 2008 15:58:29 +0000 (16:58 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Dec 2008 15:58:29 +0000 (16:58 +0100)
ChangeLog
doc/posix-functions/wcrtomb.texi
lib/wchar.in.h
lib/wcrtomb.c [new file with mode: 0644]
m4/wchar.m4
m4/wcrtomb.m4 [new file with mode: 0644]
modules/wchar
modules/wcrtomb [new file with mode: 0644]

index a6d640c..6554650 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2008-12-21  Bruno Haible  <bruno@clisp.org>
 
+       * lib/wchar.in.h (wcrtomb): New declaration.
+       * lib/wcrtomb.c: New file.
+       * m4/wcrtomb.m4: New file.
+       * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCRTOMB,
+       HAVE_WCRTOMB.
+       * modules/wchar (Makefile.am): Substitute GNULIB_WCRTOMB,
+       HAVE_WCRTOMB.
+       * modules/wcrtomb: New file.
+       * doc/posix-functions/wcrtomb.texi: Mention the new module.
+
+2008-12-21  Bruno Haible  <bruno@clisp.org>
+
        * modules/mbrtowc (Files): Add m4/codeset.m4, needed by m4/locale-fr.m4.
        * modules/mbsrtowcs (Files): Likewise.
        * modules/wctob (Files): Likewise.
index 7d39a03..0f41b2f 100644 (file)
@@ -4,18 +4,18 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wcrtomb.html}
 
-Gnulib module: ---
+Gnulib module: wcrtomb
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+HP-UX 11.00, IRIX 6.5, Solaris 2.6, mingw, Interix 3.5.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on some platforms:
-HP-UX 11.00, IRIX 6.5, Solaris 2.6, mingw, Interix 3.5.
-@item
 On AIX and Windows platforms, @code{wchar_t} is a 16-bit type and therefore cannot
 accommodate all Unicode characters.
 @end itemize
index bf90415..6bb9cbf 100644 (file)
@@ -201,6 +201,20 @@ extern size_t mbsnrtowcs (wchar_t *dest, const char **srcp, size_t srclen, size_
 #endif
 
 
+/* Convert a wide character to a multibyte character.  */
+#if @GNULIB_WCRTOMB@
+# if !@HAVE_WCRTOMB@
+extern size_t wcrtomb (char *s, wchar_t wc, mbstate_t *ps);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef wcrtomb
+# define wcrtomb(s,w,p) \
+    (GL_LINK_WARNING ("wcrtomb is unportable - " \
+                      "use gnulib module wcrtomb for portability"), \
+     wcrtomb (s, w, p))
+#endif
+
+
 /* Return the number of screen columns needed for WC.  */
 #if @GNULIB_WCWIDTH@
 # if @REPLACE_WCWIDTH@
diff --git a/lib/wcrtomb.c b/lib/wcrtomb.c
new file mode 100644 (file)
index 0000000..79df99f
--- /dev/null
@@ -0,0 +1,53 @@
+/* Convert wide character to multibyte character.
+   Copyright (C) 2008 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2008.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <wchar.h>
+
+#include <errno.h>
+#include <stdlib.h>
+
+
+size_t
+wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
+{
+  /* This implementation of wcrtomb on top of wctomb() supports only
+     stateless encodings.  ps must be in the initial state.  */
+  if (ps != NULL && !mbsinit (ps))
+    {
+      errno = EINVAL;
+      return (size_t)(-1);
+    }
+
+  if (s == NULL)
+    /* We know the NUL wide character corresponds to the NUL character.  */
+    return 1;
+  else
+    {
+      int ret = wctomb (s, wc);
+
+      if (ret >= 0)
+       return ret;
+      else
+       {
+         errno = EILSEQ;
+         return (size_t)(-1);
+       }
+    }
+}
index b907939..40c43b7 100644 (file)
@@ -7,7 +7,7 @@ dnl with or without modifications, as long as this notice is preserved.
 
 dnl Written by Eric Blake.
 
-# wchar.m4 serial 16
+# wchar.m4 serial 17
 
 AC_DEFUN([gl_WCHAR_H],
 [
@@ -68,6 +68,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
   GNULIB_MBRLEN=0;     AC_SUBST([GNULIB_MBRLEN])
   GNULIB_MBSRTOWCS=0;  AC_SUBST([GNULIB_MBSRTOWCS])
   GNULIB_MBSNRTOWCS=0; AC_SUBST([GNULIB_MBSNRTOWCS])
+  GNULIB_WCRTOMB=0;    AC_SUBST([GNULIB_WCRTOMB])
   GNULIB_WCWIDTH=0;    AC_SUBST([GNULIB_WCWIDTH])
   dnl Assume proper GNU behavior unless another module says otherwise.
   HAVE_BTOWC=1;        AC_SUBST([HAVE_BTOWC])
@@ -76,6 +77,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
   HAVE_MBRLEN=1;       AC_SUBST([HAVE_MBRLEN])
   HAVE_MBSRTOWCS=1;    AC_SUBST([HAVE_MBSRTOWCS])
   HAVE_MBSNRTOWCS=1;   AC_SUBST([HAVE_MBSNRTOWCS])
+  HAVE_WCRTOMB=1;      AC_SUBST([HAVE_WCRTOMB])
   HAVE_DECL_WCTOB=1;   AC_SUBST([HAVE_DECL_WCTOB])
   HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH])
   REPLACE_MBSTATE_T=0; AC_SUBST([REPLACE_MBSTATE_T])
diff --git a/m4/wcrtomb.m4 b/m4/wcrtomb.m4
new file mode 100644 (file)
index 0000000..4678d5c
--- /dev/null
@@ -0,0 +1,24 @@
+# wcrtomb.m4 serial 1
+dnl Copyright (C) 2008 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_WCRTOMB],
+[
+  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
+
+  AC_REQUIRE([AC_TYPE_MBSTATE_T])
+  AC_CHECK_FUNCS_ONCE([wcrtomb])
+  if test $ac_cv_func_wcrtomb = no; then
+    HAVE_WCRTOMB=0
+    gl_REPLACE_WCHAR_H
+    AC_LIBOBJ([wcrtomb])
+    gl_PREREQ_WCRTOMB
+  fi
+])
+
+# Prerequisites of lib/wcrtomb.c.
+AC_DEFUN([gl_PREREQ_WCRTOMB], [
+  :
+])
index 22724c1..d829b3a 100644 (file)
@@ -32,6 +32,7 @@ wchar.h: wchar.in.h
              -e 's|@''GNULIB_MBRLEN''@|$(GNULIB_MBRLEN)|g' \
              -e 's|@''GNULIB_MBSRTOWCS''@|$(GNULIB_MBSRTOWCS)|g' \
              -e 's|@''GNULIB_MBSNRTOWCS''@|$(GNULIB_MBSNRTOWCS)|g' \
+             -e 's|@''GNULIB_WCRTOMB''@|$(GNULIB_WCRTOMB)|g' \
              -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \
              -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \
              -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \
@@ -40,6 +41,7 @@ wchar.h: wchar.in.h
              -e 's|@''HAVE_MBRLEN''@|$(HAVE_MBRLEN)|g' \
              -e 's|@''HAVE_MBSRTOWCS''@|$(HAVE_MBSRTOWCS)|g' \
              -e 's|@''HAVE_MBSNRTOWCS''@|$(HAVE_MBSNRTOWCS)|g' \
+             -e 's|@''HAVE_WCRTOMB''@|$(HAVE_WCRTOMB)|g' \
              -e 's|@''HAVE_DECL_WCTOB''@|$(HAVE_DECL_WCTOB)|g' \
              -e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \
              -e 's|@''REPLACE_MBSTATE_T''@|$(REPLACE_MBSTATE_T)|g' \
diff --git a/modules/wcrtomb b/modules/wcrtomb
new file mode 100644 (file)
index 0000000..ed72185
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+wcrtomb() function: convert wide character to multibyte character.
+
+Files:
+lib/wcrtomb.c
+m4/wcrtomb.m4
+m4/mbstate_t.m4
+
+Depends-on:
+wchar
+mbsinit
+
+configure.ac:
+gl_FUNC_WCRTOMB
+gl_WCHAR_MODULE_INDICATOR([wcrtomb])
+
+Makefile.am:
+
+Include:
+<wchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+