X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fuchar%2Fc32rtomb.c;fp=functions%2Fuchar%2Fc32rtomb.c;h=ab5006b5633207f989354373248e50d81583ca0f;hb=a16d38f68798a7859c38f944373a8c122f7e8cd4;hp=0000000000000000000000000000000000000000;hpb=fb44227179cd8d0dbaaf694fba2d4861106add75;p=pdclib.old diff --git a/functions/uchar/c32rtomb.c b/functions/uchar/c32rtomb.c new file mode 100644 index 0000000..ab5006b --- /dev/null +++ b/functions/uchar/c32rtomb.c @@ -0,0 +1,59 @@ +/* c32rtomb( + char *restrict s, + char32_t c32, + mbstate_t *restrict ps); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#ifndef REGTEST +#include +#include +#include +#include +#include +#include <_PDCLIB_encoding.h> +#include <_PDCLIB_locale.h> + +size_t c32rtomb_l( + char *restrict s, + char32_t c32, + mbstate_t *restrict ps, + locale_t restrict l +) +{ + char32_t *restrict psrc = &c32; + size_t srcsz = 1; + size_t dstsz = MB_CUR_MAX; + size_t dstrem = dstsz; + + if(l->_Codec->__c32stombs(&s, &dstrem, &psrc, &srcsz, ps)) { + // Successful conversion + return dstsz - dstrem; + } else { + errno = EILSEQ; + return (size_t) -1; + } +} + +size_t c32rtomb( + char *restrict s, + char32_t c32, + mbstate_t *restrict ps +) +{ + return c32rtomb_l(s, c32, ps, _PDCLIB_threadlocale()); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} +#endif