src/lib/ppc405lib/strtox.c
// The most basic API is strtox(), which converts a string to an unsigned long
// long. All of the base APIs are written in terms of this.  This is legal due
// to the fact that conversion is defined to continue even in the event of
// overflow. This API may return the codes STRTOX_NO_CONVERSION_EMPTY, 
// STRTOX_NO_CONVERSION_PARSE or STRTOX_INVALID_ARGUMENT, 
// which the standard APIs always convert to a 0
// return value.  Otherwise the flags 'overflow' and 'negative' are used by
// the base APIs to determine how to handle special cases.
The most basic API is strtox(), which converts a string to an unsigned long
long. All of the base APIs are written in terms of this.  This is legal due
to the fact that conversion is defined to continue even in the event of
overflow. This API may return the codes STRTOX_NO_CONVERSION_EMPTY,
STRTOX_NO_CONVERSION_PARSE or STRTOX_INVALID_ARGUMENT,
which the standard APIs always convert to a 0
return value.  Otherwise the flags 'overflow' and 'negative' are used by
the base APIs to determine how to handle special cases.
The most basic API is strtox () which converts a string to an unsigned long long . All of the base APIs are written in terms of this . This is legal due to the fact that conversion is defined to continue even in the event of overflow . This API may return the codes STRTOX_NO_CONVERSION_EMPTY STRTOX_NO_CONVERSION_PARSE or STRTOX_INVALID_ARGUMENT which the standard APIs always convert to a 0 return value . Otherwise the flags ' overflow ' and ' negative ' are used by the base APIs to determine how to handle special cases .
static int
strtox(const char *str, char **endptr, int base, 
        unsigned long long* value, int* negative, int* overflow)
{
    const char* s;
    unsigned long long new;
    int rc, radix, digit;
    do {
        s = str;
        *value = 0;
        *negative = 0;
        *overflow = 0;
        if ((base != 0) && ((base < 2) || (base > 36))) {
            rc = STRTOX_INVALID_ARGUMENT;
            break;
        }
        s = skip_whitespace(s);
        if (*s == '\0') {
            rc = STRTOX_NO_CONVERSION_EMPTY;
            break;
        }
        *negative = sign(&s);
        if (base == 0) {
            radix = radix_mark(&s);
        } else {
            radix = base;
            if (radix == 16) {
                radix_mark(&s);
            }
        }
        rc = STRTOX_NO_CONVERSION_PARSE;
        while ((digit = parse_digit(*s, radix)) >= 0) {
            s++;
            if (!*overflow) {
                rc = 0;
                new = (*value * radix) + digit;
                if (new < *value) {
                    *overflow = 1;
                } else {
                    *value = new;
                }
            }
        }
    } while(0);
    if (endptr) {
        if (rc == 0) {
            *endptr = (char*)s;
        } else {
            *endptr = (char*)str;
        }
    }
    return rc;
}
static int strtox ( const char * str , char * * endptr , int base , unsigned long long * value , int * negative , int * overflow ) { const char * s ; unsigned long long new ; int rc , radix , digit ; do { s = str ; * value = 0 ; * negative = 0 ; * overflow = 0 ; if ( ( base != 0 ) && ( ( base < 2 ) || ( base > 36 ) ) ) { rc = STRTOX_INVALID_ARGUMENT ; break ; } s = skip_whitespace ( s ) ; if ( * s == ' \0 ' ) { rc = STRTOX_NO_CONVERSION_EMPTY ; break ; } * negative = sign ( & s ) ; if ( base == 0 ) { radix = radix_mark ( & s ) ; } else { radix = base ; if ( radix == 16 ) { radix_mark ( & s ) ; } } rc = STRTOX_NO_CONVERSION_PARSE ; while ( ( digit = parse_digit ( * s , radix ) ) >= 0 ) { s ++ ; if ( ! * overflow ) { rc = 0 ; new = ( * value * radix ) + digit ; if ( new < * value ) { * overflow = 1 ; } else { * value = new ; } } } } while ( 0 ) ; if ( endptr ) { if ( rc == 0 ) { * endptr = ( char * ) s ; } else { * endptr = ( char * ) str ; } } return rc ; }
The most basic API is strtox(), which converts a string to an unsigned long
long.
The most basic API is strtox () which converts a string to an unsigned long long .
// Initial error checks // Skip whitespace // Process a +/- sign.  Only one is allowed. // Look for a radix mark.  Note that if base == 16 this will cause the // skip of a leading 0 in the string not followed by [xX], but that's // OK because it doesn't change the result of the conversion. // Parse.  Note that once overflow is detected we continue to parse // (but ignore the data).