Add defaultValue argument to Rules::getInteger

The defaultValue can be used for callers who do not want 0 to be
returned when conversion fails since strtoimax returns 0 when the
conversion fails.
This commit is contained in:
Jelle van der Waa 2016-09-05 22:06:00 +02:00 committed by Paul Beckingham
parent 6557814909
commit c33a3a9d9a
2 changed files with 9 additions and 5 deletions

View file

@ -149,13 +149,17 @@ std::string Rules::get (const std::string& key) const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Rules::getInteger (const std::string& key) const int Rules::getInteger (const std::string& key, int defaultValue) const
{ {
auto found = _settings.find (key); auto found = _settings.find (key);
if (found != _settings.end ()) if (found != _settings.end ()) {
return strtoimax (found->second.c_str (), nullptr, 10); int tmp = strtoimax (found->second.c_str (), nullptr, 10);
// If conversion has performed, return the conversion.
if (tmp != 0)
return tmp;
}
return 0; return defaultValue;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -41,7 +41,7 @@ public:
bool has (const std::string&) const; bool has (const std::string&) const;
std::string get (const std::string&) const; std::string get (const std::string&) const;
int getInteger (const std::string&) const; int getInteger (const std::string&, int defaultValue = 0) const;
double getReal (const std::string&) const; double getReal (const std::string&) const;
bool getBoolean (const std::string&) const; bool getBoolean (const std::string&) const;