mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Split libmaxsi integer.cpp into multiple files.
This commit is contained in:
parent
9306c8f645
commit
63b1c50cd2
5 changed files with 161 additions and 104 deletions
|
@ -33,6 +33,9 @@ FREEOBJS=\
|
||||||
abort.o \
|
abort.o \
|
||||||
abs.o \
|
abs.o \
|
||||||
_assert.o \
|
_assert.o \
|
||||||
|
atoi.o \
|
||||||
|
atoll.o \
|
||||||
|
atol.o \
|
||||||
bsearch.o \
|
bsearch.o \
|
||||||
calloc.o \
|
calloc.o \
|
||||||
clearerr.o \
|
clearerr.o \
|
||||||
|
|
30
libmaxsi/atoi.cpp
Normal file
30
libmaxsi/atoi.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
||||||
|
|
||||||
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
LibMaxsi 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.
|
||||||
|
|
||||||
|
LibMaxsi 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 LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
atoi.cpp
|
||||||
|
Converts integers represented as strings to binary representation.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern "C" int atoi(const char* str)
|
||||||
|
{
|
||||||
|
return (int) strtol(str, (char**) NULL, 10);
|
||||||
|
}
|
30
libmaxsi/atol.cpp
Normal file
30
libmaxsi/atol.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
||||||
|
|
||||||
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
LibMaxsi 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.
|
||||||
|
|
||||||
|
LibMaxsi 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 LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
atol.cpp
|
||||||
|
Converts integers represented as strings to binary representation.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern "C" long atol(const char* str)
|
||||||
|
{
|
||||||
|
return strtol(str, (char**) NULL, 10);
|
||||||
|
}
|
30
libmaxsi/atoll.cpp
Normal file
30
libmaxsi/atoll.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
|
||||||
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
||||||
|
|
||||||
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
LibMaxsi 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.
|
||||||
|
|
||||||
|
LibMaxsi 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 LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
atoll.cpp
|
||||||
|
Converts integers represented as strings to binary representation.
|
||||||
|
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern "C" long long atoll(const char* str)
|
||||||
|
{
|
||||||
|
return strtoll(str, (char **) NULL, 10);
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
||||||
|
|
||||||
This file is part of LibMaxsi.
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
@ -11,116 +11,80 @@
|
||||||
|
|
||||||
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||||
more details.
|
details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
init.cpp
|
integer.cpp
|
||||||
Initializes the process by setting up the heap, signal handling,
|
Converts integers represented as strings to binary representation.
|
||||||
static memory and other useful things.
|
|
||||||
|
|
||||||
******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
namespace Maxsi
|
static int Debase(char c)
|
||||||
{
|
{
|
||||||
namespace Integer
|
if ( '0' <= c && c <= '9' ) { return c - '0'; }
|
||||||
{
|
if ( 'a' <= c && c <= 'z' ) { return 10 + c - 'a'; }
|
||||||
static bool IsSpace(char c)
|
if ( 'A' <= c && c <= 'Z' ) { return 10 + c - 'A'; }
|
||||||
{
|
return -1;
|
||||||
switch ( c )
|
|
||||||
{
|
|
||||||
case ' ':
|
|
||||||
case '\r':
|
|
||||||
case '\f':
|
|
||||||
case '\n':
|
|
||||||
case '\t':
|
|
||||||
case '\v':
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 <class INT, bool UNSIGNED> 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 int strtol(const char* str, char** endptr, int base)
|
|
||||||
{
|
|
||||||
return ParseInteger<long int, false>(str, endptr, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" long int strtoll(const char* str, char** endptr, int base)
|
|
||||||
{
|
|
||||||
return ParseInteger<long long int, false>(str, endptr, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" unsigned long int strtoul(const char* str, char** endptr, int base)
|
|
||||||
{
|
|
||||||
return ParseInteger<unsigned long int, true>(str, endptr, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" unsigned long int strtoull(const char* str, char** endptr, int base)
|
|
||||||
{
|
|
||||||
return ParseInteger<unsigned long long int, true>(str, endptr, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" int atoi(const char* str)
|
|
||||||
{
|
|
||||||
return (int) strtol(str, (char **) NULL, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" long atol(const char* str)
|
|
||||||
{
|
|
||||||
return strtol(str, (char **) NULL, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" long long atoll(const char* str)
|
|
||||||
{
|
|
||||||
return strtoll(str, (char **) NULL, 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class INT, bool UNSIGNED> 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<long, false>(str, endptr, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" long long strtoll(const char* str, char** endptr, int base)
|
||||||
|
{
|
||||||
|
return ParseInteger<long long, false>(str, endptr, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" unsigned long strtoul(const char* str, char** endptr, int base)
|
||||||
|
{
|
||||||
|
return ParseInteger<unsigned long, true>(str, endptr, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" unsigned long long strtoull(const char* str, char** endptr, int base)
|
||||||
|
{
|
||||||
|
return ParseInteger<unsigned long long, true>(str, endptr, base);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue