X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2F_PDCLIB%2Fstrtox_main.c;fp=functions%2F_PDCLIB%2Fstrtox_main.c;h=6ab56cd2d5322011e61a78e016f3861b7fbdd927;hb=f66fc2a4fc1b45e0aa357f2dd0e45ddd1b7c9142;hp=0000000000000000000000000000000000000000;hpb=f486bdde8d103b510fcd0aa6c2e8c5b0c34085e5;p=pdclib.old diff --git a/functions/_PDCLIB/strtox_main.c b/functions/_PDCLIB/strtox_main.c new file mode 100644 index 0000000..6ab56cd --- /dev/null +++ b/functions/_PDCLIB/strtox_main.c @@ -0,0 +1,79 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* _PDCLIB_strtox_main( const char * *, int, _PDCLIB_uintmax_t, _PDCLIB_uintmax_t, int ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_int.h> +#include +#include +#include + +_PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, int base, _PDCLIB_uintmax_t error, _PDCLIB_uintmax_t limval, int limdigit ) +{ + _PDCLIB_uintmax_t rc = 0; + int digit = -1; + const char * x; + while ( ( x = memchr( _PDCLIB_digits, toupper(**p), base ) ) != NULL ) + { + digit = x - _PDCLIB_digits; + if ( ( rc < limval ) || ( ( rc == limval ) && ( digit <= limdigit ) ) ) + { + rc = rc * base + ( x - _PDCLIB_digits ); + ++(*p); + } + else + { + errno = ERANGE; + /* TODO: Only if endptr != NULL */ + while ( memchr( _PDCLIB_digits, **p, base ) != NULL ) ++(*p); + return error; + } + } + if ( digit == -1 ) + { + *p = NULL; + return 0; + } + return rc; +} + +#ifdef TEST +#include <_PDCLIB_test.h> +#include + +int main() +{ + const char * p; + char test[] = "123_"; + char fail[] = "xxx"; + BEGIN_TESTS; + /* basic functionality */ + p = test; + errno = 0; + TESTCASE( _PDCLIB_strtox_main( &p, 10, 999, 12, 3 ) == 123 ); + TESTCASE( errno == 0 ); + TESTCASE( p == &test[3] ); + /* proper functioning to smaller base */ + p = test; + TESTCASE( _PDCLIB_strtox_main( &p, 8, 999, 12, 3 ) == 0123 ); + TESTCASE( errno == 0 ); + TESTCASE( p == &test[3] ); + /* overflowing subject sequence must still return proper endptr */ + p = test; + TESTCASE( _PDCLIB_strtox_main( &p, 4, 999, 1, 2 ) == 999 ); + TESTCASE( errno == ERANGE ); + TESTCASE( p == &test[3] ); + /* testing conversion failure */ + errno = 0; + p = fail; + TESTCASE( _PDCLIB_strtox_main( &p, 10, 999, 99, 8 ) == 0 ); + TESTCASE( p == NULL ); + return TEST_RESULTS; +} + +#endif