Revert "Fix settings to honor numeric conversion errors"

This reverts commit 3f376a092e1c16429fb52f24736e9da98aff4cd5.
This commit is contained in:
kwolekr 2014-02-05 00:05:58 -05:00
parent 7859e57f6c
commit c6c5edd73a
8 changed files with 43 additions and 79 deletions

@ -102,15 +102,15 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
// if unspecified, leave a proc for the main thread and one for // if unspecified, leave a proc for the main thread and one for
// some other misc thread // some other misc thread
int nthreads = 0; int nthreads = 0;
if (!g_settings->getS16NoEx("num_emerge_threads", nthreads)) if (!g_settings->tryGetS16("num_emerge_threads", nthreads))
nthreads = porting::getNumberOfProcessors() - 2; nthreads = porting::getNumberOfProcessors() - 2;
if (nthreads < 1) if (nthreads < 1)
nthreads = 1; nthreads = 1;
qlimit_total = g_settings->getU16("emergequeue_limit_total"); qlimit_total = g_settings->getU16("emergequeue_limit_total");
if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", qlimit_diskonly)) if (!g_settings->tryGetU16("emergequeue_limit_diskonly", qlimit_diskonly))
qlimit_diskonly = nthreads * 5 + 1; qlimit_diskonly = nthreads * 5 + 1;
if (!g_settings->getU16NoEx("emergequeue_limit_generate", qlimit_generate)) if (!g_settings->tryGetU16("emergequeue_limit_generate", qlimit_generate))
qlimit_generate = nthreads + 1; qlimit_generate = nthreads + 1;
for (int i = 0; i != nthreads; i++) for (int i = 0; i != nthreads; i++)
@ -352,13 +352,13 @@ void EmergeManager::loadParamsFromSettings(Settings *settings) {
std::string seed_str; std::string seed_str;
const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed"; const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed";
if (settings->getNoEx(setname, seed_str)) if (settings->tryGet(setname, seed_str))
params.seed = read_seed(seed_str.c_str()); params.seed = read_seed(seed_str.c_str());
settings->getNoEx("mg_name", params.mg_name); settings->tryGet("mg_name", params.mg_name);
settings->getS16NoEx("water_level", params.water_level); settings->tryGetS16("water_level", params.water_level);
settings->getS16NoEx("chunksize", params.chunksize); settings->tryGetS16("chunksize", params.chunksize);
settings->getFlagStrNoEx("mg_flags", params.flags, flagdesc_mapgen); settings->tryGetFlagStr("mg_flags", params.flags, flagdesc_mapgen);
delete params.sparams; delete params.sparams;
params.sparams = createMapgenParams(params.mg_name); params.sparams = createMapgenParams(params.mg_name);

@ -85,11 +85,6 @@ public:
SettingNotFoundException(std::string s): BaseException(s) {} SettingNotFoundException(std::string s): BaseException(s) {}
}; };
class NumericException : public BaseException {
public:
NumericException(std::string s): BaseException(s) {}
};
class InvalidFilenameException : public BaseException { class InvalidFilenameException : public BaseException {
public: public:
InvalidFilenameException(std::string s): BaseException(s) {} InvalidFilenameException(std::string s): BaseException(s) {}

@ -1671,10 +1671,9 @@ int main(int argc, char *argv[])
//infostream<<"Main: password hash: '"<<password<<"'"<<std::endl; //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
address = menudata.address; address = menudata.address;
try { int newport = stoi(menudata.port);
port = stoi(menudata.port); if(newport != 0)
} port = newport;
catch (NumericException&e) {}
simple_singleplayer_mode = menudata.simple_singleplayer_mode; simple_singleplayer_mode = menudata.simple_singleplayer_mode;

@ -164,7 +164,7 @@ MapgenIndevParams::MapgenIndevParams() {
void MapgenIndevParams::readParams(Settings *settings) { void MapgenIndevParams::readParams(Settings *settings) {
MapgenV6Params::readParams(settings); MapgenV6Params::readParams(settings);
settings->getS16NoEx("mgindev_float_islands", float_islands); settings->tryGetS16("mgindev_float_islands", float_islands);
settings->getNoiseIndevParams("mgindev_np_terrain_base", npindev_terrain_base); settings->getNoiseIndevParams("mgindev_np_terrain_base", npindev_terrain_base);
settings->getNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher); settings->getNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);

@ -112,9 +112,9 @@ MapgenV6Params::MapgenV6Params() {
void MapgenV6Params::readParams(Settings *settings) { void MapgenV6Params::readParams(Settings *settings) {
settings->getFlagStrNoEx("mgv6_spflags", spflags, flagdesc_mapgen_v6); settings->tryGetFlagStr("mgv6_spflags", spflags, flagdesc_mapgen_v6);
settings->getFloatNoEx("mgv6_freq_desert", freq_desert); settings->tryGetFloat("mgv6_freq_desert", freq_desert);
settings->getFloatNoEx("mgv6_freq_beach", freq_beach); settings->tryGetFloat("mgv6_freq_beach", freq_beach);
settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base); settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base);
settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher); settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);

@ -126,7 +126,7 @@ MapgenV7Params::MapgenV7Params() {
void MapgenV7Params::readParams(Settings *settings) { void MapgenV7Params::readParams(Settings *settings) {
settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7); settings->tryGetFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7);
settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base); settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base);
settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt); settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);

