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