New module 'wcsnrtombs'.
authorBruno Haible <bruno@clisp.org>
Sun, 21 Dec 2008 23:42:11 +0000 (00:42 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Dec 2008 23:42:11 +0000 (00:42 +0100)
12 files changed:
ChangeLog
doc/posix-functions/wcsnrtombs.texi
lib/wchar.in.h
lib/wcsnrtombs.c [new file with mode: 0644]
lib/wcsrtombs-state.c [new file with mode: 0644]
lib/wcsrtombs.c
m4/wchar.m4
m4/wcsnrtombs.m4 [new file with mode: 0644]
m4/wcsrtombs.m4
modules/wchar
modules/wcsnrtombs [new file with mode: 0644]
modules/wcsrtombs

index 19d76c1d57af80e1413a7f3a75d6c2deebb4da71..9f9b447b9b415f8c16eb4804d8373fe32b81dc78 100644 (file)
--- 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.
index 402d55fd71cb2ad68dfefc5ea44a4751de50147b..42ba1d285444a454f729b5e9646bed5626ed0a2b 100644 (file)
@@ -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
+@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
-@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
index e11f8c04bfc5cecbcf474f82ca3bd97c4b7931d9..d63fc230bf4ef12c70864bdc0d9e910050537978 100644 (file)
@@ -241,6 +241,20 @@ extern size_t wcsrtombs (char *dest, const wchar_t **srcp, size_t len, mbstate_t
 #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@
diff --git a/lib/wcsnrtombs.c b/lib/wcsnrtombs.c
new file mode 100644 (file)
index 0000000..742b857
--- /dev/null
@@ -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
diff --git a/lib/wcsrtombs-state.c b/lib/wcsrtombs-state.c
new file mode 100644 (file)
index 0000000..c06ad94
--- /dev/null
@@ -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;
index 1b33926684277756a3aba2bd5275ec55189d2449..5abbac481c30cce5d247d8fdb379d5cff6e30f40 100644 (file)
@@ -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 @@ size_t
 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 @@ size_t
 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;
index 6932b659285169b917a504e91e79dda598ea4ee2..1a3efef014c37ebd5b3a1f0bb8b9ec9c1c596737 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 20
+# wchar.m4 serial 21
 
 AC_DEFUN([gl_WCHAR_H],
 [
@@ -70,6 +70,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
   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 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
   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])
diff --git a/m4/wcsnrtombs.m4 b/m4/wcsnrtombs.m4
new file mode 100644 (file)
index 0000000..9aef477
--- /dev/null
@@ -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], [
+  :
+])
index 1ff3a2f9ba3b932f842b83ed9e4a07d92317e650..829168ed807911985013c9fba808981848d628bd 100644 (file)
@@ -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 @@ AC_DEFUN([gl_FUNC_WCSRTOMBS],
   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
 ])
index d691e869f448e80c98ef6064fa50933ad25929e5..38d3503007df551fce8a1f755281f917c65ef516 100644 (file)
@@ -34,6 +34,7 @@ wchar.h: wchar.in.h
              -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 @@ wchar.h: wchar.in.h
              -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' \
diff --git a/modules/wcsnrtombs b/modules/wcsnrtombs
new file mode 100644 (file)
index 0000000..af1a9c2
--- /dev/null
@@ -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
+
index 5a9ee50aa5e458de6c1fca9d296393f516e9948c..295aa50eed8b4daa5d9be8bc2dc5530513c868ac 100644 (file)
@@ -3,6 +3,7 @@ wcsrtombs() function: convert wide string to string.
 
 Files:
 lib/wcsrtombs.c
+lib/wcsrtombs-state.c
 m4/wcsrtombs.m4
 m4/mbstate_t.m4
 m4/locale-fr.m4