mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-06-26 10:54:28 +02:00
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:
parent
6557814909
commit
c33a3a9d9a
2 changed files with 9 additions and 5 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue