]> pd.if.org Git - pdclib.old/blob - functions/uchar/mbrtoc32.c
PDCLIB-2 PDCLIB-9: Single character conversions for <uchar.h>
[pdclib.old] / functions / uchar / mbrtoc32.c
1 /* size_t mbrtoc32(
2     char16_t    *restrict   pc16,
3     const char  *restrict   s, 
4     size_t                  n,
5     mbstate_t   *restrict   ps);
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #ifndef REGTEST
12 #include <uchar.h>
13 #include <errno.h>
14 #include <stdint.h>
15 #include <assert.h>
16 #include <_PDCLIB_encoding.h>
17 #include <_PDCLIB_locale.h>
18
19 size_t mbrtoc32_l(
20     char32_t    *restrict   pc32,
21     const char  *restrict   s, 
22     size_t                  n,
23     mbstate_t   *restrict   ps,
24     locale_t     restrict   l
25 )
26 {
27     size_t dstlen = 1;
28     size_t nr = n;
29
30     if(l->_Codec->__mbstoc32s(&pc32, &dstlen, &s, &nr, ps)) {
31         // Successful conversion
32         if(dstlen == 0) {
33             // A character was output
34             if(nr == n) {
35                 // The output character resulted entirely from stored state
36                 return (size_t) -3;
37             } else if(pc32[-1] == 0) {
38                 // Was null character
39                 return 0;
40             } else {
41                 // Count of processed characters
42                 return n - nr;
43             }
44         } else {
45             assert(nr == 0 && "Must have processed whole input");
46             return (size_t) -2;
47         }
48     } else {
49         // Failed conversion
50         errno = EILSEQ;
51         return (size_t) -1;
52     }
53 }
54
55 size_t mbrtoc32(
56     char32_t    *restrict   pc32,
57     const char  *restrict   s, 
58     size_t                  n,
59     mbstate_t   *restrict   ps
60 )
61 {
62     return mbrtoc32_l(pc32, s, n, ps, _PDCLIB_threadlocale());
63 }
64
65 #endif
66
67 #ifdef TEST
68 #include <_PDCLIB_test.h>
69
70 int main( void )
71 {
72     TESTCASE( NO_TESTDRIVER );
73     return TEST_RESULTS;
74 }
75 #endif