From a1e9c15bca89bc44b87aa1863a0a2482043078ed Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 28 May 2014 18:26:30 +0200 Subject: [PATCH] Add wcstof(3), wcstod(3) and wcstold(3). --- libc/Makefile | 3 +++ libc/include/wchar.h | 6 +++--- libc/stdlib/strtod.cpp | 6 ++++-- libc/stdlib/strtof.cpp | 41 ++++++++++++++++++++++------------------- libc/stdlib/strtold.cpp | 6 ++++-- libc/wchar/wcstod.cpp | 30 ++++++++++++++++++++++++++++++ libc/wchar/wcstof.cpp | 30 ++++++++++++++++++++++++++++++ libc/wchar/wcstold.cpp | 30 ++++++++++++++++++++++++++++++ 8 files changed, 126 insertions(+), 26 deletions(-) create mode 100644 libc/wchar/wcstod.cpp create mode 100644 libc/wchar/wcstof.cpp create mode 100644 libc/wchar/wcstold.cpp diff --git a/libc/Makefile b/libc/Makefile index bafd43f8..e63b8dc9 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -260,7 +260,10 @@ wchar/wcsrchr.o \ wchar/wcsrtombs.o \ wchar/wcsspn.o \ wchar/wcsstr.o \ +wchar/wcstod.o \ +wchar/wcstof.o \ wchar/wcstok.o \ +wchar/wcstold.o \ wchar/wcstoll.o \ wchar/wcstol.o \ wchar/wcstoull.o \ diff --git a/libc/include/wchar.h b/libc/include/wchar.h index 0198004c..aef37e33 100644 --- a/libc/include/wchar.h +++ b/libc/include/wchar.h @@ -150,7 +150,7 @@ 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* wcsstr(const wchar_t* __restrict, const wchar_t* __restrict); -/* TODO: double wcstod(const wchar_t* __restrict, wchar_t** __restrict); */ +double wcstod(const wchar_t* __restrict, wchar_t** __restrict); wchar_t* wcstok(wchar_t* __restrict, const wchar_t* __restrict, wchar_t** __restrict); long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int); unsigned long wcstoul(const wchar_t* __restrict, wchar_t** __restrict, int); @@ -179,8 +179,8 @@ wchar_t* wmemset(wchar_t*, wchar_t, size_t); #if __USE_SORTIX || 1999 <= __USE_C /* TODO: int vfwscanf(FILE* __restrict, const wchar_t* __restrict, va_list); */ /* TODO: int vswscanf(const wchar_t* __restrict, const wchar_t* __restrict, va_list); */ -/* TODO: float wcstof(const wchar_t* __restrict, wchar_t** __restrict); */ -/* TODO: long double wcstold(const wchar_t* __restrict, wchar_t** __restrict); */ +float wcstof(const wchar_t* __restrict, wchar_t** __restrict); +long double wcstold(const wchar_t* __restrict, wchar_t** __restrict); /* TODO: int vwscanf(const wchar_t* __restrict, va_list); */ /* TODO: size_t wcsftime(wchar_t* __restrict, size_t, const wchar_t* __restrict, const struct tm* __restrict); */ long long wcstoll(const wchar_t* __restrict, wchar_t** __restrict, int); diff --git a/libc/stdlib/strtod.cpp b/libc/stdlib/strtod.cpp index c64e8389..1f98896f 100644 --- a/libc/stdlib/strtod.cpp +++ b/libc/stdlib/strtod.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of the Sortix C Library. @@ -22,7 +22,9 @@ *******************************************************************************/ -#define FLOAT double +#define STRTOF_FLOAT double #define STRTOF strtod +#define STRTOF_CHAR char +#define STRTOF_L(x) x #include "strtof.cpp" diff --git a/libc/stdlib/strtof.cpp b/libc/stdlib/strtof.cpp index a41eec71..2e07df53 100644 --- a/libc/stdlib/strtof.cpp +++ b/libc/stdlib/strtof.cpp @@ -22,13 +22,15 @@ *******************************************************************************/ -#ifndef FLOAT -#define FLOAT float +#ifndef STRTOF +#define STRTOF_FLOAT float #define STRTOF strtof +#define STRTOF_CHAR char +#define STRTOF_L(x) x #endif #include -#include +#include // TODO: This horribly hacky code is taken from sdlquake's common.c, which is // licensed under the GPL. Since most Sortix software is GPL-compatible @@ -36,31 +38,32 @@ // writing a real strtod function - most of them are true horrors. #if !defined(__is_sortix_kernel) -extern "C" FLOAT STRTOF(const char* str, char** nptr) +extern "C" STRTOF_FLOAT STRTOF(const STRTOF_CHAR* str, STRTOF_CHAR** nptr) { - int sign = *str == '-' ? (str++, -1) : 1; - FLOAT val = 0.0; + int sign = *str == STRTOF_L('-') ? (str++, -1) : 1; + STRTOF_FLOAT val = 0.0; if ( false ) { out: if ( nptr ) - *nptr = (char*) str; + *nptr = (STRTOF_CHAR*) str; return val * sign; } - if ( str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) + if ( str[0] == STRTOF_L('0') && + (str[1] == STRTOF_L('x') || str[1] == STRTOF_L('X')) ) { str += 2; while ( true ) { - char c = *str++; - if ( '0' <= c && c <= '9' ) - val = val * 16 + c - '0'; - else if ( 'a' <= c && c <= 'f' ) - val = val * 16 + c - 'a' + 10; - else if ( 'A' <= c && c <= 'F' ) - val = val * 16 + c - 'A' + 10; + STRTOF_CHAR c = *str++; + if ( STRTOF_L('0') <= c && c <= STRTOF_L('9') ) + val = val * 16 + c - STRTOF_L('0'); + else if ( STRTOF_L('a') <= c && c <= STRTOF_L('f') ) + val = val * 16 + c - STRTOF_L('a') + 10; + else if ( STRTOF_L('A') <= c && c <= STRTOF_L('F') ) + val = val * 16 + c - STRTOF_L('A') + 10; else goto out; } @@ -70,15 +73,15 @@ extern "C" FLOAT STRTOF(const char* str, char** nptr) int total = 0; while ( true ) { - char c = *str++; - if (c == '.') + STRTOF_CHAR c = *str++; + if ( c == STRTOF_L('.') ) { decimal = total; continue; } - if ( c < '0' || c > '9' ) + if ( c < STRTOF_L('0') || c > STRTOF_L('9') ) break; - val = val * 10 + c - '0'; + val = val * 10 + c - STRTOF_L('0'); total++; } diff --git a/libc/stdlib/strtold.cpp b/libc/stdlib/strtold.cpp index db981fa5..9ad1da33 100644 --- a/libc/stdlib/strtold.cpp +++ b/libc/stdlib/strtold.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2013. + Copyright(C) Jonas 'Sortie' Termansen 2013, 2014. This file is part of the Sortix C Library. @@ -22,7 +22,9 @@ *******************************************************************************/ -#define FLOAT long double +#define STRTOF_FLOAT long double #define STRTOF strtold +#define STRTOF_CHAR char +#define STRTOF_L(x) x #include "strtof.cpp" diff --git a/libc/wchar/wcstod.cpp b/libc/wchar/wcstod.cpp new file mode 100644 index 00000000..9514fb61 --- /dev/null +++ b/libc/wchar/wcstod.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + 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 . + + wchar/wcstod.cpp + Converts floating numbers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOF_FLOAT double +#define STRTOF wcstod +#define STRTOF_CHAR wchar_t +#define STRTOF_L(x) L##x + +#include "../stdlib/strtof.cpp" diff --git a/libc/wchar/wcstof.cpp b/libc/wchar/wcstof.cpp new file mode 100644 index 00000000..450f8fbf --- /dev/null +++ b/libc/wchar/wcstof.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + 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 . + + wchar/wcstof.cpp + Converts floating numbers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOF_FLOAT float +#define STRTOF wcstof +#define STRTOF_CHAR wchar_t +#define STRTOF_L(x) L##x + +#include "../stdlib/strtof.cpp" diff --git a/libc/wchar/wcstold.cpp b/libc/wchar/wcstold.cpp new file mode 100644 index 00000000..a881ea11 --- /dev/null +++ b/libc/wchar/wcstold.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + + 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 . + + wchar/wcstold.cpp + Converts floating numbers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOF_FLOAT long double +#define STRTOF wcstold +#define STRTOF_CHAR wchar_t +#define STRTOF_L(x) L##x + +#include "../stdlib/strtof.cpp"