]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/_PDCLIB_strtox_main.c
86b6d42e18c4f05470a9f06c258de965b200dcf5
[pdclib] / functions / _PDCLIB / _PDCLIB_strtox_main.c
1 /* $Id$ */
2
3 /* _PDCLIB_strtox_main( const char * *, int, _PDCLIB_uintmax_t, _PDCLIB_uintmax_t, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <ctype.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <stdint.h>
13
14 #ifndef REGTEST
15 #include <_PDCLIB_int.h>
16 _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, uintmax_t error, uintmax_t limval, int limdigit, char * sign )
17 {
18     _PDCLIB_uintmax_t rc = 0;
19     int digit = -1;
20     const char * x;
21     while ( ( x = memchr( _PDCLIB_digits, tolower(**p), base ) ) != NULL )
22     {
23         digit = x - _PDCLIB_digits;
24         if ( ( rc < limval ) || ( ( rc == limval ) && ( digit <= limdigit ) ) )
25         {
26             rc = rc * base + (unsigned)digit;
27             ++(*p);
28         }
29         else
30         {
31             errno = ERANGE;
32             /* TODO: Only if endptr != NULL - but do we really want *another* parameter? */
33             /* TODO: Earlier version was missing tolower() here but was not caught by tests */
34             while ( memchr( _PDCLIB_digits, tolower(**p), base ) != NULL ) ++(*p);
35             /* TODO: This is ugly, but keeps caller from negating the error value */
36             *sign = '+';
37             return error;
38         }
39     }
40     if ( digit == -1 )
41     {
42         *p = NULL;
43         return 0;
44     }
45     return rc;
46 }
47 #endif
48
49 #ifdef TEST
50 #include <_PDCLIB_test.h>
51 #include <errno.h>
52
53 int main( void )
54 {
55 #ifndef REGTEST
56     const char * p;
57     char test[] = "123_";
58     char fail[] = "xxx";
59     char sign = '-';
60     /* basic functionality */
61     p = test;
62     errno = 0;
63     TESTCASE( _PDCLIB_strtox_main( &p, 10u, (uintmax_t)999, (uintmax_t)12, 3, &sign ) == 123 );
64     TESTCASE( errno == 0 );
65     TESTCASE( p == &test[3] );
66     /* proper functioning to smaller base */
67     p = test;
68     TESTCASE( _PDCLIB_strtox_main( &p, 8u, (uintmax_t)999, (uintmax_t)12, 3, &sign ) == 0123 );
69     TESTCASE( errno == 0 );
70     TESTCASE( p == &test[3] );
71     /* overflowing subject sequence must still return proper endptr */
72     p = test;
73     TESTCASE( _PDCLIB_strtox_main( &p, 4u, (uintmax_t)999, (uintmax_t)1, 2, &sign ) == 999 );
74     TESTCASE( errno == ERANGE );
75     TESTCASE( p == &test[3] );
76     TESTCASE( sign == '+' );
77     /* testing conversion failure */
78     errno = 0;
79     p = fail;
80     sign = '-';
81     TESTCASE( _PDCLIB_strtox_main( &p, 10u, (uintmax_t)999, (uintmax_t)99, 8, &sign ) == 0 );
82     TESTCASE( p == NULL );
83 #endif
84     return TEST_RESULTS;
85 }
86
87 #endif