bool Parser::floating(std::string::iterator& iter, double& val) { auto localIter = iter; double localValue = 0; // A single digit int dgt = 0; // Number of digits int numDgt = 0; if(isDecimalPoint(localIter)) return false; while (digit(localIter)) { dgt = (*localIter) - '0'; numDgt++; localValue = localValue * 10 + dgt; localIter++; // If there is a decimal point then return false if (isDecimalPoint(localIter)) return false; } // Push the digits next to the decimal point while ((int)localValue % 10) localValue /= 10; iter = localIter; val = localValue; return true; }
/*--------------------------------------------------------------------------- | Facility : libnform | Function : static bool Check_This_Character( | int c, | const void * argp) | | Description : Check a character for the numeric type. | | Return Values : TRUE - character is valid | FALSE - character is invalid +--------------------------------------------------------------------------*/ static bool Check_This_Character(int c, const void *argp) { const thisARG *argn = (const thisARG *)argp; struct lconv *L = argn->L; return ((isDigit(c) || c == '+' || c == '-' || isDecimalPoint(c)) ? TRUE : FALSE); }
bool Parser::number(std::string::iterator &iter, double& val) { auto localIter = iter; double localValue = 0; // A single digit int dgt = 0; // If next char is a digit or it was a decimal point previously then try to list all digits/decimal points if (digit(localIter) || isDecimalPoint(localIter)) { double floatVal = 0; // While finding digits or decimal points while (digit(localIter) || isDecimalPoint(localIter)) { if (character(localIter, '.')) { if (!floating(localIter, floatVal)) throw std::exception("Syntax error"); } else { // Convert every digit to number and then add to the value dgt = (*localIter) - '0'; localValue = localValue * 10 + dgt; localIter++; } } // If found all the digits set factor's value and iter to the local localValue = localValue + floatVal; iter = localIter; val = localValue; return true; } return false; }
/*--------------------------------------------------------------------------- | Facility : libnform | Function : static bool Check_This_Field(FIELD * field, | const void * argp) | | Description : Validate buffer content to be a valid numeric value | | Return Values : TRUE - field is valid | FALSE - field is invalid +--------------------------------------------------------------------------*/ static bool Check_This_Field(FIELD *field, const void *argp) { const thisARG *argn = (const thisARG *)argp; double low = argn->low; double high = argn->high; int prec = argn->precision; unsigned char *bp = (unsigned char *)field_buffer(field, 0); char *s = (char *)bp; double val = 0.0; struct lconv *L = argn->L; char buf[64]; bool result = FALSE; while (*bp && *bp == ' ') bp++; if (*bp) { if (*bp == '-' || *bp == '+') bp++; #if USE_WIDEC_SUPPORT if (*bp) { bool blank = FALSE; int state = 0; int len; int n; wchar_t *list = _nc_Widen_String((char *)bp, &len); if (list != 0) { result = TRUE; for (n = 0; n < len; ++n) { if (blank) { if (list[n] != ' ') { result = FALSE; break; } } else if (list[n] == ' ') { blank = TRUE; } else if (isDecimalPoint(list[n])) { if (++state > 1) { result = FALSE; break; } } else if (!isDigit(list[n])) { result = FALSE; break; } } free(list); } } #else while (*bp) { if (!isdigit(UChar(*bp))) break; bp++; } if (isDecimalPoint(*bp)) { bp++; while (*bp) { if (!isdigit(UChar(*bp))) break; bp++; } } while (*bp && *bp == ' ') bp++; result = (*bp == '\0'); #endif if (result) { val = atof(s); if (low < high) { if (val < low || val > high) result = FALSE; } if (result) { sprintf(buf, "%.*f", (prec > 0 ? prec : 0), val); set_field_buffer(field, 0, buf); } } } return (result); }