changeset 10943:47cd92ce639a

New module 'wcsnrtombs'.
author Bruno Haible <bruno@clisp.org>
date Mon, 22 Dec 2008 00:42:11 +0100
parents d3a1e8911dfa
children f08a483da33c
files ChangeLog doc/posix-functions/wcsnrtombs.texi lib/wchar.in.h lib/wcsnrtombs.c lib/wcsrtombs-state.c lib/wcsrtombs.c m4/wchar.m4 m4/wcsnrtombs.m4 m4/wcsrtombs.m4 modules/wchar modules/wcsnrtombs modules/wcsrtombs
diffstat 12 files changed, 229 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2008-12-21  Bruno Haible  <bruno@clisp.org>
+
+	New module 'wcsnrtombs'.
+	* lib/wchar.in.h (wcsnrtombs): New declaration.
+	* lib/wcsnrtombs.c: New file.
+	* lib/wcsrtombs-state.c: New file.
+	* lib/wcsrtombs.c: Refer to _gl_wcsrtombs_state.
+	(internal_state): Remove variable.
+	* m4/wcsnrtombs.m4: New file.
+	* m4/wcsrtombs.m4 (gl_FUNC_WCSRTOMBS): Add wcsrtombs-state.c to the
+	compilation units.
+	* m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCSNRTOMBS,
+	HAVE_WCSNRTOMBS.
+	* modules/wchar (Makefile.am): Substitute GNULIB_WCSNRTOMBS,
+	HAVE_WCSNRTOMBS.
+	* modules/wcsnrtombs: New file.
+	* modules/wcsrtombs (Files): Add lib/wcsrtombs-state.c.
+	* doc/posix-functions/wcsnrtombs.texi: Mention the new module.
+
 2008-12-21  Bruno Haible  <bruno@clisp.org>
 
 	* modules/wcsrtombs-tests: New file.
--- a/doc/posix-functions/wcsnrtombs.texi
+++ b/doc/posix-functions/wcsnrtombs.texi
@@ -4,15 +4,15 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wcsnrtombs.html}
 
-Gnulib module: ---
+Gnulib module: wcsnrtombs
 
 Portability problems fixed by Gnulib:
 @itemize
-@end itemize
-
-Portability problems not fixed by Gnulib:
-@itemize
 @item
 This function is missing on some platforms:
 MacOS X 10.3, FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS.
 @end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -241,6 +241,20 @@
 #endif
 
 
+/* Convert a wide string to a string.  */
+#if @GNULIB_WCSNRTOMBS@
+# if !@HAVE_WCSNRTOMBS@
+extern size_t wcsnrtombs (char *dest, const wchar_t **srcp, size_t srclen, size_t len, mbstate_t *ps);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef wcsnrtombs
+# define wcsnrtombs(d,s,n,l,p) \
+    (GL_LINK_WARNING ("wcsnrtombs is unportable - " \
+                      "use gnulib module wcsnrtombs for portability"), \
+     wcsnrtombs (d, s, n, l, p))
+#endif
+
+
 /* Return the number of screen columns needed for WC.  */
 #if @GNULIB_WCWIDTH@
 # if @REPLACE_WCWIDTH@
