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