Skip to content

Commit

Permalink
Merge pull request #53 from TheVice/INIReader
Browse files Browse the repository at this point in the history
[INIReader] class now using constant reference as method arguments.
  • Loading branch information
benhoyt authored Sep 12, 2016
2 parents 5dbf5cb + c4c1f31 commit 421bdb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions cpp/INIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

using std::string;

INIReader::INIReader(string filename)
INIReader::INIReader(const string& filename)
{
_error = ini_parse(filename.c_str(), ValueHandler, this);
}
Expand All @@ -23,14 +23,14 @@ int INIReader::ParseError() const
return _error;
}

string INIReader::Get(string section, string name, string default_value) const
string INIReader::Get(const string& section, const string& name, const string& default_value) const
{
string key = MakeKey(section, name);
// Use _values.find() here instead of _values.at() to support pre C++11 compilers
return _values.count(key) ? _values.find(key)->second : default_value;
}

long INIReader::GetInteger(string section, string name, long default_value) const
long INIReader::GetInteger(const string& section, const string& name, long default_value) const
{
string valstr = Get(section, name, "");
const char* value = valstr.c_str();
Expand All @@ -40,7 +40,7 @@ long INIReader::GetInteger(string section, string name, long default_value) cons
return end > value ? n : default_value;
}

double INIReader::GetReal(string section, string name, double default_value) const
double INIReader::GetReal(const string& section, const string& name, double default_value) const
{
string valstr = Get(section, name, "");
const char* value = valstr.c_str();
Expand All @@ -49,7 +49,7 @@ double INIReader::GetReal(string section, string name, double default_value) con
return end > value ? n : default_value;
}

bool INIReader::GetBoolean(string section, string name, bool default_value) const
bool INIReader::GetBoolean(const string& section, const string& name, bool default_value) const
{
string valstr = Get(section, name, "");
// Convert to lower case to make string comparisons case-insensitive
Expand All @@ -62,7 +62,7 @@ bool INIReader::GetBoolean(string section, string name, bool default_value) cons
return default_value;
}

string INIReader::MakeKey(string section, string name)
string INIReader::MakeKey(const string& section, const string& name)
{
string key = section + "=" + name;
// Convert to lower case to make section/name lookups case-insensitive
Expand Down
14 changes: 7 additions & 7 deletions cpp/INIReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ class INIReader
public:
// Construct INIReader and parse given filename. See ini.h for more info
// about the parsing.
INIReader(std::string filename);
INIReader(const std::string& filename);

// Return the result of ini_parse(), i.e., 0 on success, line number of
// first error on parse error, or -1 on file open error.
int ParseError() const;

// Get a string value from INI file, returning default_value if not found.
std::string Get(std::string section, std::string name,
std::string default_value) const;
std::string Get(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").
long GetInteger(std::string section, std::string name, long default_value) const;
long GetInteger(const std::string& section, const std::string& name, long default_value) const;

// Get a real (floating point double) value from INI file, returning
// default_value if not found or not a valid floating point value
// according to strtod().
double GetReal(std::string section, std::string name, double default_value) const;
double GetReal(const std::string& section, const std::string& name, double default_value) const;

// Get a boolean value from INI file, returning default_value if not found or if
// not a valid true/false value. Valid true values are "true", "yes", "on", "1",
// and valid false values are "false", "no", "off", "0" (not case sensitive).
bool GetBoolean(std::string section, std::string name, bool default_value) const;
bool GetBoolean(const std::string& section, const std::string& name, bool default_value) const;

private:
int _error;
std::map<std::string, std::string> _values;
static std::string MakeKey(std::string section, std::string name);
static std::string MakeKey(const std::string& section, const std::string& name);
static int ValueHandler(void* user, const char* section, const char* name,
const char* value);
};
Expand Down

0 comments on commit 421bdb2

Please sign in to comment.