summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2014-02-05 01:35:40 +0100
committerkwolekr <kwolekr@minetest.net>2014-02-04 22:14:13 -0500
commit3f376a092e1c16429fb52f24736e9da98aff4cd5 (patch)
tree7055808f86884593ef2f63316d61533968112956 /src/util
parent2927a327cabe4feddc0aeea0f4111d888560612f (diff)
downloadminetest-3f376a092e1c16429fb52f24736e9da98aff4cd5.tar.gz
minetest-3f376a092e1c16429fb52f24736e9da98aff4cd5.tar.bz2
minetest-3f376a092e1c16429fb52f24736e9da98aff4cd5.zip
Fix settings to honor numeric conversion errors
Rename try* non exceptioning functions to *NoEx
Diffstat (limited to 'src/util')
-rw-r--r--src/util/string.h42
1 files changed, 26 insertions, 16 deletions
diff --git a/src/util/string.h b/src/util/string.h
index e5a60bc47..a3f84c0ea 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cstring>
#include <vector>
#include <sstream>
+#include "exceptions.h"
struct FlagDesc {
const char *name;
@@ -145,17 +146,31 @@ inline std::string trim(const std::string &s)
return s.substr(front, back - front);
}
+inline s32 mystoi(const std::string &s)
+{
+ char* endptr = NULL;
+ s32 retval = strtol(s.c_str(),&endptr,10);
+
+ if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
+ throw NumericException("string to convert");
+
+ return retval;
+}
+
inline bool is_yes(const std::string &s)
{
std::string s2 = lowercase(trim(s));
- if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
- return true;
+ try {
+ if(s2 == "y" || s2 == "yes" || s2 == "true" || mystoi(s2) != 0)
+ return true;
+ }
+ catch(NumericException&e) {}
return false;
}
inline s32 mystoi(const std::string &s, s32 min, s32 max)
{
- s32 i = atoi(s.c_str());
+ s32 i = mystoi(s);
if(i < min)
i = min;
if(i > max)
@@ -173,25 +188,20 @@ inline s64 stoi64(const std::string &s) {
// MSVC2010 includes it's own versions of these
//#if !defined(_MSC_VER) || _MSC_VER < 1600
-inline s32 mystoi(const std::string &s)
-{
- return atoi(s.c_str());
-}
-
inline s32 mystoi(const std::wstring &s)
{
- return atoi(wide_to_narrow(s).c_str());
+ return mystoi(wide_to_narrow(s).c_str());
}
inline float mystof(const std::string &s)
{
- // This crap causes a segfault in certain cases on MinGW
- /*float f;
- std::istringstream ss(s);
- ss>>f;
- return f;*/
- // This works in that case
- return atof(s.c_str());
+ char* endptr = NULL;
+ float retval = strtof(s.c_str(),&endptr);
+
+ if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
+ throw NumericException("string to convert");
+
+ return retval;
}
//#endif