Reduce duplicated code in IniReader

This commit is contained in:
Ted John 2017-02-22 17:58:50 +00:00
parent d72ced4ea9
commit 40eb2c55fd
1 changed files with 19 additions and 23 deletions

View File

@ -93,49 +93,45 @@ public:
bool GetBoolean(const std::string &name, bool defaultValue) const override bool GetBoolean(const std::string &name, bool defaultValue) const override
{ {
auto it = _values.find(name); bool result = defaultValue;
if (it == _values.end()) std::string value;
if (TryGetString(name, &value))
{ {
return defaultValue; result = String::Equals(value, "true", true);
} }
return result;
std::string value = it->second;
return String::Equals(value, "true", true);
} }
sint32 GetSint32(const std::string &name, sint32 defaultValue) const override sint32 GetSint32(const std::string &name, sint32 defaultValue) const override
{ {
auto it = _values.find(name); sint32 result = defaultValue;
if (it == _values.end()) std::string value;
if (TryGetString(name, &value))
{ {
return defaultValue; result = std::stoi(value);
} }
return result;
std::string value = it->second;
return std::stoi(value);
} }
float GetFloat(const std::string &name, float defaultValue) const override float GetFloat(const std::string &name, float defaultValue) const override
{ {
auto it = _values.find(name); float result = defaultValue;
if (it == _values.end()) std::string value;
if (TryGetString(name, &value))
{ {
return defaultValue; result = std::stof(value);
} }
return result;
std::string value = it->second;
return std::stof(value);
} }
std::string GetString(const std::string &name, const std::string &defaultValue) const override std::string GetString(const std::string &name, const std::string &defaultValue) const override
{ {
auto it = _values.find(name); std::string result;
if (it == _values.end()) if (TryGetString(name, &result))
{ {
return defaultValue; result = defaultValue;
} }
return result;
return it->second;
} }
bool TryGetString(const std::string &name, std::string * outValue) const override bool TryGetString(const std::string &name, std::string * outValue) const override