]> pd.if.org Git - pdclib.old/commitdiff
PDCLIB-2 PDCLIB-9: Single character conversions for <uchar.h>
authorOwen Shepherd <owen.shepherd@e43.eu>
Mon, 31 Dec 2012 20:34:58 +0000 (20:34 +0000)
committerOwen Shepherd <owen.shepherd@e43.eu>
Mon, 31 Dec 2012 20:34:58 +0000 (20:34 +0000)
functions/uchar/c32rtomb.c [new file with mode: 0644]
functions/uchar/mbrtoc32.c [new file with mode: 0644]

diff --git a/functions/uchar/c32rtomb.c b/functions/uchar/c32rtomb.c
new file mode 100644 (file)
index 0000000..ab5006b
--- /dev/null
@@ -0,0 +1,59 @@
+/* c32rtomb(
+    char        *restrict   s, 
+    char32_t                c32,
+    mbstate_t   *restrict   ps);
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#ifndef REGTEST
+#include <uchar.h>
+#include <errno.h>
+#include <stdint.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <_PDCLIB_encoding.h>
+#include <_PDCLIB_locale.h>
+
+size_t c32rtomb_l(
+    char        *restrict   s, 
+    char32_t                c32,
+    mbstate_t   *restrict   ps,
+    locale_t     restrict   l
+)
+{
+    char32_t *restrict psrc = &c32;
+    size_t srcsz  = 1;
+    size_t dstsz  = MB_CUR_MAX;
+    size_t dstrem = dstsz;
+
+    if(l->_Codec->__c32stombs(&s, &dstrem, &psrc, &srcsz, ps)) {
+        // Successful conversion
+        return dstsz - dstrem;
+    } else {
+        errno = EILSEQ;
+        return (size_t) -1;
+    }
+}
+
+size_t c32rtomb(
+    char        *restrict   s, 
+    char32_t                c32,
+    mbstate_t   *restrict   ps
+)
+{
+    return c32rtomb_l(s, c32, ps, _PDCLIB_threadlocale());
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    TESTCASE( NO_TESTDRIVER );
+    return TEST_RESULTS;
+}
+#endif
diff --git a/functions/uchar/mbrtoc32.c b/functions/uchar/mbrtoc32.c
new file mode 100644 (file)
index 0000000..05d9184
--- /dev/null
@@ -0,0 +1,75 @@
+/* size_t mbrtoc32(
+    char16_t    *restrict   pc16,
+    const char  *restrict   s, 
+    size_t                  n,
+    mbstate_t   *restrict   ps);
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#ifndef REGTEST
+#include <uchar.h>
+#include <errno.h>
+#include <stdint.h>
+#include <assert.h>
+#include <_PDCLIB_encoding.h>
+#include <_PDCLIB_locale.h>
+
+size_t mbrtoc32_l(
+    char32_t    *restrict   pc32,
+    const char  *restrict   s, 
+    size_t                  n,
+    mbstate_t   *restrict   ps,
+    locale_t     restrict   l
+)
+{
+    size_t dstlen = 1;
+    size_t nr = n;
+
+    if(l->_Codec->__mbstoc32s(&pc32, &dstlen, &s, &nr, ps)) {
+        // Successful conversion
+        if(dstlen == 0) {
+            // A character was output
+            if(nr == n) {
+                // The output character resulted entirely from stored state
+                return (size_t) -3;
+            } else if(pc32[-1] == 0) {
+                // Was null character
+                return 0;
+            } else {
+                // Count of processed characters
+                return n - nr;
+            }
+        } else {
+            assert(nr == 0 && "Must have processed whole input");
+            return (size_t) -2;
+        }
+    } else {
+        // Failed conversion
+        errno = EILSEQ;
+        return (size_t) -1;
+    }
+}
+
+size_t mbrtoc32(
+    char32_t    *restrict   pc32,
+    const char  *restrict   s, 
+    size_t                  n,
+    mbstate_t   *restrict   ps
+)
+{
+    return mbrtoc32_l(pc32, s, n, ps, _PDCLIB_threadlocale());
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    TESTCASE( NO_TESTDRIVER );
+    return TEST_RESULTS;
+}
+#endif