1
0
mirror of https://github.com/benhoyt/inih.git synced 2025-01-28 22:52:54 +08:00

[INIReader] class now using constant reference as method arguments instead of using direct type for non primitive type string.

Return of Get method can be safe switch to constant reference.
This commit is contained in:
TheVice 2016-09-10 18:50:29 +03:00
parent 5dbf5cb6b4
commit 0d0f0182b3
2 changed files with 13 additions and 13 deletions

View File

@ -13,7 +13,7 @@
using std::string;
INIReader::INIReader(string filename)
INIReader::INIReader(const string& filename)
{
_error = ini_parse(filename.c_str(), ValueHandler, this);
}
@ -23,14 +23,14 @@ int INIReader::ParseError() const
return _error;
}
string INIReader::Get(string section, string name, string default_value) const
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();
@ -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();
@ -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
@ -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

View File

@ -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;
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);
};