summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2008-12-21 16:58:29 +0100
committerBruno Haible <bruno@clisp.org>2008-12-21 16:58:29 +0100
commit5680a72abf25da38840cb77697ae9b39fdf461b0 (patch)
tree45a019705291ffc0ffc63685ad9ae3c663cfbad8
parent3d9a7aaf49a48b507300643e444049dac8b5563a (diff)
downloadgnulib-5680a72abf25da38840cb77697ae9b39fdf461b0.tar.gz
New module 'wcrtomb'.
-rw-r--r--ChangeLog12
-rw-r--r--doc/posix-functions/wcrtomb.texi8
-rw-r--r--lib/wchar.in.h14
-rw-r--r--lib/wcrtomb.c53
-rw-r--r--m4/wchar.m44
-rw-r--r--m4/wcrtomb.m424
-rw-r--r--modules/wchar2
-rw-r--r--modules/wcrtomb27
8 files changed, 139 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index a6d640ce99..6554650c3a 100644
--- 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.
diff --git a/doc/posix-functions/wcrtomb.texi b/doc/posix-functions/wcrtomb.texi
index 7d39a03e99..0f41b2f906 100644
--- a/doc/posix-functions/wcrtomb.texi
+++ b/doc/posix-functions/wcrtomb.texi
@@ -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
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index bf90415006..6bb9cbf8ed 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -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
index 0000000000..79df99f235
--- /dev/null
+++ b/lib/wcrtomb.c
@@ -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);
+ }
+ }
+}
diff --git a/m4/wchar.m4 b/m4/wchar.m4
index b907939d12..40c43b7957 100644
--- a/m4/wchar.m4
+++ b/m4/wchar.m4
@@ -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
index 0000000000..4678d5c04b
--- /dev/null
+++ b/m4/wcrtomb.m4
@@ -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], [
+ :
+])
diff --git a/modules/wchar b/modules/wchar
index 22724c1fe9..d829b3ab53 100644
--- a/modules/wchar
+++ b/modules/wchar
@@ -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
index 0000000000..ed72185ca2
--- /dev/null
+++ b/modules/wcrtomb
@@ -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
+