2011-12-16 10:49:27 -05:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
|
|
|
init.cpp
|
|
|
|
Initializes the process by setting up the heap, signal handling,
|
|
|
|
static memory and other useful things.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-02-11 20:03:34 -05:00
|
|
|
#include <libmaxsi/platform.h>
|
2011-12-16 10:49:27 -05:00
|
|
|
|
|
|
|
namespace Maxsi
|
|
|
|
{
|
|
|
|
namespace Integer
|
|
|
|
{
|
|
|
|
static bool IsSpace(char c)
|
|
|
|
{
|
|
|
|
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'; }
|
2011-12-16 15:15:26 -05:00
|
|
|
if ( 'a' <= c && c <= 'z' ) { return 10 + c - 'a'; }
|
|
|
|
if ( 'A' <= c && c <= 'Z' ) { return 10 + c - 'A'; }
|
2011-12-16 10:49:27 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class INT, bool UNSIGNED> INT ParseInteger(const char* str, char** endptr, int base)
|
|
|
|
{
|
2011-12-16 15:44:32 -05:00
|
|
|
const char* origstr = str;
|
2011-12-16 10:49:27 -05:00
|
|
|
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; }
|
2011-12-16 16:24:29 -05:00
|
|
|
else if ( 0 <= Debase(str[1]) && Debase(str[1]) < 8 ) { str++; base = 8; }
|
2011-12-16 10:49:27 -05:00
|
|
|
}
|
|
|
|
if ( !base ) { base = 10; }
|
|
|
|
if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; }
|
2011-12-16 15:44:32 -05:00
|
|
|
size_t numconvertedchars = 0;
|
2011-12-16 12:28:22 -05:00
|
|
|
while ( (c = *str ) )
|
2011-12-16 10:49:27 -05:00
|
|
|
{
|
|
|
|
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;
|
2011-12-16 15:15:26 -05:00
|
|
|
str++;
|
2011-12-16 15:44:32 -05:00
|
|
|
numconvertedchars++;
|
2011-12-16 10:49:27 -05:00
|
|
|
}
|
2011-12-16 15:44:32 -05:00
|
|
|
if ( !numconvertedchars ) { str = origstr; result = 0; }
|
2011-12-16 10:49:27 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|