util: add parseint32 function with strict error reporting

None of the current integer parsing functions in util
check whether the result is valid and fits in the range
of the type. This is required for less sloppy error reporting.
This commit is contained in:
Wladimir J. van der Laan
2014-05-03 10:20:58 +02:00
parent e443ed2462
commit 0d4ea1cf8a
3 changed files with 43 additions and 0 deletions

View File

@@ -256,6 +256,13 @@ inline int atoi(const std::string& str)
return atoi(str.c_str());
}
/**
* Convert string to signed 32-bit integer with strict parse error feedback.
* @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occured.
*/
bool ParseInt32(const std::string& str, int32_t *out);
inline int roundint(double d)
{
return (int)(d > 0 ? d + 0.5 : d - 0.5);