From 172b44d208f0bf8f949168d2e42b76202ee4180f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 19 Dec 2008 03:12:24 +0100 Subject: [PATCH] New module 'wctob'. --- ChangeLog | 12 ++++++++++++ doc/posix-functions/wctob.texi | 8 ++++---- lib/wchar.in.h | 14 ++++++++++++++ lib/wctob.c | 37 +++++++++++++++++++++++++++++++++++++ m4/wchar.m4 | 4 +++- m4/wctob.m4 | 23 +++++++++++++++++++++++ modules/wchar | 6 ++++-- modules/wctob | 25 +++++++++++++++++++++++++ 8 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 lib/wctob.c create mode 100644 m4/wctob.m4 create mode 100644 modules/wctob diff --git a/ChangeLog b/ChangeLog index e9eebfe63..d6f991a3a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2008-12-18 Bruno Haible + New module 'wctob'. + * lib/wchar.in.h (wctob): New declaration. + * lib/wctob.c: New file. + * m4/wctob.m4: New file. + * modules/wctob: New file. + * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCTOB and + HAVE_WCTOB. + * modules/wchar (Makefile.am): Substitute GNULIB_WCTOB and HAVE_WCTOB. + * doc/posix-functions/wctob.texi: Document the new module. + +2008-12-18 Bruno Haible + * m4/mbsinit.m4 (gl_FUNC_MBSINIT): Invoke gl_REPLACE_WCHAR_H. * m4/btowc.m4 (gl_FUNC_BTOWC): Likewise. diff --git a/doc/posix-functions/wctob.texi b/doc/posix-functions/wctob.texi index 9cb52f34f..c4970767c 100644 --- a/doc/posix-functions/wctob.texi +++ b/doc/posix-functions/wctob.texi @@ -4,18 +4,18 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wctob.html} -Gnulib module: --- +Gnulib module: wctob Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +HP-UX 11, IRIX 5.3, 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, IRIX 5.3, Solaris 2.6, mingw, Interix 3.5. -@item On 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 445530d4b..bf73b5ceb 100644 --- a/lib/wchar.in.h +++ b/lib/wchar.in.h @@ -85,6 +85,20 @@ extern wint_t btowc (int c); #endif +/* Convert a wide character to a single-byte character. */ +#if @GNULIB_WCTOB@ +# if !@HAVE_WCTOB@ +extern int wctob (wint_t wc); +# endif +#elif defined GNULIB_POSIXCHECK +# undef wctob +# define wctob(w) \ + (GL_LINK_WARNING ("wctob is unportable - " \ + "use gnulib module wctob for portability"), \ + wctob (w)) +#endif + + /* Test whether *PS is in the initial state. */ #if @GNULIB_MBSINIT@ # if !@HAVE_MBSINIT@ diff --git a/lib/wctob.c b/lib/wctob.c new file mode 100644 index 000000000..4fa71b0d8 --- /dev/null +++ b/lib/wctob.c @@ -0,0 +1,37 @@ +/* Convert wide character to unibyte character. + 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 + +int +wctob (wint_t wc) +{ + char buf[64]; + + if (!(MB_CUR_MAX <= sizeof (buf))) + abort (); + if (wctomb (buf, wc) == 1) + return (unsigned char) buf[0]; + else + return EOF; +} diff --git a/m4/wchar.m4 b/m4/wchar.m4 index 0f562822d..a6671581a 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 8 +# wchar.m4 serial 9 AC_DEFUN([gl_WCHAR_H], [ @@ -62,10 +62,12 @@ AC_DEFUN([gl_WCHAR_MODULE_INDICATOR], AC_DEFUN([gl_WCHAR_H_DEFAULTS], [ GNULIB_BTOWC=0; AC_SUBST([GNULIB_BTOWC]) + GNULIB_WCTOB=0; AC_SUBST([GNULIB_WCTOB]) GNULIB_MBSINIT=0; AC_SUBST([GNULIB_MBSINIT]) GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC]) + HAVE_WCTOB=1; AC_SUBST([HAVE_WCTOB]) HAVE_MBSINIT=1; AC_SUBST([HAVE_MBSINIT]) HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH]) REPLACE_WCWIDTH=0; AC_SUBST([REPLACE_WCWIDTH]) diff --git a/m4/wctob.m4 b/m4/wctob.m4 new file mode 100644 index 000000000..f511b51ce --- /dev/null +++ b/m4/wctob.m4 @@ -0,0 +1,23 @@ +# wctob.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_WCTOB], +[ + AC_REQUIRE([gl_WCHAR_H_DEFAULTS]) + + AC_CHECK_FUNCS_ONCE([wctob]) + if test $ac_cv_func_wctob = no; then + HAVE_WCTOB=0 + gl_REPLACE_WCHAR_H + AC_LIBOBJ([wctob]) + gl_PREREQ_WCTOB + fi +]) + +# Prerequisites of lib/wctob.c. +AC_DEFUN([gl_PREREQ_WCTOB], [ + : +]) diff --git a/modules/wchar b/modules/wchar index 3f60ceaee..15f76ea81 100644 --- a/modules/wchar +++ b/modules/wchar @@ -24,12 +24,14 @@ wchar.h: wchar.in.h sed -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \ -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \ -e 's|@''NEXT_WCHAR_H''@|$(NEXT_WCHAR_H)|g' \ - -e 's/@''HAVE_WCHAR_H''@/$(HAVE_WCHAR_H)/g' \ + -e 's|@''HAVE_WCHAR_H''@|$(HAVE_WCHAR_H)|g' \ -e 's|@''GNULIB_BTOWC''@|$(GNULIB_BTOWC)|g' \ + -e 's|@''GNULIB_WCTOB''@|$(GNULIB_WCTOB)|g' \ -e 's|@''GNULIB_MBSINIT''@|$(GNULIB_MBSINIT)|g' \ -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \ - -e 's/@''HAVE_WINT_T''@/$(HAVE_WINT_T)/g' \ + -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \ -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \ + -e 's|@''HAVE_WCTOB''@|$(HAVE_WCTOB)|g' \ -e 's|@''HAVE_MBSINIT''@|$(HAVE_MBSINIT)|g' \ -e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \ -e 's|@''REPLACE_WCWIDTH''@|$(REPLACE_WCWIDTH)|g' \ diff --git a/modules/wctob b/modules/wctob new file mode 100644 index 000000000..d32cbcbea --- /dev/null +++ b/modules/wctob @@ -0,0 +1,25 @@ +Description: +wctob() function: convert wide character to unibyte character. + +Files: +lib/wctob.c +m4/wctob.m4 + +Depends-on: +wchar + +configure.ac: +gl_FUNC_WCTOB +gl_WCHAR_MODULE_INDICATOR([wctob]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + -- 2.11.0