@ -745,19 +745,17 @@ fail:
} }
//////////// Try to get value, no exception thrown //////////// Try to get value, no exception thrown
bool getNoEx(std::string name, std::string &val) bool tryGet(std::string name, std::string &val)
{ {
try { try {
val = get(name); val = get(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc) bool tryGetFlagStr(std::string name, u32 &val, FlagDesc *flagdesc)
{ {
try { try {
val = getFlagStr(name, flagdesc); val = getFlagStr(name, flagdesc);
@ -767,111 +765,93 @@ fail:
} }
} }
bool getFloatNoEx(std::string name, float &val) bool tryGetFloat(std::string name, float &val)
{ {
try { try {
val = getFloat(name); val = getFloat(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getU16NoEx(std::string name, int &val) bool tryGetU16(std::string name, int &val)
{ {
try { try {
val = getU16(name); val = getU16(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getU16NoEx(std::string name, u16 &val) bool tryGetU16(std::string name, u16 &val)
{ {
try { try {
val = getU16(name); val = getU16(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getS16NoEx(std::string name, int &val) bool tryGetS16(std::string name, int &val)
{ {
try { try {
val = getU16(name); val = getU16(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getS16NoEx(std::string name, s16 &val) bool tryGetS16(std::string name, s16 &val)
{ {
try { try {
val = getS16(name); val = getS16(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getS32NoEx(std::string name, s32 &val) bool tryGetS32(std::string name, s32 &val)
{ {
try { try {
val = getS32(name); val = getS32(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getV3FNoEx(std::string name, v3f &val) bool tryGetV3F(std::string name, v3f &val)
{ {
try { try {
val = getV3F(name); val = getV3F(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getV2FNoEx(std::string name, v2f &val) bool tryGetV2F(std::string name, v2f &val)
{ {
try { try {
val = getV2F(name); val = getV2F(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }
bool getU64NoEx(std::string name, u64 &val) bool tryGetU64(std::string name, u64 &val)
{ {
try { try {
val = getU64(name); val = getU64(name);
return true; return true;
} catch (SettingNotFoundException &e) { } catch (SettingNotFoundException &e) {
return false; return false;
} catch (NumericException &e) {
return false;
} }
} }

@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "exceptions.h"
struct FlagDesc { struct FlagDesc {
const char *name; const char *name;
@ -146,31 +145,17 @@ inline std::string trim(const std::string &s)
return s.substr(front, back - front); 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) inline bool is_yes(const std::string &s)
{ {
std::string s2 = lowercase(trim(s)); std::string s2 = lowercase(trim(s));
try { if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
if(s2 == "y" || s2 == "yes" || s2 == "true" || mystoi(s2) != 0)
return true; return true;
}
catch(NumericException&e) {}
return false; return false;
} }
inline s32 mystoi(const std::string &s, s32 min, s32 max) inline s32 mystoi(const std::string &s, s32 min, s32 max)
{ {
s32 i = mystoi(s); s32 i = atoi(s.c_str());
if(i < min) if(i < min)
i = min; i = min;
if(i > max) if(i > max)
@ -188,20 +173,25 @@ inline s64 stoi64(const std::string &s) {
// MSVC2010 includes it's own versions of these // MSVC2010 includes it's own versions of these
//#if !defined(_MSC_VER) || _MSC_VER < 1600 //#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) inline s32 mystoi(const std::wstring &s)
{ {
return mystoi(wide_to_narrow(s).c_str()); return atoi(wide_to_narrow(s).c_str());
} }
inline float mystof(const std::string &s) inline float mystof(const std::string &s)
{ {
char* endptr = NULL; // This crap causes a segfault in certain cases on MinGW
float retval = strtof(s.c_str(),&endptr); /*float f;
std::istringstream ss(s);
if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str())) ss>>f;
throw NumericException("string to convert"); return f;*/
// This works in that case
return retval; return atof(s.c_str());
} }
//#endif //#endif