new file mode 100644
--- /dev/null
+++ b/lib/wcsnrtombs.c
@@ -0,0 +1,104 @@
+/* Convert wide string to string.
+   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>
+# include <string.h>
+
+
+extern mbstate_t _gl_wcsrtombs_state;
+
+size_t
+wcsnrtombs (char *dest, const wchar_t **srcp, size_t srclen, size_t len, mbstate_t *ps)
+{
+  if (ps == NULL)
+    ps = &_gl_wcsrtombs_state;
+  {
+    const wchar_t *src = *srcp;
+    size_t cur_max = MB_CUR_MAX;
+    char buf[64];
+
+    if (!(cur_max <= sizeof (buf)))
+      abort ();
+
+    if (dest != NULL)
+      {
+	char *destptr = dest;
+
+	for (; srclen > 0 && len > 0; src++, srclen--)
+	  {
+	    wchar_t wc = *src;
+	    size_t ret = wcrtomb (len >= cur_max ? destptr : buf, wc, ps);
+
+	    if (ret == (size_t)(-1))
+	      goto bad_input;
+	    if (!(ret <= cur_max))
+	      abort ();
+	    if (len < ret)
+	      break;
+	    if (len < cur_max)
+	      memcpy (destptr, buf, ret);
+	    if (wc == 0)
+	      {
+		src = NULL;
+		/* Here mbsinit (ps).  */
+		break;
+	      }
+	    destptr += ret;
+	    len -= ret;
+	  }
+	*srcp = src;
+	return destptr - dest;
+      }
+    else
+      {
+	/* Ignore dest and len, don't store *srcp at the end, and
+	   don't clobber *ps.  */
+	mbstate_t state = *ps;
+	size_t totalcount = 0;
+
+	for (; srclen > 0; src++, srclen--)
+	  {
+	    wchar_t wc = *src;
+	    size_t ret = wcrtomb (buf, wc, &state);
+
+	    if (ret == (size_t)(-1))
+	      goto bad_input2;
+	    if (wc == 0)
+	      {
+		/* Here mbsinit (&state).  */
+		break;
+	      }
+	    totalcount += ret;
+	  }
+	return totalcount;
+      }
+
+   bad_input:
+    *srcp = src;
+   bad_input2:
+    errno = EILSEQ;
+    return (size_t)(-1);
+  }
+}
+
+#endif
new file mode 100644
--- /dev/null
+++ b/lib/wcsrtombs-state.c
@@ -0,0 +1,23 @@
+/* Convert wide string to string.
+   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>
+
+#include <wchar.h>
+
+/* Internal state used by the functions wcsrtombs() and wcsnrtombs().  */
+mbstate_t _gl_wcsrtombs_state;
--- a/lib/wcsrtombs.c
+++ b/lib/wcsrtombs.c
@@ -20,7 +20,7 @@
 /* Specification.  */
 #include <wchar.h>
 
-static mbstate_t internal_state;
+extern mbstate_t _gl_wcsrtombs_state;
 
 #if HAVE_WCSRTOMBS && !WCSRTOMBS_TERMINATION_BUG
 /* Override the system's wcsrtombs() function.  */
