1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add wcstof(3), wcstod(3) and wcstold(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-05-28 18:26:30 +02:00
parent ba12c1d246
commit a1e9c15bca
8 changed files with 126 additions and 26 deletions

View file

@ -260,7 +260,10 @@ wchar/wcsrchr.o \
wchar/wcsrtombs.o \ wchar/wcsrtombs.o \
wchar/wcsspn.o \ wchar/wcsspn.o \
wchar/wcsstr.o \ wchar/wcsstr.o \
wchar/wcstod.o \
wchar/wcstof.o \
wchar/wcstok.o \ wchar/wcstok.o \
wchar/wcstold.o \
wchar/wcstoll.o \ wchar/wcstoll.o \
wchar/wcstol.o \ wchar/wcstol.o \
wchar/wcstoull.o \ wchar/wcstoull.o \

View file

@ -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 wcsrtombs(char* __restrict, const wchar_t** __restrict, size_t, mbstate_t* __restrict);
size_t wcsspn(const wchar_t*, const wchar_t*); size_t wcsspn(const wchar_t*, const wchar_t*);
wchar_t* wcsstr(const wchar_t* __restrict, const wchar_t* __restrict); 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); wchar_t* wcstok(wchar_t* __restrict, const wchar_t* __restrict, wchar_t** __restrict);
long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int); long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int);
unsigned long wcstoul(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 #if __USE_SORTIX || 1999 <= __USE_C
/* TODO: int vfwscanf(FILE* __restrict, const wchar_t* __restrict, va_list); */ /* 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: int vswscanf(const wchar_t* __restrict, const wchar_t* __restrict, va_list); */
/* TODO: float wcstof(const wchar_t* __restrict, wchar_t** __restrict); */ float wcstof(const wchar_t* __restrict, wchar_t** __restrict);
/* TODO: long double wcstold(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: 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); */ /* 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); long long wcstoll(const wchar_t* __restrict, wchar_t** __restrict, int);

View file

@ -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. 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 strtod
#define STRTOF_CHAR char
#define STRTOF_L(x) x
#include "strtof.cpp" #include "strtof.cpp"

View file

@ -22,13 +22,15 @@
*******************************************************************************/ *******************************************************************************/
#ifndef FLOAT #ifndef STRTOF
#define FLOAT float #define STRTOF_FLOAT float
#define STRTOF strtof #define STRTOF strtof
#define STRTOF_CHAR char
#define STRTOF_L(x) x
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <wchar.h>
// TODO: This horribly hacky code is taken from sdlquake's common.c, which is // 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 // 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. // writing a real strtod function - most of them are true horrors.
#if !defined(__is_sortix_kernel) #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; int sign = *str == STRTOF_L('-') ? (str++, -1) : 1;
FLOAT val = 0.0; STRTOF_FLOAT val = 0.0;
if ( false ) if ( false )
{ {
out: out:
if ( nptr ) if ( nptr )
*nptr = (char*) str; *nptr = (STRTOF_CHAR*) str;
return val * sign; 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; str += 2;
while ( true ) while ( true )
{ {
char c = *str++; STRTOF_CHAR c = *str++;
if ( '0' <= c && c <= '9' ) if ( STRTOF_L('0') <= c && c <= STRTOF_L('9') )
val = val * 16 + c - '0'; val = val * 16 + c - STRTOF_L('0');
else if ( 'a' <= c && c <= 'f' ) else if ( STRTOF_L('a') <= c && c <= STRTOF_L('f') )
val = val * 16 + c - 'a' + 10; val = val * 16 + c - STRTOF_L('a') + 10;
else if ( 'A' <= c && c <= 'F' ) else if ( STRTOF_L('A') <= c && c <= STRTOF_L('F') )
val = val * 16 + c - 'A' + 10; val = val * 16 + c - STRTOF_L('A') + 10;
else else
goto out; goto out;
} }
@ -70,15 +73,15 @@ extern "C" FLOAT STRTOF(const char* str, char** nptr)
int total = 0; int total = 0;
while ( true ) while ( true )
{ {
char c = *str++; STRTOF_CHAR c = *str++;
if (c == '.') if ( c == STRTOF_L('.') )
{ {
decimal = total; decimal = total;
continue; continue;
} }
if ( c < '0' || c > '9' ) if ( c < STRTOF_L('0') || c > STRTOF_L('9') )
break; break;
val = val * 10 + c - '0'; val = val * 10 + c - STRTOF_L('0');
total++; total++;
} }

View file

@ -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. 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 strtold
#define STRTOF_CHAR char
#define STRTOF_L(x) x
#include "strtof.cpp" #include "strtof.cpp"

30
libc/wchar/wcstod.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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"

30
libc/wchar/wcstof.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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"

30
libc/wchar/wcstold.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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"