Add wcsrtombs(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-03-24 18:07:59 +01:00
parent 9e242e9cd7
commit 8e0d659cec
3 changed files with 78 additions and 1 deletions

View File

@ -142,6 +142,7 @@ wcslen.o \
wcsncat.o \
wcsncpy.o \
wcsrchr.o \
wcsrtombs.o \
wcsspn.o \
wcstok.o \
wctomb.o \

View File

@ -74,6 +74,7 @@ size_t wcslen(const wchar_t*);
wchar_t* wcsncat(wchar_t* restrict, const wchar_t* restrict, size_t);
wchar_t* wcsncpy(wchar_t* restrict, const wchar_t* restrict, size_t);
wchar_t* wcsrchr(const wchar_t*, wchar_t);
size_t wcsrtombs(char* restrict, const wchar_t** restrict, size_t, mbstate_t* restrict);
size_t wcsspn(const wchar_t*, const wchar_t*);
wchar_t* wcstok(wchar_t* restrict, const wchar_t* restrict, wchar_t** restrict);
@ -108,7 +109,6 @@ long long wcstoll(const wchar_t* restrict, wchar_t** restrict, int);
long wcstol(const wchar_t* restrict, wchar_t** restrict, int);
size_t mbrlen(const char* restrict, size_t, mbstate_t* restrict);
size_t wcsftime(wchar_t* restrict, size_t, const wchar_t* restrict, const struct tm* restrict);
size_t wcsrtombs(char* restrict, const wchar_t** restrict, size_t, mbstate_t* restrict);
size_t wcsxfrm(wchar_t* restrict, const wchar_t* restrict, size_t);
unsigned long long wcstoull(const wchar_t* restrict, wchar_t** restrict, int);
unsigned long wcstoul(const wchar_t* restrict, wchar_t** restrict, int);

76
libc/wcsrtombs.cpp Normal file
View File

@ -0,0 +1,76 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
wcsrtombs.cpp
Convert a wide-character string to multibyte string.
*******************************************************************************/
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
extern "C" size_t wcsrtombs(char* dst, const wchar_t** src_ptr, size_t dst_len,
mbstate_t* ps)
{
assert(src_ptr && *src_ptr);
// Avoid changing *src_ptr if dst is NULL.
const wchar_t* local_src_ptr = *src_ptr;
if ( !dst )
src_ptr = &local_src_ptr;
// For some reason, the standards don't mandate that the secret ps variable
// is reset when ps is NULL, unlike mbstowcs that always resets this
// variable. We'll avoid resetting the variable here in case any programs
// actually take advantage of this fact.
static mbstate_t static_ps;
if ( !ps )
ps = &static_ps;
size_t ret = 0;
size_t src_len = wcslen(*src_ptr);
char buf[MB_CUR_MAX];
while ( !dst || dst_len )
{
mbstate_t saved_ps = *ps;
size_t produced = wcrtomb(buf, **src_ptr, ps);
if ( produced == (size_t) -1 )
return (size_t) -1;
if ( dst && dst_len < produced )
{
*ps = saved_ps;
break;
}
memcpy(dst, buf, produced);
if ( **src_ptr == L'\0' )
{
ret += produced - 1; // Don't count the '\0' byte.
*src_ptr = NULL;
break;
}
ret += produced;
(*src_ptr)++;
src_len--;
if ( dst )
dst += produced,
dst_len -= produced;
ret++;
}
return ret;
}