Cygwin: wcitombs: like wcstombs, just for wint_t

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2023-03-09 11:07:36 +01:00
parent 4449f3a11d
commit c47a998d0a
2 changed files with 47 additions and 0 deletions

View File

@ -44,6 +44,10 @@ extern wctomb_f __utf8_wctomb;
for surrogate pairs, plus a wchar_t NUL. */
void wcintowcs (wchar_t *, wint_t *, size_t);
/* replacement function for wcstombs, converting a UTF-32 string to
a multibyte string. */
size_t wcitombs (char *, const wint_t *, size_t);
/* replacement function for wcrtomb, converting a UTF-32 char to a
multibyte string. */
size_t wirtomb (char *, wint_t, mbstate_t *);

View File

@ -129,6 +129,49 @@ wcintowcs (wchar_t *dest, wint_t *src, size_t len)
*dest = '\0';
}
/* replacement function for wcstombs, converting a UTF-32 string to
a multibyte string. */
extern "C" size_t
wcitombs (char *dest, const wint_t *src, size_t n)
{
char *ptr = dest;
size_t max = n;
char buf[8];
size_t i, bytes, num_to_copy;
mbstate_t state;
if (dest == NULL)
{
size_t num_bytes = 0;
while (*src != '\0')
{
bytes = wirtomb (buf, *src++, &state);
if (bytes == (size_t) -1)
return (size_t) -1;
num_bytes += bytes;
}
return num_bytes;
}
else
{
while (n > 0)
{
bytes = wirtomb (buf, *src++, &state);
if (bytes == (size_t) -1)
return (size_t) -1;
num_to_copy = (n > bytes ? bytes : (int)n);
for (i = 0; i < num_to_copy; ++i)
*ptr++ = buf[i];
if (*src == '\0')
return ptr - dest - (n >= bytes);
++src;
n -= num_to_copy;
}
return max;
}
}
/* replacement function for wcrtomb, converting a UTF-32 char to a
multibyte string. */
extern "C" size_t