From 63b1c50cd2ae0562c327b52f0798abe596ab083b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 28 Sep 2012 23:14:00 +0200 Subject: [PATCH] Split libmaxsi integer.cpp into multiple files. --- libmaxsi/Makefile | 3 + libmaxsi/atoi.cpp | 30 ++++++++ libmaxsi/atol.cpp | 30 ++++++++ libmaxsi/atoll.cpp | 30 ++++++++ libmaxsi/integer.cpp | 172 +++++++++++++++++-------------------------- 5 files changed, 161 insertions(+), 104 deletions(-) create mode 100644 libmaxsi/atoi.cpp create mode 100644 libmaxsi/atol.cpp create mode 100644 libmaxsi/atoll.cpp diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index 3b4016ab..75767360 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -33,6 +33,9 @@ FREEOBJS=\ abort.o \ abs.o \ _assert.o \ +atoi.o \ +atoll.o \ +atol.o \ bsearch.o \ calloc.o \ clearerr.o \ diff --git a/libmaxsi/atoi.cpp b/libmaxsi/atoi.cpp new file mode 100644 index 00000000..1b2295eb --- /dev/null +++ b/libmaxsi/atoi.cpp @@ -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 . + + atoi.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#include + +extern "C" int atoi(const char* str) +{ + return (int) strtol(str, (char**) NULL, 10); +} diff --git a/libmaxsi/atol.cpp b/libmaxsi/atol.cpp new file mode 100644 index 00000000..36dfbabf --- /dev/null +++ b/libmaxsi/atol.cpp @@ -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 . + + atol.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#include + +extern "C" long atol(const char* str) +{ + return strtol(str, (char**) NULL, 10); +} diff --git a/libmaxsi/atoll.cpp b/libmaxsi/atoll.cpp new file mode 100644 index 00000000..13f8fd43 --- /dev/null +++ b/libmaxsi/atoll.cpp @@ -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 . + + atoll.cpp + Converts integers represented as strings to binary representation. + +*******************************************************************************/ + +#include + +extern "C" long long atoll(const char* str) +{ + return strtoll(str, (char **) NULL, 10); +} diff --git a/libmaxsi/integer.cpp b/libmaxsi/integer.cpp index 7148c07f..5e023b8f 100644 --- a/libmaxsi/integer.cpp +++ b/libmaxsi/integer.cpp @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011. This file is part of LibMaxsi. @@ -11,116 +11,80 @@ 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. + 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 . - init.cpp - Initializes the process by setting up the heap, signal handling, - static memory and other useful things. + integer.cpp + Converts integers represented as strings to binary representation. -******************************************************************************/ +*******************************************************************************/ +#include #include +#include -namespace Maxsi +static int Debase(char c) { - 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'; } - 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 int strtol(const char* str, char** endptr, int base) - { - return ParseInteger(str, endptr, base); - } - - extern "C" long int strtoll(const char* str, char** endptr, int base) - { - return ParseInteger(str, endptr, base); - } - - extern "C" unsigned long int strtoul(const char* str, char** endptr, int base) - { - return ParseInteger(str, endptr, base); - } - - extern "C" unsigned long int strtoull(const char* str, char** endptr, int base) - { - return ParseInteger(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); - } - } + 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" 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); +}