From 33c0a9586e4f0673dbdbdf62eb02791933c660df Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 9 Nov 2011 23:48:26 +0100 Subject: [PATCH] Added atoi(3). --- libmaxsi/c/hsrc/stdlib.h | 1 + libmaxsi/hsrc/string.h | 1 + libmaxsi/string.cpp | 14 ++++++++++++++ 3 files changed, 16 insertions(+) diff --git a/libmaxsi/c/hsrc/stdlib.h b/libmaxsi/c/hsrc/stdlib.h index 921439f7..fd6bf59d 100644 --- a/libmaxsi/c/hsrc/stdlib.h +++ b/libmaxsi/c/hsrc/stdlib.h @@ -45,6 +45,7 @@ typedef int div_t, ldiv_t, lldiv_t; /* TODO: WEXITSTATUS, WIFEXITED, WIFSIGNALED, WIFSTOPPED, WNOHANG, WSTOPSIG, WTERMSIG, WUNTRACED is missing here */ +int atoi(const char*); void exit(int); void _Exit(int status); void free(void*); diff --git a/libmaxsi/hsrc/string.h b/libmaxsi/hsrc/string.h index c3bb407d..1c9ebf09 100644 --- a/libmaxsi/hsrc/string.h +++ b/libmaxsi/hsrc/string.h @@ -39,6 +39,7 @@ namespace Maxsi int Compare(const char* A, const char* B); int CompareN(const char* A, const char* B, size_t MaxLength); int StartsWith(const char* Haystack, const char* Needle); + int ToInt(const char* str); int ConvertUInt8T(uint8_t num, char* dest); int ConvertUInt16(uint16_t num, char* dest); diff --git a/libmaxsi/string.cpp b/libmaxsi/string.cpp index 174c46d2..88d1b5ff 100644 --- a/libmaxsi/string.cpp +++ b/libmaxsi/string.cpp @@ -90,6 +90,20 @@ namespace Maxsi return Result; } + DUAL_FUNCTION(int, atoi, ToInt, (const char* str)) + { + bool negative = ( *str == '-' ); + if ( negative ) { str++; } + int result = 0; + int c; + while ( (c = *str++) != 0 ) + { + if ( c < '0' || '9' < c ) { return result; } + result = result * 10 + c - '0'; + } + return (negative) ? -result : result; + } + #if 0 char* Combine(size_t NumParameters, ...) {