int GetIntParam( char const *input, uint8_t len, uint8_t decimal, uint8_t acceptneg ) { int retval = 0; char converted; uint8_t found = 0; uint8_t negative = 0; char const *tmp = input; while( tmp - input < len ) { converted = ConvertDigit(*tmp, decimal); if(converted == (char)-1) { if(!retval) { if(*tmp == 'x' || *tmp == 'X') { decimal = 0; converted = 0; } else if(*tmp == '-') { negative = 1; converted = 0; } else break; } else break; } retval *= decimal ? 10 : 16; retval += converted; found = 1; ++tmp; } return found ? (negative ? 0 - retval : retval ) : (acceptneg ? INT_MIN : -1); }
// Reads an integer. bool SymFile::ReadInteger(unsigned long& n) { SkipWhitespace(); if (!IsAsciiDigit(m_buffer[m_pos])) return false; int startPos = m_pos; int radix = 10; if (m_buffer[m_pos] == '0' && m_buffer[m_pos + 1] == 'x') { radix = 16; m_pos += 2; } unsigned long cutoff = ULONG_MAX / radix; unsigned long cutoffRemainder = ULONG_MAX % radix; int digit; n = 0; while ((digit = ConvertDigit(m_buffer[m_pos], radix)) != -1) { if (n < cutoff || (n == cutoff && (unsigned long)digit <= cutoffRemainder)) { n = n * radix + digit; } else { m_pos++; while (ConvertDigit(m_buffer[m_pos], radix) != -1) m_pos++; RaiseError("integer is too large (%s)", std::string(&m_buffer[startPos], m_pos - startPos).c_str()); } m_pos++; } return true; }