]> pd.if.org Git - pdclib.old/blob - functions/uchar/mbrtoc32.c
PDCLIB-2 PDCLIB-9 Correct header entry of mbrtoc32
[pdclib.old] / functions / uchar / mbrtoc32.c
1 /* size_t mbrtoc32(
2     char32_t    *restrict   pc32,
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                 // With UTF-32, this shouldn't be possible?
37                 return (size_t) -3;
38             } else if(pc32[-1] == 0) {
39                 // Was null character
40                 return 0;
41             } else {
42                 // Count of processed characters
43                 return n - nr;
44             }
45         } else {
46             assert(nr == 0 && "Must have processed whole input");
47             return (size_t) -2;
48         }
49     } else {
50         // Failed conversion
51         errno = EILSEQ;
52         return (size_t) -1;
53     }
54 }
55
56 size_t mbrtoc32(
57     char32_t    *restrict   pc32,
58     const char  *restrict   s, 
59     size_t                  n,
60     mbstate_t   *restrict   ps
61 )
62 {
63     return mbrtoc32_l(pc32, s, n, ps, _PDCLIB_threadlocale());
64 }
65
66 #endif
67
68 #ifdef TEST
69 #include <_PDCLIB_test.h>
70
71 int main( void )
72 {
73     TESTCASE( NO_TESTDRIVER );
74     return TEST_RESULTS;
75 }
76 #endif