@@ -31,7 +31,7 @@
 rpl_wcsrtombs (char *dest, const wchar_t **srcp, size_t len, mbstate_t *ps)
 {
   if (ps == NULL)
-    ps = &internal_state;
+    ps = &_gl_wcsrtombs_state;
 # if WCSRTOMBS_NULL_ARG_BUG
   if (dest == NULL)
     {
@@ -55,7 +55,7 @@
 wcsrtombs (char *dest, const wchar_t **srcp, size_t len, mbstate_t *ps)
 {
   if (ps == NULL)
-    ps = &internal_state;
+    ps = &_gl_wcsrtombs_state;
   {
     const wchar_t *src = *srcp;
     size_t cur_max = MB_CUR_MAX;
--- a/m4/wchar.m4
+++ b/m4/wchar.m4
@@ -7,7 +7,7 @@
 
 dnl Written by Eric Blake.
 
-# wchar.m4 serial 20
+# wchar.m4 serial 21
 
 AC_DEFUN([gl_WCHAR_H],
 [
@@ -70,6 +70,7 @@
   GNULIB_MBSNRTOWCS=0; AC_SUBST([GNULIB_MBSNRTOWCS])
   GNULIB_WCRTOMB=0;    AC_SUBST([GNULIB_WCRTOMB])
   GNULIB_WCSRTOMBS=0;  AC_SUBST([GNULIB_WCSRTOMBS])
+  GNULIB_WCSNRTOMBS=0; AC_SUBST([GNULIB_WCSNRTOMBS])
   GNULIB_WCWIDTH=0;    AC_SUBST([GNULIB_WCWIDTH])
   dnl Assume proper GNU behavior unless another module says otherwise.
   HAVE_BTOWC=1;        AC_SUBST([HAVE_BTOWC])
@@ -80,6 +81,7 @@
   HAVE_MBSNRTOWCS=1;   AC_SUBST([HAVE_MBSNRTOWCS])
   HAVE_WCRTOMB=1;      AC_SUBST([HAVE_WCRTOMB])
   HAVE_WCSRTOMBS=1;    AC_SUBST([HAVE_WCSRTOMBS])
+  HAVE_WCSNRTOMBS=1;   AC_SUBST([HAVE_WCSNRTOMBS])
   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])
new file mode 100644
--- /dev/null
+++ b/m4/wcsnrtombs.m4
@@ -0,0 +1,25 @@
+# wcsnrtombs.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_WCSNRTOMBS],
+[
+  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
+
+  AC_REQUIRE([AC_TYPE_MBSTATE_T])
+  AC_CHECK_FUNCS_ONCE([wcsnrtombs])
+  if test $ac_cv_func_wcsnrtombs = no; then
+    HAVE_WCSNRTOMBS=0
+    gl_REPLACE_WCHAR_H
+    AC_LIBOBJ([wcsnrtombs])
+    AC_LIBOBJ([wcsrtombs-state])
+    gl_PREREQ_WCSNRTOMBS
+  fi
+])
+
+# Prerequisites of lib/wcsnrtombs.c.
+AC_DEFUN([gl_PREREQ_WCSNRTOMBS], [
+  :
+])
--- a/m4/wcsrtombs.m4
+++ b/m4/wcsrtombs.m4
@@ -1,4 +1,4 @@
-# wcsrtombs.m4 serial 1
+# wcsrtombs.m4 serial 2
 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,
@@ -31,6 +31,7 @@
   if test $HAVE_WCSRTOMBS = 0 || test $REPLACE_WCSRTOMBS = 1; then
     gl_REPLACE_WCHAR_H
     AC_LIBOBJ([wcsrtombs])
+    AC_LIBOBJ([wcsrtombs-state])
     gl_PREREQ_WCSRTOMBS
   fi
 ])
--- a/modules/wchar
+++ b/modules/wchar
@@ -34,6 +34,7 @@
 	      -e 's|@''GNULIB_MBSNRTOWCS''@|$(GNULIB_MBSNRTOWCS)|g' \
 	      -e 's|@''GNULIB_WCRTOMB''@|$(GNULIB_WCRTOMB)|g' \
 	      -e 's|@''GNULIB_WCSRTOMBS''@|$(GNULIB_WCSRTOMBS)|g' \
+	      -e 's|@''GNULIB_WCSNRTOMBS''@|$(GNULIB_WCSNRTOMBS)|g' \
 	      -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \
 	      -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \
 	      -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \
@@ -44,6 +45,7 @@
 	      -e 's|@''HAVE_MBSNRTOWCS''@|$(HAVE_MBSNRTOWCS)|g' \
 	      -e 's|@''HAVE_WCRTOMB''@|$(HAVE_WCRTOMB)|g' \
 	      -e 's|@''HAVE_WCSRTOMBS''@|$(HAVE_WCSRTOMBS)|g' \
+	      -e 's|@''HAVE_WCSNRTOMBS''@|$(HAVE_WCSNRTOMBS)|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' \
new file mode 100644
--- /dev/null
+++ b/modules/wcsnrtombs
@@ -0,0 +1,28 @@
+Description:
+wcsnrtombs() function: convert wide string to string.
+
+Files:
+lib/wcsnrtombs.c
+lib/wcsrtombs-state.c
+m4/wcsnrtombs.m4
+m4/mbstate_t.m4
+
+Depends-on:
+wchar
+wcrtomb
+
+configure.ac:
+gl_FUNC_WCSNRTOMBS
+gl_WCHAR_MODULE_INDICATOR([wcsnrtombs])
+
+Makefile.am:
+
+Include:
+<wchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
--- a/modules/wcsrtombs
+++ b/modules/wcsrtombs
@@ -3,6 +3,7 @@
 
 Files:
 lib/wcsrtombs.c
+lib/wcsrtombs-state.c
 m4/wcsrtombs.m4
 m4/mbstate_t.m4
 m4/locale-fr.m4