# HG changeset patch # User Bruno Haible # Date 1229902931 -3600 # Node ID 47cd92ce639abe07c18cb5b3f403d38b2ec14a2c # Parent d3a1e8911dfa75a5f2dfcd64548006c81f5c7783 New module 'wcsnrtombs'. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2008-12-21 Bruno Haible + + 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 * modules/wcsrtombs-tests: New file. diff --git a/doc/posix-functions/wcsnrtombs.texi b/doc/posix-functions/wcsnrtombs.texi --- 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 diff --git a/lib/wchar.in.h b/lib/wchar.in.h --- 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@ diff --git a/lib/wcsnrtombs.c b/lib/wcsnrtombs.c 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 , 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 . */ + +#include + +/* Specification. */ +#include + +# include +# include +# include + + +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 --- /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 , 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 . */ + +#include + +#include + +/* Internal state used by the functions wcsrtombs() and wcsnrtombs(). */ +mbstate_t _gl_wcsrtombs_state; diff --git a/lib/wcsrtombs.c b/lib/wcsrtombs.c --- a/lib/wcsrtombs.c +++ b/lib/wcsrtombs.c @@ -20,7 +20,7 @@ /* Specification. */ #include -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; diff --git a/m4/wchar.m4 b/m4/wchar.m4 --- 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]) diff --git a/m4/wcsnrtombs.m4 b/m4/wcsnrtombs.m4 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], [ + : +]) diff --git a/m4/wcsrtombs.m4 b/m4/wcsrtombs.m4 --- 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 ]) diff --git a/modules/wchar b/modules/wchar --- 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' \ diff --git a/modules/wcsnrtombs b/modules/wcsnrtombs 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: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/wcsrtombs b/modules/wcsrtombs --- 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