]> pd.if.org Git - pdclib/commitdiff
PDCLIB-2 PDCLIB-9 mbsinit
authorOwen Shepherd <owen.shepherd@e43.eu>
Tue, 23 Apr 2013 17:20:05 +0000 (18:20 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Tue, 23 Apr 2013 17:20:05 +0000 (18:20 +0100)
functions/wchar/mbsinit.c [new file with mode: 0644]
internals/_PDCLIB_encoding.h
man3/mbsinit.3 [new file with mode: 0644]
opt/basecodecs/_PDCLIB_ascii.c
opt/basecodecs/_PDCLIB_latin1.c
opt/basecodecs/_PDCLIB_utf8.c

diff --git a/functions/wchar/mbsinit.c b/functions/wchar/mbsinit.c
new file mode 100644 (file)
index 0000000..145edd8
--- /dev/null
@@ -0,0 +1,53 @@
+/* mbsinit(mbstate_t *ps);\r
+\r
+   This file is part of the Public Domain C Library (PDCLib).\r
+   Permission is granted to use, modify, and / or redistribute at will.\r
+*/\r
+\r
+#include <wchar.h>\r
+#ifndef REGTEST\r
+#include <_PDCLIB_encoding.h>\r
+#include <_PDCLIB_locale.h>\r
+\r
+int _PDCLIB_mbsinit_l( const mbstate_t *ps, locale_t l )\r
+{\r
+    if( ps ) {\r
+        return ps->_Surrogate == 0\r
+            && ps->_PendState == 0\r
+            && l->_Codec->__mbsinit(ps);\r
+    } else return 1;\r
+}\r
+\r
+int mbsinit( const mbstate_t * ps )\r
+{\r
+    return _PDCLIB_mbsinit_l(ps, _PDCLIB_threadlocale());\r
+}\r
+\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+int main( void )\r
+{\r
+    mbstate_t mbs;\r
+    memset(&mbs, 0, sizeof mbs);\r
+\r
+    TESTCASE(mbsinit(NULL) != 0);\r
+    TESTCASE(mbsinit(&mbs) != 0);\r
+\r
+#ifndef REGTEST\r
+    // Surrogate pending\r
+    mbs._Surrogate = 0xFEED;\r
+    TESTCASE(mbsinit(&mbs) == 0);\r
+\r
+    mbs._Surrogate = 0;\r
+    mbs._PendState = 1;\r
+    TESTCASE(mbsinit(&mbs) == 0);\r
+#endif\r
+    return TEST_RESULTS;\r
+}\r
+#endif\r
+\r
+\r
+\r
index cec3089aa8e3ad01460485990255c5b950b33b3d..fdaee7ee42061044cdb792212b3ef5b4a24287c1 100644 (file)
@@ -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 (file)
index 0000000..7e84ae1
--- /dev/null
@@ -0,0 +1,58 @@
+.\" This file is part of the Public Domain C Library (PDCLib).\r
+.\" Permission is granted to use, modify, and / or redistribute at will.\r
+.\"\r
+.Dd\r
+.Dt MBSINIT 3\r
+.Os\r
+.\"\r
+.Sh NAME\r
+.Nm mbsinit\r
+.Nd determines multibyte conversion state\r
+.\"\r
+.Sh SYNOPSIS\r
+.In wchar.h\r
+.Fn "int mbsinit" "const mbstate_t *ps"\r
+.In xlocale.h\r
+.Fn "int mbsinit_l" "const mbstate_t *ps" "locale_t loc"\r
+.\"\r
+.Sh DESCRIPTION\r
+.Fn mbsinit \r
+and\r
+.Fn mbsinit_l \r
+shall return a nonzero value if the multibyte converison state pointed to by\r
+.Va ps\r
+corresponds to an initial conversion state. The interpretation of \r
+.Va *ps\r
+is locale dependent; the only guarantee is that an\r
+.Vt mbstate_t\r
+object initialized to zero shall correspond to an initial conversion state. If\r
+.Va ps\r
+is\r
+.Dv NULL ,\r
+then a nonzero value shall be returned.\r
+.\"\r
+.Pp\r
+The locale used for \r
+.Fn mbsinit\r
+shall be the currently active locale; either the current thread locale as set by\r
+.Xr uselocale 3\r
+if one has been specified, or otherwise the global locale controlled by\r
+.Xr setlocale 3 .\r
+The locale used by \r
+.Fn mbsinit_l\r
+is that specified by\r
+.Va loc .\r
+.\"\r
+.Sh SEE ALSO\r
+.Xr mbrtowc 3\r
+.Xr wcrtomb 3\r
+.Xr setlocale 3\r
+.Xr uselocale 3\r
+.\"\r
+.Sh STANDARDS\r
+.Fn mbsinit\r
+is first defined in\r
+.St -isoC-amd1 ;\r
+.Fn mbsinit_l\r
+is a nonstandard extension originating in Darwin. See\r
+.Xr xlocale.h 3
\ No newline at end of file
index e07e2ba217a341c5199d798f3979d6394ea11d49..efbc581f09b509cfe0e9e6943a0db848c204e2d1 100644 (file)
@@ -9,6 +9,9 @@
 #include <uchar.h>
 #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,
index 72a344680436c3aaf225084b4af52c8752545121..00d2f2600d1b3bbb33dfedaeae5bf848eb38128c 100644 (file)
@@ -9,6 +9,9 @@
 #include <uchar.h>
 #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,
index bac0e824a7f9b5a1d0b662058778f32f34eceb79..a93454194fb04ea3a845168351a0ee86df2a46e8 100644 (file)
@@ -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,