Add btowc(3) and wctob(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-05-03 21:34:35 +02:00
parent 20b8a3c639
commit d6067f9da7
4 changed files with 91 additions and 2 deletions

View File

@ -236,6 +236,7 @@ timespec/timespec.o \
time/strftime_l.o \
time/strftime.o \
time/timegm.o \
wchar/btowc.o \
wchar/mbrlen.o \
wchar/mbrtowc.o \
wchar/mbsinit.o \
@ -266,6 +267,7 @@ wchar/wcstoull.o \
wchar/wcstoul.o \
wchar/wcswidth.o \
wchar/wcsxfrm.o \
wchar/wctob.o \
wchar/wcwidth.o \
wchar/wmemchr.o \
wchar/wmemcmp.o \

View File

@ -120,7 +120,7 @@ typedef struct
struct tm;
/* TODO: wint_t btowc(int); */
wint_t btowc(int);
/* TODO: wint_t fgetwc(FILE*); */
/* TODO: wchar_t* fgetws(wchar_t* __restrict, int, FILE* __restrict); */
/* TODO: wint_t fputwc(wchar_t, FILE*); */
@ -155,7 +155,7 @@ wchar_t* wcstok(wchar_t* __restrict, const wchar_t* __restrict, wchar_t** __rest
long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int);
unsigned long wcstoul(const wchar_t* __restrict, wchar_t** __restrict, int);
size_t wcsxfrm(wchar_t* __restrict, const wchar_t* __restrict, size_t);
/* TODO: int wctob(wint_t); */
int wctob(wint_t);
wchar_t* wmemchr(const wchar_t*, wchar_t, size_t);
int wmemcmp(const wchar_t*, const wchar_t*, size_t);
wchar_t* wmemcpy(wchar_t* __restrict, const wchar_t* __restrict, size_t);

44
libc/wchar/btowc.cpp Normal file
View File

@ -0,0 +1,44 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014.
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/>.
wchar/btowc.cpp
Single byte to wide character conversion.
*******************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
extern "C" wint_t btowc(int ic)
{
if ( ic == EOF )
return WEOF;
unsigned char uc = (unsigned char) ic;
char c = (char) uc;
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
wchar_t wc;
int saved_errno = errno;
size_t status = mbrtowc(&wc, (const char*) &c, 1, &ps);
if ( status == (size_t) -1 || status == (size_t) -2 )
return errno = saved_errno, WEOF;
return (wint_t) wc;
}

43
libc/wchar/wctob.cpp Normal file
View File

@ -0,0 +1,43 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014.
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/>.
wchar/wctob.cpp
Wide-character to single-byte conversion.
*******************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
extern "C" int wctob(wint_t wc)
{
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
char mb[MB_CUR_MAX];
int saved_errno = errno;
size_t status = wcrtomb(mb, wc, &ps);
if ( status == (size_t) -1 )
return errno = saved_errno, EOF;
if ( 2 <= status )
return EOF;
return (int) (unsigned char) mb[0];
}