From 0d0f0182b3ebb3b4c6afc480d34a34f392a29bc7 Mon Sep 17 00:00:00 2001 From: TheVice Date: Sat, 10 Sep 2016 18:50:29 +0300 Subject: [PATCH] [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. --- cpp/INIReader.cpp | 12 ++++++------ cpp/INIReader.h | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cpp/INIReader.cpp b/cpp/INIReader.cpp index cc3a9b6..f836f52 100644 --- a/cpp/INIReader.cpp +++ b/cpp/INIReader.cpp @@ -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 diff --git a/cpp/INIReader.h b/cpp/INIReader.h index f9d291c..5ac3d30 100644 --- a/cpp/INIReader.h +++ b/cpp/INIReader.h @@ -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 _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); };