]> pd.if.org Git - pdclib.old/blob - functions/uchar/c32rtomb.c
PDCLIB-2 PDCLIB-9: Single character conversions for <uchar.h>
[pdclib.old] / functions / uchar / c32rtomb.c
1 /* c32rtomb(
2     char        *restrict   s, 
3     char32_t                c32,
4     mbstate_t   *restrict   ps);
5
6    This file is part of the Public Domain C Library (PDCLib).
7    Permission is granted to use, modify, and / or redistribute at will.
8 */
9
10 #ifndef REGTEST
11 #include <uchar.h>
12 #include <errno.h>
13 #include <stdint.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <_PDCLIB_encoding.h>
17 #include <_PDCLIB_locale.h>
18
19 size_t c32rtomb_l(
20     char        *restrict   s, 
21     char32_t                c32,
22     mbstate_t   *restrict   ps,
23     locale_t     restrict   l
24 )
25 {
26     char32_t *restrict psrc = &c32;
27     size_t srcsz  = 1;
28     size_t dstsz  = MB_CUR_MAX;
29     size_t dstrem = dstsz;
30
31     if(l->_Codec->__c32stombs(&s, &dstrem, &psrc, &srcsz, ps)) {
32         // Successful conversion
33         return dstsz - dstrem;
34     } else {
35         errno = EILSEQ;
36         return (size_t) -1;
37     }
38 }
39
40 size_t c32rtomb(
41     char        *restrict   s, 
42     char32_t                c32,
43     mbstate_t   *restrict   ps
44 )
45 {
46     return c32rtomb_l(s, c32, ps, _PDCLIB_threadlocale());
47 }
48
49 #endif
50
51 #ifdef TEST
52 #include <_PDCLIB_test.h>
53
54 int main( void )
55 {
56     TESTCASE( NO_TESTDRIVER );
57     return TEST_RESULTS;
58 }
59 #endif