Skip to content

Commit

Permalink
Add INIReader::GetString to get nicer default_value behavior (#72)
Browse files Browse the repository at this point in the history
* add INIReader::GetString

Same as INIReader::Get, but returns default_value if the value is an empty string, which provides functionality similar to GetInteger and the others

* clarify GetString description comment
  • Loading branch information
NeatNit authored and benhoyt committed Oct 12, 2018
1 parent 0ee2bf2 commit 2023872
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cpp/INIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ string INIReader::Get(const string& section, const string& name, const string& d
return _values.count(key) ? _values.find(key)->second : default_value;
}

string INIReader::GetString(const string& section, const string& name, const string& default_value) const
{
const string str = Get(section, name, "");
return str.empty() ? default_value : str;
}

long INIReader::GetInteger(const string& section, const string& name, long default_value) const
{
string valstr = Get(section, name, "");
Expand Down
5 changes: 5 additions & 0 deletions cpp/INIReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class INIReader
// Get a string value from INI file, returning default_value if not found.
std::string Get(const std::string& section, const std::string& name,
const std::string& default_value) const;

// Get a string value from INI file, returning default_value if not found,
// empty, or contains only whitespace.
std::string GetString(const std::string& section, const std::string& name,
const std::string& default_value) const;

// Get an integer (long) value from INI file, returning default_value if
// not found or not a valid integer (decimal "1234", "-1234", or hex "0x4d2").
Expand Down

0 comments on commit 2023872

Please sign in to comment.