From af0bf7d39ae3e433be81a69ea3c5d418814de4a0 Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Tue, 23 Apr 2013 18:20:05 +0100 Subject: [PATCH] PDCLIB-2 PDCLIB-9 mbsinit --- functions/wchar/mbsinit.c | 53 ++++++++++++++++++++++++++++++ internals/_PDCLIB_encoding.h | 3 ++ man3/mbsinit.3 | 58 +++++++++++++++++++++++++++++++++ opt/basecodecs/_PDCLIB_ascii.c | 4 +++ opt/basecodecs/_PDCLIB_latin1.c | 4 +++ opt/basecodecs/_PDCLIB_utf8.c | 5 +++ 6 files changed, 127 insertions(+) create mode 100644 functions/wchar/mbsinit.c create mode 100644 man3/mbsinit.3 diff --git a/functions/wchar/mbsinit.c b/functions/wchar/mbsinit.c new file mode 100644 index 0000000..145edd8 --- /dev/null +++ b/functions/wchar/mbsinit.c @@ -0,0 +1,53 @@ +/* mbsinit(mbstate_t *ps); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#ifndef REGTEST +#include <_PDCLIB_encoding.h> +#include <_PDCLIB_locale.h> + +int _PDCLIB_mbsinit_l( const mbstate_t *ps, locale_t l ) +{ + if( ps ) { + return ps->_Surrogate == 0 + && ps->_PendState == 0 + && l->_Codec->__mbsinit(ps); + } else return 1; +} + +int mbsinit( const mbstate_t * ps ) +{ + return _PDCLIB_mbsinit_l(ps, _PDCLIB_threadlocale()); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + mbstate_t mbs; + memset(&mbs, 0, sizeof mbs); + + TESTCASE(mbsinit(NULL) != 0); + TESTCASE(mbsinit(&mbs) != 0); + +#ifndef REGTEST + // Surrogate pending + mbs._Surrogate = 0xFEED; + TESTCASE(mbsinit(&mbs) == 0); + + mbs._Surrogate = 0; + mbs._PendState = 1; + TESTCASE(mbsinit(&mbs) == 0); +#endif + return TEST_RESULTS; +} +#endif + + + diff --git a/internals/_PDCLIB_encoding.h b/internals/_PDCLIB_encoding.h index cec3089..fdaee7e 100644 --- a/internals/_PDCLIB_encoding.h +++ b/internals/_PDCLIB_encoding.h @@ -99,6 +99,9 @@ struct _PDCLIB_charcodec { * encountered), else return false. */ + /* mbsinit. Mandatory. */ + _PDCLIB_bool (*__mbsinit)(const _PDCLIB_mbstate_t *_P_ps); + /* UCS-4 variants. Mandatory. */ _PDCLIB_bool (*__mbstoc32s)( diff --git a/man3/mbsinit.3 b/man3/mbsinit.3 new file mode 100644 index 0000000..7e84ae1 --- /dev/null +++ b/man3/mbsinit.3 @@ -0,0 +1,58 @@ +.\" This file is part of the Public Domain C Library (PDCLib). +.\" Permission is granted to use, modify, and / or redistribute at will. +.\" +.Dd +.Dt MBSINIT 3 +.Os +.\" +.Sh NAME +.Nm mbsinit +.Nd determines multibyte conversion state +.\" +.Sh SYNOPSIS +.In wchar.h +.Fn "int mbsinit" "const mbstate_t *ps" +.In xlocale.h +.Fn "int mbsinit_l" "const mbstate_t *ps" "locale_t loc" +.\" +.Sh DESCRIPTION +.Fn mbsinit +and +.Fn mbsinit_l +shall return a nonzero value if the multibyte converison state pointed to by +.Va ps +corresponds to an initial conversion state. The interpretation of +.Va *ps +is locale dependent; the only guarantee is that an +.Vt mbstate_t +object initialized to zero shall correspond to an initial conversion state. If +.Va ps +is +.Dv NULL , +then a nonzero value shall be returned. +.\" +.Pp +The locale used for +.Fn mbsinit +shall be the currently active locale; either the current thread locale as set by +.Xr uselocale 3 +if one has been specified, or otherwise the global locale controlled by +.Xr setlocale 3 . +The locale used by +.Fn mbsinit_l +is that specified by +.Va loc . +.\" +.Sh SEE ALSO +.Xr mbrtowc 3 +.Xr wcrtomb 3 +.Xr setlocale 3 +.Xr uselocale 3 +.\" +.Sh STANDARDS +.Fn mbsinit +is first defined in +.St -isoC-amd1 ; +.Fn mbsinit_l +is a nonstandard extension originating in Darwin. See +.Xr xlocale.h 3 \ No newline at end of file diff --git a/opt/basecodecs/_PDCLIB_ascii.c b/opt/basecodecs/_PDCLIB_ascii.c index e07e2ba..efbc581 100644 --- a/opt/basecodecs/_PDCLIB_ascii.c +++ b/opt/basecodecs/_PDCLIB_ascii.c @@ -9,6 +9,9 @@ #include #include <_PDCLIB_encoding.h> +static bool ascii_mbsinit( const mbstate_t *ps ) +{ return 1; } + static bool asciitoc32( char32_t *restrict *restrict p_outbuf, size_t *restrict p_outsz, @@ -60,6 +63,7 @@ static bool c32toascii( } struct _PDCLIB_charcodec _PDCLIB_ascii_codec = { + .__mbsinit = ascii_mbsinit, .__mbstoc32s = asciitoc32, .__c32stombs = c32toascii, .__mb_max = 1, diff --git a/opt/basecodecs/_PDCLIB_latin1.c b/opt/basecodecs/_PDCLIB_latin1.c index 72a3446..00d2f26 100644 --- a/opt/basecodecs/_PDCLIB_latin1.c +++ b/opt/basecodecs/_PDCLIB_latin1.c @@ -9,6 +9,9 @@ #include #include <_PDCLIB_encoding.h> +static bool latin1_mbsinit( const mbstate_t *ps ) +{ return 1; } + static bool latin1toc32( char32_t *restrict *restrict p_outbuf, size_t *restrict p_outsz, @@ -58,6 +61,7 @@ static bool c32tolatin1( } struct _PDCLIB_charcodec _PDCLIB_latin1_codec = { + .__mbsinit = latin1_mbsinit, .__mbstoc32s = latin1toc32, .__c32stombs = c32tolatin1, .__mb_max = 1, diff --git a/opt/basecodecs/_PDCLIB_utf8.c b/opt/basecodecs/_PDCLIB_utf8.c index bac0e82..a934541 100644 --- a/opt/basecodecs/_PDCLIB_utf8.c +++ b/opt/basecodecs/_PDCLIB_utf8.c @@ -17,6 +17,9 @@ * _St32[1] is the character accumulated so far */ +static bool utf8_mbsinit( const mbstate_t *p_s ) +{ return p_s->_StUC[0] == 0; } + enum { DecStart = 0, @@ -52,6 +55,7 @@ end_conversion: \ _PDCLIB_UNDEFINED(accum); \ state = DecStart; \ } while(0) + #define CHECK_CONTINUATION \ do { if((c & 0xC0) != 0x80) return false; } while(0) @@ -229,6 +233,7 @@ static bool c32toutf8( } struct _PDCLIB_charcodec _PDCLIB_utf8_codec = { + .__mbsinit = utf8_mbsinit, .__mbstoc32s = utf8toc32, .__c32stombs = c32toutf8, .__mb_max = 4, -- 2.40.0