From b9d633108d423903d27becf5d432fc9218c0b946 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 20 Dec 2012 16:18:24 +0100 Subject: [PATCH] Add wctype(3). --- libc/Makefile | 1 + libc/decl/wctype_t.h | 2 +- libc/include/wctype.h | 34 +++++---- libc/wctype.cpp | 124 +++++++++++++++++++++++++++++++ sortix/include/sortix/x64/bits.h | 1 - sortix/include/sortix/x86/bits.h | 1 - 6 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 libc/wctype.cpp diff --git a/libc/Makefile b/libc/Makefile index 74ad6af3..c819bbaa 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -117,6 +117,7 @@ vfscanf.o \ vsscanf.o \ wcrtomb.o \ wctomb.o \ +wctype.o \ HOSTEDOBJS=\ access.o \ diff --git a/libc/decl/wctype_t.h b/libc/decl/wctype_t.h index f50b1faf..c983014c 100644 --- a/libc/decl/wctype_t.h +++ b/libc/decl/wctype_t.h @@ -1,4 +1,4 @@ #ifndef _WCTYPE_T_DECL #define _WCTYPE_T_DECL -typedef __wctype_t wctype_t; +typedef int (*wctype_t)(wint_t); #endif diff --git a/libc/include/wctype.h b/libc/include/wctype.h index 73ae9b25..0f66a5cd 100644 --- a/libc/include/wctype.h +++ b/libc/include/wctype.h @@ -31,46 +31,48 @@ __BEGIN_DECLS @include(wint_t.h) @include(wctrans_t.h) +@include(wctype_t.h) @include(locale_t.h) @include(WEOF.h) +int iswalnum(wint_t); +int iswalpha(wint_t); +int iswblank(wint_t); +int iswcntrl(wint_t); +int iswctype(wint_t, wctype_t); +int iswdigit(wint_t); +int iswgraph(wint_t); +int iswlower(wint_t); +int iswprint(wint_t); +int iswpunct(wint_t); +int iswspace(wint_t); +int iswupper(wint_t); +int iswxdigit(wint_t); +wint_t towlower(wint_t); +wint_t towupper(wint_t); +wctype_t wctype(const char *); + /* TODO: These are not implemented in sortix libc yet. */ #if defined(__SORTIX_SHOW_UNIMPLEMENTED) -int iswalnum(wint_t); int iswalnum_l(wint_t, locale_t); -int iswalpha(wint_t); int iswalpha_l(wint_t, locale_t); -int iswblank(wint_t); int iswblank_l(wint_t, locale_t); -int iswcntrl(wint_t); int iswcntrl_l(wint_t, locale_t); -int iswctype(wint_t, wctype_t); int iswctype_l(wint_t, wctype_t, locale_t); -int iswdigit(wint_t); int iswdigit_l(wint_t, locale_t); -int iswgraph(wint_t); int iswgraph_l(wint_t, locale_t); -int iswlower(wint_t); int iswlower_l(wint_t, locale_t); -int iswprint(wint_t); int iswprint_l(wint_t, locale_t); -int iswpunct(wint_t); int iswpunct_l(wint_t, locale_t); -int iswspace(wint_t); int iswspace_l(wint_t, locale_t); -int iswupper(wint_t); int iswupper_l(wint_t, locale_t); -int iswxdigit(wint_t); int iswxdigit_l(wint_t, locale_t); wint_t towctrans(wint_t, wctrans_t); wint_t towctrans_l(wint_t, wctrans_t, locale_t); -wint_t towlower(wint_t); wint_t towlower_l(wint_t, locale_t); -wint_t towupper(wint_t); wint_t towupper_l(wint_t, locale_t); wctrans_t wctrans(const char *); wctrans_t wctrans_l(const char *, locale_t); -wctype_t wctype(const char *); wctype_t wctype_l(const char *, locale_t); #endif diff --git a/libc/wctype.cpp b/libc/wctype.cpp new file mode 100644 index 00000000..10c17204 --- /dev/null +++ b/libc/wctype.cpp @@ -0,0 +1,124 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + 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 + + wctype.cpp + Character types. + +*******************************************************************************/ + +#include +#include + +// TODO: Support other locales than ASCII. + +extern "C" int iswalnum(wint_t c) +{ + return iswalpha(c) || iswdigit(c); +} + +extern "C" int iswalpha(wint_t c) +{ + return iswupper(c) || iswlower(c); +} + +extern "C" int iswblank(wint_t c) +{ + return c == L' ' || c == L'\t'; +} + +extern "C" int iswcntrl(wint_t c) +{ + return 0 <= c && c < 32; +} + +extern "C" int iswdigit(wint_t c) +{ + return L'0' <= c && c <= L'9'; +} + +extern "C" int iswgraph(wint_t c) +{ + return L'!' <= c && c <= L'~'; +} + +extern "C" int iswlower(wint_t c) +{ + return L'a' <= c && c <= L'z'; +} + +extern "C" int iswprint(wint_t c) +{ + return iswgraph(c) || c == L' '; +} + +extern "C" int iswpunct(wint_t c) +{ + return iswprint(c) && c != L' ' && !iswalnum(c); +} + +extern "C" int iswspace(wint_t c) +{ + return c == L'\t' || c == L'\n' || c == L'\v' || c == L'\f' || c == L'\r' || c == L' '; +} + +extern "C" int iswupper(wint_t c) +{ + return L'A' <= c && c <= L'Z'; +} + +extern "C" int iswxdigit(wint_t c) +{ + if ( iswdigit(c) ) { return 1; } + if ( L'a' <= c && c <= L'f' ) { return 1; } + if ( L'A' <= c && c <= L'F' ) { return 1; } + return 0; +} + +extern "C" int towlower(wint_t c) +{ + if ( L'A' <= c && c <= L'Z' ) { return L'a' + c - L'A'; } + return c; +} + +extern "C" int towupper(wint_t c) +{ + if ( L'a' <= c && c <= L'z' ) { return L'A' + c - L'a'; } + return c; +} + +extern "C" int iswctype(wint_t c, wctype_t wct) +{ + return wct(c); +} + +extern "C" wctype_t wctype(const char *name) +{ + if ( !strcmp(name, "alnum") ) return (wctype_t) iswalnum; + if ( !strcmp(name, "alpha") ) return (wctype_t) iswalpha; + if ( !strcmp(name, "blank") ) return (wctype_t) iswblank; + if ( !strcmp(name, "cntrl") ) return (wctype_t) iswcntrl; + if ( !strcmp(name, "digit") ) return (wctype_t) iswdigit; + if ( !strcmp(name, "graph") ) return (wctype_t) iswgraph; + if ( !strcmp(name, "lower") ) return (wctype_t) iswlower; + if ( !strcmp(name, "print") ) return (wctype_t) iswprint; + if ( !strcmp(name, "punct") ) return (wctype_t) iswpunct; + if ( !strcmp(name, "space") ) return (wctype_t) iswspace; + if ( !strcmp(name, "upper") ) return (wctype_t) iswupper; + if ( !strcmp(name, "xdigit") ) return (wctype_t) iswxdigit; + return (wctype_t) 0; +} diff --git a/sortix/include/sortix/x64/bits.h b/sortix/include/sortix/x64/bits.h index faa9fd1c..c73cd29e 100644 --- a/sortix/include/sortix/x64/bits.h +++ b/sortix/include/sortix/x64/bits.h @@ -141,7 +141,6 @@ typedef __intmax_t __off_t; #define __OFF_MIN __INTMAX_MIN #define __OFF_MAX __INTMAX_MAX typedef unsigned int __wctrans_t; /* TODO: figure out what this does and typedef it properly. This is just a temporary assignment. */ -typedef unsigned int __wctype_t; /* TODO: figure out what this does and typedef it properly. This is just a temporary assignment. */ typedef unsigned int __useconds_t; typedef __off_t __blksize_t; typedef __off_t __blkcnt_t; diff --git a/sortix/include/sortix/x86/bits.h b/sortix/include/sortix/x86/bits.h index c9e92745..d8c0f49d 100644 --- a/sortix/include/sortix/x86/bits.h +++ b/sortix/include/sortix/x86/bits.h @@ -141,7 +141,6 @@ typedef __intmax_t __off_t; #define __OFF_MIN __INTMAX_MIN #define __OFF_MAX __INTMAX_MAX typedef unsigned int __wctrans_t; /* TODO: figure out what this does and typedef it properly. This is just a temporary assignment. */ -typedef unsigned int __wctype_t; /* TODO: figure out what this does and typedef it properly. This is just a temporary assignment. */ typedef unsigned int __useconds_t; typedef __off_t __blksize_t; typedef __off_t __blkcnt_t;