Пример #1
0
long SDL_strtol(const char *string, char **endp, int base)
{
    size_t len;
    long value;

    if ( !base ) {
        if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
            base = 16;
        } else {
            base = 10;
        }
    }

    len = SDL_ScanLong(string, base, &value);
    if ( endp ) {
        *endp = (char *)string + len;
    }
    return value;
}
Пример #2
0
long
SDL_strtol(const char *string, char **endp, int base)
{
#if defined(HAVE_STRTOL)
    return strtol(string, endp, base);
#else
    size_t len;
    long value;

    if (!base) {
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
            base = 16;
        } else {
            base = 10;
        }
    }

    len = SDL_ScanLong(string, base, &value);
    if (endp) {
        *endp = (char *) string + len;
    }
    return value;
#endif /* HAVE_STRTOL */
}
Пример #3
0
int SDL_sscanf(const char *text, const char *fmt, ...)
{
    va_list ap;
    int retval = 0;

    va_start(ap, fmt);
    while ( *fmt ) {
        if ( *fmt == ' ' ) {
            while ( SDL_isspace((unsigned char) *text) ) {
                ++text;
            }
            ++fmt;
            continue;
        }
        if ( *fmt == '%' ) {
            SDL_bool done = SDL_FALSE;
            long count = 0;
            int radix = 10;
            enum {
                DO_SHORT,
                DO_INT,
                DO_LONG,
                DO_LONGLONG
            } inttype = DO_INT;
            SDL_bool suppress = SDL_FALSE;

            ++fmt;
            if ( *fmt == '%' ) {
                if ( *text == '%' ) {
                    ++text;
                    ++fmt;
                    continue;
                }
                break;
            }
            if ( *fmt == '*' ) {
                suppress = SDL_TRUE;
                ++fmt;
            }
            fmt += SDL_ScanLong(fmt, 10, &count);

            if ( *fmt == 'c' ) {
                if ( ! count ) {
                    count = 1;
                }
                if ( suppress ) {
                    while ( count-- ) {
                        ++text;
                    }
                } else {
                    char *valuep = va_arg(ap, char*);
                    while ( count-- ) {
                        *valuep++ = *text++;
                    }
                    ++retval;
                }
                continue;
            }

            while ( SDL_isspace((unsigned char) *text) ) {
                ++text;
            }

            /* FIXME: implement more of the format specifiers */
            while (!done) {
                switch(*fmt) {
                    case '*':
                        suppress = SDL_TRUE;
                        break;
                    case 'h':
                        if ( inttype > DO_SHORT ) {
                            ++inttype;
                        }
                        break;
                    case 'l':
                        if ( inttype < DO_LONGLONG ) {
                            ++inttype;
                        }
                        break;
                    case 'I':
                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
                            fmt += 2;
                            inttype = DO_LONGLONG;
                        }
                        break;
                    case 'i':
                        {
                            int index = 0;
                            if ( text[index] == '-' ) {
                                ++index;
                            }
                            if ( text[index] == '0' ) {
                                if ( SDL_tolower((unsigned char) text[index+1]) == 'x' ) {
                                    radix = 16;
                                } else {
                                    radix = 8;
                                }
                            }
                        }
                        /* Fall through to %d handling */
                    case 'd':
#ifdef SDL_HAS_64BIT_TYPE
                        if ( inttype == DO_LONGLONG ) {
                            Sint64 value;
                            text += SDL_ScanLongLong(text, radix, &value);
                            if ( ! suppress ) {
                                Sint64 *valuep = va_arg(ap, Sint64*);
                                *valuep = value;
                                ++retval;
                            }
                        }
                        else
#endif /* SDL_HAS_64BIT_TYPE */
                        {
                            long value;
                            text += SDL_ScanLong(text, radix, &value);
                            if ( ! suppress ) {
                                switch (inttype) {
                                    case DO_SHORT:
                                        { short* valuep = va_arg(ap, short*);
                                            *valuep = (short)value;
                                        }
                                        break;
                                    case DO_INT:
                                        { int* valuep = va_arg(ap, int*);
                                            *valuep = (int)value;
                                        }
                                        break;
                                    case DO_LONG:
                                        { long* valuep = va_arg(ap, long*);
                                            *valuep = value;
                                        }
                                        break;
                                    case DO_LONGLONG:
                                        /* Handled above */
                                        break;
                                }
                                ++retval;
                            }
                        }
                        done = SDL_TRUE;
                        break;
                    case 'o':
                        if ( radix == 10 ) {
                            radix = 8;
                        }
                        /* Fall through to unsigned handling */
                    case 'x':
                    case 'X':
                        if ( radix == 10 ) {
                            radix = 16;
                        }
                        /* Fall through to unsigned handling */
                    case 'u':
#ifdef SDL_HAS_64BIT_TYPE
                        if ( inttype == DO_LONGLONG ) {
                            Uint64 value;
                            text += SDL_ScanUnsignedLongLong(text, radix, &value);
                            if ( ! suppress ) {
                                Uint64 *valuep = va_arg(ap, Uint64*);
                                *valuep = value;
                                ++retval;
                            }