Fix settings to honor numeric conversion errors
authorsapier <Sapier at GMX dot net>
Wed, 5 Feb 2014 00:35:40 +0000 (01:35 +0100)
committerkwolekr <kwolekr@minetest.net>
Wed, 5 Feb 2014 03:14:13 +0000 (22:14 -0500)
Rename try* non exceptioning functions to *NoEx

src/emerge.cpp
src/exceptions.h
src/main.cpp
src/mapgen_indev.cpp
src/mapgen_v6.cpp
src/mapgen_v7.cpp
src/settings.h
src/util/string.h

index 42e5337596f9b3dc20974e988e5df823df1e43d9..2c46b0e141cfb5164f608f3b0e9ae5fe9aee79cd 100644 (file)
@@ -102,15 +102,15 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
        // if unspecified, leave a proc for the main thread and one for
        // some other misc thread
        int nthreads = 0;
-       if (!g_settings->tryGetS16("num_emerge_threads", nthreads))
+       if (!g_settings->getS16NoEx("num_emerge_threads", nthreads))
                nthreads = porting::getNumberOfProcessors() - 2;
        if (nthreads < 1)
                nthreads = 1;
 
        qlimit_total = g_settings->getU16("emergequeue_limit_total");
-       if (!g_settings->tryGetU16("emergequeue_limit_diskonly", qlimit_diskonly))
+       if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", qlimit_diskonly))
                qlimit_diskonly = nthreads * 5 + 1;
-       if (!g_settings->tryGetU16("emergequeue_limit_generate", qlimit_generate))
+       if (!g_settings->getU16NoEx("emergequeue_limit_generate", qlimit_generate))
                qlimit_generate = nthreads + 1;
 
        for (int i = 0; i != nthreads; i++)
@@ -352,13 +352,13 @@ void EmergeManager::loadParamsFromSettings(Settings *settings) {
        std::string seed_str;
        const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed";
 
-       if (settings->tryGet(setname, seed_str))
+       if (settings->getNoEx(setname, seed_str))
                params.seed = read_seed(seed_str.c_str());
 
-       settings->tryGet("mg_name",         params.mg_name);
-       settings->tryGetS16("water_level",  params.water_level);
-       settings->tryGetS16("chunksize",    params.chunksize);
-       settings->tryGetFlagStr("mg_flags", params.flags, flagdesc_mapgen);
+       settings->getNoEx("mg_name",         params.mg_name);
+       settings->getS16NoEx("water_level",  params.water_level);
+       settings->getS16NoEx("chunksize",    params.chunksize);
+       settings->getFlagStrNoEx("mg_flags", params.flags, flagdesc_mapgen);
 
        delete params.sparams;
        params.sparams = createMapgenParams(params.mg_name);
index 6fb97f3ed3758a52db40d5905519b229b9339ede..703d6c1a542371aeb5b909474a0150a20b0e2aef 100644 (file)
@@ -85,6 +85,11 @@ public:
        SettingNotFoundException(std::string s): BaseException(s) {}
 };
 
