Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static PRStatus
- GetInt(ScanfState *state, int code)
- {
- char buf[FMAX + 1], *p;
- int ch;
- static const char digits[] = "0123456789abcdefABCDEF";
- PRBool seenDigit = PR_FALSE;
- int base;
- int dlen;
- switch (code) {
- case 'd': case 'u':
- base = 10;
- break;
- case 'i':
- base = 0;
- break;
- case 'x': case 'X': case 'p':
- base = 16;
- break;
- case 'o':
- base = 8;
- break;
- default:
- return PR_FAILURE;
- }
- if (state->width == 0 || state->width > FMAX) {
- state->width = FMAX;
- }
- p = buf;
- GET_IF_WITHIN_WIDTH(state, ch);
- if (WITHIN_WIDTH(state) && (ch == '+' || ch == '-')) {
- *p++ = ch;
- GET_IF_WITHIN_WIDTH(state, ch);
- }
- if (WITHIN_WIDTH(state) && ch == '0') {
- seenDigit = PR_TRUE;
- *p++ = ch;
- GET_IF_WITHIN_WIDTH(state, ch);
- if (WITHIN_WIDTH(state)
- && (ch == 'x' || ch == 'X')
- && (base == 0 || base == 16)) {
- base = 16;
- *p++ = ch;
- GET_IF_WITHIN_WIDTH(state, ch);
- } else if (base == 0) {
- base = 8;
- }
- }
- if (base == 0 || base == 10) {
- dlen = 10;
- } else if (base == 8) {
- dlen = 8;
- } else {
- PR_ASSERT(base == 16);
- dlen = 16 + 6; /* 16 digits, plus 6 in uppercase */
- }
- while (WITHIN_WIDTH(state) && memchr(digits, ch, dlen)) {
- *p++ = ch;
- GET_IF_WITHIN_WIDTH(state, ch);
- seenDigit = PR_TRUE;
- }
- if (WITHIN_WIDTH(state)) {
- UNGET(state, ch);
- }
- if (!seenDigit) {
- return PR_FAILURE;
- }
- *p = '\0';
- if (state->assign) {
- if (code == 'd' || code == 'i') {
- if (state->sizeSpec == _PR_size_ll) {
- PRInt64 llval = _pr_strtoull(buf, NULL, base);
- *va_arg(state->ap, PRInt64 *) = llval;
- } else {
- long lval = strtol(buf, NULL, base);
- if (state->sizeSpec == _PR_size_none) {
- *va_arg(state->ap, PRIntn *) = lval;
- } else if (state->sizeSpec == _PR_size_h) {
- *va_arg(state->ap, PRInt16 *) = (PRInt16)lval;
- } else if (state->sizeSpec == _PR_size_l) {
- *va_arg(state->ap, PRInt32 *) = lval;
- } else {
- return PR_FAILURE;
- }
- }
- } else {
- if (state->sizeSpec == _PR_size_ll) {
- PRUint64 llval = _pr_strtoull(buf, NULL, base);
- *va_arg(state->ap, PRUint64 *) = llval;
- } else {
- unsigned long lval = strtoul(buf, NULL, base);
- if (state->sizeSpec == _PR_size_none) {
- *va_arg(state->ap, PRUintn *) = lval;
- } else if (state->sizeSpec == _PR_size_h) {
- *va_arg(state->ap, PRUint16 *) = (PRUint16)lval;
- } else if (state->sizeSpec == _PR_size_l) {
- *va_arg(state->ap, PRUint32 *) = lval;
- } else {
- return PR_FAILURE;
- }
- }
- }
- state->converted = PR_TRUE;
- }
- return PR_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement