diff --git a/libc/Makefile b/libc/Makefile index 5ed768f9..f517e131 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -25,6 +25,8 @@ dirent/alphasort.o \ dirent/dir.o \ dirent/versionsort.o \ errno/errno.o \ +inttypes/strtoimax.o \ +inttypes/strtoumax.o \ signal/sigaddset.o \ signal/sigdelset.o \ signal/sigemptyset.o \ @@ -86,7 +88,6 @@ stdlib/bsearch.o \ stdlib/calloc.o \ stdlib/div.o \ stdlib/heap.o \ -stdlib/integer.o \ stdlib/ldiv.o \ stdlib/lldiv.o \ stdlib/mblen.o \ @@ -96,6 +97,10 @@ stdlib/qsort.o \ stdlib/strtod.o \ stdlib/strtof.o \ stdlib/strtold.o \ +stdlib/strtoll.o \ +stdlib/strtol.o \ +stdlib/strtoull.o \ +stdlib/strtoul.o \ stdlib/wcstombs.o \ stdlib/wctomb.o \ string/ffsll.o \ diff --git a/libc/inttypes/strtoimax.cpp b/libc/inttypes/strtoimax.cpp new file mode 100644 index 00000000..0f8e7f86 --- /dev/null +++ b/libc/inttypes/strtoimax.cpp @@ -0,0 +1,29 @@ +/******************************************************************************* + + 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 . + + inttypes/strtoimax.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOL strtoimax +#define STRTOL_INT intmax_t +#define STRTOL_INT_IS_UNSIGNED false + +#include "../stdlib/strtol.cpp" diff --git a/libc/inttypes/strtoumax.cpp b/libc/inttypes/strtoumax.cpp new file mode 100644 index 00000000..cc8d3e80 --- /dev/null +++ b/libc/inttypes/strtoumax.cpp @@ -0,0 +1,29 @@ +/******************************************************************************* + + 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 . + + inttypes/strtoumax.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOL strtoumax +#define STRTOL_INT uintmax_t +#define STRTOL_INT_IS_UNSIGNED true + +#include "../stdlib/strtol.cpp" diff --git a/libc/sortix/inttypes/.gitignore b/libc/sortix/inttypes/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/libc/stdlib/integer.cpp b/libc/stdlib/integer.cpp deleted file mode 100644 index 221bbd61..00000000 --- a/libc/stdlib/integer.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 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 . - - stdlib/integer.cpp - Converts integers represented as strings to binary representation. - -*******************************************************************************/ - -#include -#include -#include -#include - -static int Debase(char c) -{ - if ( '0' <= c && c <= '9' ) { return c - '0'; } - if ( 'a' <= c && c <= 'z' ) { return 10 + c - 'a'; } - if ( 'A' <= c && c <= 'Z' ) { return 10 + c - 'A'; } - return -1; -} - -template INT ParseInteger(const char* str, char** endptr, int base) -{ - const char* origstr = str; - int origbase = base; - while ( isspace(*str) ) { str++; } - if ( base < 0 || 36 < base ) { if ( endptr ) { *endptr = (char*) str; } return 0; } - INT result = 0; - bool negative = false; - char c = *str; - if ( !UNSIGNED && c == '-' ) { str++; negative = true; } - if ( !UNSIGNED && c == '+' ) { str++; negative = false; } - if ( !base && str[0] == '0' ) - { - if ( str[1] == 'x' || str[1] == 'X' ) { str += 2; base = 16; } - else if ( 0 <= Debase(str[1]) && Debase(str[1]) < 8 ) { str++; base = 8; } - } - if ( !base ) { base = 10; } - if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; } - size_t numconvertedchars = 0; - while ( (c = *str ) ) - { - int val = Debase(c); - if ( val < 0 ) { break; } - if ( base <= val ) { break; } - if ( !UNSIGNED && negative ) { val = -val; } - // TODO: Detect overflow! - result = result * (INT) base + (INT) val; - str++; - numconvertedchars++; - } - if ( !numconvertedchars ) { str = origstr; result = 0; } - if ( endptr ) { *endptr = (char*) str; } - return result; -} - -extern "C" long strtol(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} - -extern "C" long long strtoll(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} - -extern "C" intmax_t strtoimax(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} - -extern "C" unsigned long strtoul(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} - -extern "C" unsigned long long strtoull(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} - -extern "C" uintmax_t strtoumax(const char* str, char** endptr, int base) -{ - return ParseInteger(str, endptr, base); -} diff --git a/libc/stdlib/strtol.cpp b/libc/stdlib/strtol.cpp new file mode 100644 index 00000000..e7dcfc5d --- /dev/null +++ b/libc/stdlib/strtol.cpp @@ -0,0 +1,102 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 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 . + + stdlib/strtol.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#ifndef STRTOL +#define STRTOL strtol +#define STRTOL_INT long +#define STRTOL_INT_IS_UNSIGNED false +#endif + +#include +#include +#include +#include + +static int Debase(char c) +{ + if ( '0' <= c && c <= '9' ) + return c - '0'; + if ( 'a' <= c && c <= 'z' ) + return 10 + c - 'a'; + if ( 'A' <= c && c <= 'Z' ) + return 10 + c - 'A'; + return -1; +} + +template +INT ParseInteger(const char* str, char** endptr, int base) +{ + const char* origstr = str; + int origbase = base; + while ( isspace(*str) ) { str++; } + if ( base < 0 || 36 < base ) + { + if ( endptr ) + *endptr = (char*) str; + return 0; + } + INT result = 0; + bool negative = false; + char c = *str; + if ( !UNSIGNED && c == '-' ) + str++, negative = true; + if ( !UNSIGNED && c == '+' ) + str++, negative = false; + if ( !base && str[0] == '0' ) + { + if ( str[1] == 'x' || str[1] == 'X' ) + str += 2, base = 16; + else if ( 0 <= Debase(str[1]) && Debase(str[1]) < 8 ) + str++, base = 8; + } + if ( !base ) + base = 10; + if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) + str += 2; + size_t numconvertedchars = 0; + while ( (c = *str ) ) + { + int val = Debase(c); + if ( val < 0 ) + break; + if ( base <= val ) + break; + if ( !UNSIGNED && negative ) + val = -val; + // TODO: Detect overflow! + result = result * (INT) base + (INT) val; + str++; + numconvertedchars++; + } + if ( !numconvertedchars ) + str = origstr, result = 0; + if ( endptr ) + *endptr = (char*) str; + return result; +} + +extern "C" STRTOL_INT STRTOL(const char* str, char** endptr, int base) +{ + return ParseInteger(str, endptr, base); +} diff --git a/libc/stdlib/strtoll.cpp b/libc/stdlib/strtoll.cpp new file mode 100644 index 00000000..7e1e34d9 --- /dev/null +++ b/libc/stdlib/strtoll.cpp @@ -0,0 +1,29 @@ +/******************************************************************************* + + 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 . + + stdlib/strtoll.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOL strtoll +#define STRTOL_INT long long +#define STRTOL_INT_IS_UNSIGNED true + +#include "strtol.cpp" diff --git a/libc/stdlib/strtoul.cpp b/libc/stdlib/strtoul.cpp new file mode 100644 index 00000000..da0d0d11 --- /dev/null +++ b/libc/stdlib/strtoul.cpp @@ -0,0 +1,29 @@ +/******************************************************************************* + + 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 . + + stdlib/strtoul.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOL strtoul +#define STRTOL_INT unsigned long +#define STRTOL_INT_IS_UNSIGNED true + +#include "strtol.cpp" diff --git a/libc/stdlib/strtoull.cpp b/libc/stdlib/strtoull.cpp new file mode 100644 index 00000000..07092358 --- /dev/null +++ b/libc/stdlib/strtoull.cpp @@ -0,0 +1,29 @@ +/******************************************************************************* + + 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 . + + stdlib/strtoull.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#define STRTOL strtoull +#define STRTOL_INT unsigned long long +#define STRTOL_INT_IS_UNSIGNED true + +#include "strtol.cpp"