mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Add mbsrtowcs(3).
This commit is contained in:
parent
eb9e027697
commit
9c336562a7
3 changed files with 74 additions and 1 deletions
|
@ -79,6 +79,7 @@ lldiv.o \
|
||||||
localtime.o \
|
localtime.o \
|
||||||
localtime_r.o \
|
localtime_r.o \
|
||||||
mbrtowc.o \
|
mbrtowc.o \
|
||||||
|
mbsrtowcs.o \
|
||||||
mbtowc.o \
|
mbtowc.o \
|
||||||
memccpy.o \
|
memccpy.o \
|
||||||
memchr.o \
|
memchr.o \
|
||||||
|
|
|
@ -61,6 +61,7 @@ __BEGIN_DECLS
|
||||||
|
|
||||||
struct tm;
|
struct tm;
|
||||||
|
|
||||||
|
size_t mbsrtowcs(wchar_t* restrict, const char** restrict, size_t, mbstate_t* restrict);
|
||||||
size_t wcrtomb(char* restrict, wchar_t, mbstate_t* restrict);
|
size_t wcrtomb(char* restrict, wchar_t, mbstate_t* restrict);
|
||||||
size_t mbrtowc(wchar_t* restrict, const char* restrict, size_t, mbstate_t* restrict);
|
size_t mbrtowc(wchar_t* restrict, const char* restrict, size_t, mbstate_t* restrict);
|
||||||
wchar_t* wcscat(wchar_t* restrict, const wchar_t* restrict);
|
wchar_t* wcscat(wchar_t* restrict, const wchar_t* restrict);
|
||||||
|
@ -106,7 +107,6 @@ long double wcstold(const wchar_t* restrict, wchar_t** restrict);
|
||||||
long long wcstoll(const wchar_t* restrict, wchar_t** restrict, int);
|
long long wcstoll(const wchar_t* restrict, wchar_t** restrict, int);
|
||||||
long wcstol(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 mbrlen(const char* restrict, size_t, mbstate_t* restrict);
|
||||||
size_t mbsrtowcs(wchar_t* restrict, 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 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 wcsrtombs(char* restrict, const wchar_t** restrict, size_t, mbstate_t* restrict);
|
||||||
size_t wcsxfrm(wchar_t* restrict, const wchar_t* restrict, size_t);
|
size_t wcsxfrm(wchar_t* restrict, const wchar_t* restrict, size_t);
|
||||||
|
|
72
libc/mbsrtowcs.cpp
Normal file
72
libc/mbsrtowcs.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
|
||||||
|
mbsrtowcs.cpp
|
||||||
|
Convert a multibyte string to a wide-character string.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
|
extern "C" size_t mbsrtowcs(wchar_t* dst, const char** src_ptr, size_t dst_len,
|
||||||
|
mbstate_t* ps)
|
||||||
|
{
|
||||||
|
assert(src_ptr && *src_ptr);
|
||||||
|
// Avoid changing *src_ptr if dst is NULL.
|
||||||
|
const char* 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 = strlen(*src_ptr);
|
||||||
|
while ( !dst || dst_len )
|
||||||
|
{
|
||||||
|
mbstate_t saved_ps = *ps;
|
||||||
|
size_t consumed = mbrtowc(dst, *src_ptr, src_len, ps);
|
||||||
|
if ( consumed == (size_t) 0 )
|
||||||
|
{
|
||||||
|
*src_ptr = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ( consumed == (size_t) -1 )
|
||||||
|
return (size_t) -1;
|
||||||
|
if ( consumed == (size_t) -2 )
|
||||||
|
{
|
||||||
|
*ps = saved_ps;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*src_ptr += consumed;
|
||||||
|
src_len -= consumed;
|
||||||
|
if ( dst )
|
||||||
|
dst++,
|
||||||
|
dst_len--;
|
||||||
|
ret++;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
Loading…
Reference in a new issue