]> pd.if.org Git - pdclib.old/blob - functions/uchar/c32rtomb.c
PDCLIB-2 c32rtomb: likewise missing NULL destination check
[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     char buf[s ? 0 : MB_CUR_MAX];
27     s =      s ? s : buf;
28
29     const char32_t *restrict psrc = &c32;
30     size_t srcsz  = 1;
31     size_t dstsz  = MB_CUR_MAX;
32     size_t dstrem = dstsz;
33
34     if(l->_Codec->__c32stombs(&s, &dstrem, &psrc, &srcsz, ps)) {
35         // Successful conversion
36         return dstsz - dstrem;
37     } else {
38         errno = EILSEQ;
39         return (size_t) -1;
40     }
41 }
42
43 size_t c32rtomb(
44     char        *restrict   s, 
45     char32_t                c32,
46     mbstate_t   *restrict   ps
47 )
48 {
49     return c32rtomb_l(s, c32, ps, _PDCLIB_threadlocale());
50 }
51
52 #endif
53
54 #ifdef TEST
55 #include <_PDCLIB_test.h>
56
57 int main( void )
58 {
59     TESTCASE( NO_TESTDRIVER );
60     return TEST_RESULTS;
61 }
62 #endif