+class NumericException : public BaseException {
+public:
+       NumericException(std::string s): BaseException(s) {}
+};
+
 class InvalidFilenameException : public BaseException {
 public:
        InvalidFilenameException(std::string s): BaseException(s) {}
index 8186e26c8a70fcc6b7fd548c1d585a7385566356..c90e6fde3983d708421f57f69b9b8b64e0fb2305 100644 (file)
@@ -1671,9 +1671,10 @@ int main(int argc, char *argv[])
                                //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
 
                                address = menudata.address;
-                               int newport = stoi(menudata.port);
-                               if(newport != 0)
-                                       port = newport;
+                               try {
+                                       port = stoi(menudata.port);
+                               }
+                               catch (NumericException&e) {}
 
                                simple_singleplayer_mode = menudata.simple_singleplayer_mode;
 
index 67535b0b464606a1d9d0182ba1351cac25f1423e..ac5b4e811599004bda56b3e592eee8fdc6ef67ae 100644 (file)
@@ -164,7 +164,7 @@ MapgenIndevParams::MapgenIndevParams() {
 void MapgenIndevParams::readParams(Settings *settings) {
        MapgenV6Params::readParams(settings);
 
-       settings->tryGetS16("mgindev_float_islands", float_islands);
+       settings->getS16NoEx("mgindev_float_islands", float_islands);
 
        settings->getNoiseIndevParams("mgindev_np_terrain_base",   npindev_terrain_base);
        settings->getNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);
index 526d4af2e52b4bd658130930ebb5a1ecf9271ca3..92ae7dff0cca7a8884a3a812746deb503202d606 100644 (file)
@@ -112,9 +112,9 @@ MapgenV6Params::MapgenV6Params() {
 
 
 void MapgenV6Params::readParams(Settings *settings) {
-       settings->tryGetFlagStr("mgv6_spflags", spflags, flagdesc_mapgen_v6);
-       settings->tryGetFloat("mgv6_freq_desert", freq_desert);
-       settings->tryGetFloat("mgv6_freq_beach",  freq_beach);
+       settings->getFlagStrNoEx("mgv6_spflags", spflags, flagdesc_mapgen_v6);
+       settings->getFloatNoEx("mgv6_freq_desert", freq_desert);
+       settings->getFloatNoEx("mgv6_freq_beach",  freq_beach);
 
        settings->getNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
        settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
index 77e25a6723a5ae41cf361bb4625fb1b0806c8ea8..8359151648e2a9332f5a288a88e939c8291950e3 100644 (file)
@@ -126,7 +126,7 @@ MapgenV7Params::MapgenV7Params() {
 
 
 void MapgenV7Params::readParams(Settings *settings) {
-       settings->tryGetFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7);
+       settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
 
        settings->getNoiseParams("mgv7_np_terrain_base",    np_terrain_base);
        settings->getNoiseParams("mgv7_np_terrain_alt",     np_terrain_alt);
index f019ce50c8e2c1ca93f0b233c22ef248fc721b2b..11b11e6358e057cec4ae421e9766a8f9c739308b 100644 (file)
@@ -745,17 +745,19 @@ fail:
        }
 
        //////////// Try to get value, no exception thrown
-       bool tryGet(std::string name, std::string &val)
+       bool getNoEx(std::string name, std::string &val)
        {
                try {
                        val = get(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetFlagStr(std::string name, u32 &val, FlagDesc *flagdesc)
+       bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc)
        {
                try {
                        val = getFlagStr(name, flagdesc);
@@ -765,93 +767,111 @@ fail:
                }
        }
 
-       bool tryGetFloat(std::string name, float &val)
+       bool getFloatNoEx(std::string name, float &val)
        {
                try {
                        val = getFloat(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetU16(std::string name, int &val)
+       bool getU16NoEx(std::string name, int &val)
        {
                try {
                        val = getU16(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetU16(std::string name, u16 &val)
+       bool getU16NoEx(std::string name, u16 &val)
        {
                try {
                        val = getU16(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetS16(std::string name, int &val)
+       bool getS16NoEx(std::string name, int &val)
        {
                try {
                        val = getU16(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetS16(std::string name, s16 &val)
+       bool getS16NoEx(std::string name, s16 &val)
        {
                try {
                        val = getS16(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetS32(std::string name, s32 &val)
+       bool getS32NoEx(std::string name, s32 &val)
        {
                try {
                        val = getS32(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetV3F(std::string name, v3f &val)
+       bool getV3FNoEx(std::string name, v3f &val)
        {
                try {
                        val = getV3F(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetV2F(std::string name, v2f &val)
+       bool getV2FNoEx(std::string name, v2f &val)
        {
                try {
                        val = getV2F(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
-       bool tryGetU64(std::string name, u64 &val)
+       bool getU64NoEx(std::string name, u64 &val)
        {
                try {
                        val = getU64(name);
                        return true;
                } catch (SettingNotFoundException &e) {
                        return false;
+               } catch (NumericException &e) {
+                       return false;
                }
        }
 
index e5a60bc47162824770b8e2af78270fa5d4cba609..a3f84c0eab27ff40ad45880bc23e1f398687e560 100644 (file)
@@ -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