diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 5a912cf4..9ced0464 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -269,8 +269,27 @@ bool str2int(const string& str, int& result) bool str2int(const string& str, double& result) { - char* s = (char*)str.c_str(); - return atof(s); + try + { + size_t pos; + result = std::stod(str, &pos); + + /* check whether the whole stirng was converted */ + if (pos != str.length()) + return false; + + return true; + } + catch (const std::invalid_argument) + { + return false; + } + catch (const std::out_of_range) + { + return false; + } + + return false; } bool str2int(char*& str, int& result, char sep) diff --git a/core/AmUtils.h b/core/AmUtils.h index 8267453d..fa6ceafa 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -143,6 +143,12 @@ bool str2int(char*& str, unsigned int& result, char sep = ' '); */ bool str2int(const string& str, int& result); +/** + * Convert a string to the double. + * @param str [in] string to convert. + * @param result [out] result integer. + * @return true on success + */ bool str2int(const string& str, double& result); /**