Advertisement
Guest User

Untitled

a guest
Dec 8th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. static PRStatus
  2. GetInt(ScanfState *state, int code)
  3. {
  4.     char buf[FMAX + 1], *p;
  5.     int ch;
  6.     static const char digits[] = "0123456789abcdefABCDEF";
  7.     PRBool seenDigit = PR_FALSE;
  8.     int base;
  9.     int dlen;
  10.  
  11.     switch (code) {
  12.         case 'd': case 'u':
  13.             base = 10;
  14.             break;
  15.         case 'i':
  16.             base = 0;
  17.             break;
  18.         case 'x': case 'X': case 'p':
  19.             base = 16;
  20.             break;
  21.         case 'o':
  22.             base = 8;
  23.             break;
  24.         default:
  25.             return PR_FAILURE;
  26.     }
  27.     if (state->width == 0 || state->width > FMAX) {
  28.         state->width = FMAX;
  29.     }
  30.     p = buf;
  31.     GET_IF_WITHIN_WIDTH(state, ch);
  32.     if (WITHIN_WIDTH(state) && (ch == '+' || ch == '-')) {
  33.         *p++ = ch;
  34.         GET_IF_WITHIN_WIDTH(state, ch);
  35.     }
  36.     if (WITHIN_WIDTH(state) && ch == '0') {
  37.         seenDigit = PR_TRUE;
  38.         *p++ = ch;
  39.         GET_IF_WITHIN_WIDTH(state, ch);
  40.         if (WITHIN_WIDTH(state)
  41.                 && (ch == 'x' || ch == 'X')
  42.                 && (base == 0 || base == 16)) {
  43.             base = 16;
  44.             *p++ = ch;
  45.             GET_IF_WITHIN_WIDTH(state, ch);
  46.         } else if (base == 0) {
  47.             base = 8;
  48.         }
  49.     }
  50.     if (base == 0 || base == 10) {
  51.         dlen = 10;
  52.     } else if (base == 8) {
  53.         dlen = 8;
  54.     } else {
  55.         PR_ASSERT(base == 16);
  56.         dlen = 16 + 6; /* 16 digits, plus 6 in uppercase */
  57.     }
  58.     while (WITHIN_WIDTH(state) && memchr(digits, ch, dlen)) {
  59.         *p++ = ch;
  60.         GET_IF_WITHIN_WIDTH(state, ch);
  61.         seenDigit = PR_TRUE;
  62.     }
  63.     if (WITHIN_WIDTH(state)) {
  64.         UNGET(state, ch);
  65.     }
  66.     if (!seenDigit) {
  67.         return PR_FAILURE;
  68.     }
  69.     *p = '\0';
  70.     if (state->assign) {
  71.         if (code == 'd' || code == 'i') {
  72.             if (state->sizeSpec == _PR_size_ll) {
  73.                 PRInt64 llval = _pr_strtoull(buf, NULL, base);
  74.                 *va_arg(state->ap, PRInt64 *) = llval;
  75.             } else {
  76.                 long lval = strtol(buf, NULL, base);
  77.  
  78.                 if (state->sizeSpec == _PR_size_none) {
  79.                     *va_arg(state->ap, PRIntn *) = lval;
  80.                 } else if (state->sizeSpec == _PR_size_h) {
  81.                     *va_arg(state->ap, PRInt16 *) = (PRInt16)lval;
  82.                 } else if (state->sizeSpec == _PR_size_l) {
  83.                     *va_arg(state->ap, PRInt32 *) = lval;
  84.                 } else {
  85.                     return PR_FAILURE;
  86.                 }
  87.             }
  88.         } else {
  89.             if (state->sizeSpec == _PR_size_ll) {
  90.                 PRUint64 llval = _pr_strtoull(buf, NULL, base);
  91.                 *va_arg(state->ap, PRUint64 *) = llval;
  92.             } else {
  93.                 unsigned long lval = strtoul(buf, NULL, base);
  94.  
  95.                 if (state->sizeSpec == _PR_size_none) {
  96.                     *va_arg(state->ap, PRUintn *) = lval;
  97.                 } else if (state->sizeSpec == _PR_size_h) {
  98.                     *va_arg(state->ap, PRUint16 *) = (PRUint16)lval;
  99.                 } else if (state->sizeSpec == _PR_size_l) {
  100.                     *va_arg(state->ap, PRUint32 *) = lval;
  101.                 } else {
  102.                     return PR_FAILURE;
  103.                 }
  104.             }
  105.         }
  106.         state->converted = PR_TRUE;
  107.     }
  108.     return PR_SUCCESS;
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement