Merge pull request #7653 from jordan-woyak/ini-cleanup

IniFile: Minor cleanup. Removed unused function. Improved template usage.
This commit is contained in:
Mat M 2018-12-28 06:21:24 -05:00 committed by GitHub
commit 2749c50843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 55 deletions

View File

@ -45,23 +45,18 @@ IniFile::Section::Section(std::string name_) : name{std::move(name_)}
{ {
} }
void IniFile::Section::Set(const std::string& key, const std::string& newValue) void IniFile::Section::Set(const std::string& key, std::string new_value)
{ {
auto it = values.find(key); auto it = values.find(key);
if (it != values.end()) if (it != values.end())
it->second = newValue; it->second = std::move(new_value);
else else
{ {
values[key] = newValue; values[key] = std::move(new_value);
keys_order.push_back(key); keys_order.push_back(key);
} }
} }
void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues)
{
Set(key, JoinStrings(newValues, ","));
}
bool IniFile::Section::Get(const std::string& key, std::string* value, bool IniFile::Section::Get(const std::string& key, std::string* value,
const std::string& defaultValue) const const std::string& defaultValue) const
{ {
@ -80,36 +75,6 @@ bool IniFile::Section::Get(const std::string& key, std::string* value,
return false; return false;
} }
bool IniFile::Section::Get(const std::string& key, std::vector<std::string>* out) const
{
std::string temp;
bool retval = Get(key, &temp);
if (!retval || temp.empty())
{
return false;
}
// ignore starting comma, if any
size_t subStart = temp.find_first_not_of(",");
// split by comma
while (subStart != std::string::npos)
{
// Find next comma
size_t subEnd = temp.find(',', subStart);
if (subStart != subEnd)
{
// take from first char until next comma
out->push_back(StripSpaces(temp.substr(subStart, subEnd - subStart)));
}
// Find the next non-comma char
subStart = temp.find_first_not_of(",", subEnd);
}
return true;
}
bool IniFile::Section::Exists(const std::string& key) const bool IniFile::Section::Exists(const std::string& key) const
{ {
return values.find(key) != values.end(); return values.find(key) != values.end();
@ -126,12 +91,7 @@ bool IniFile::Section::Delete(const std::string& key)
return true; return true;
} }
void IniFile::Section::SetLines(const std::vector<std::string>& lines) void IniFile::Section::SetLines(std::vector<std::string> lines)
{
m_lines = lines;
}
void IniFile::Section::SetLines(std::vector<std::string>&& lines)
{ {
m_lines = std::move(lines); m_lines = std::move(lines);
} }

View File

@ -35,26 +35,26 @@ public:
bool Exists(const std::string& key) const; bool Exists(const std::string& key) const;
bool Delete(const std::string& key); bool Delete(const std::string& key);
void Set(const std::string& key, const std::string& newValue); void Set(const std::string& key, std::string new_value);
template <typename T> template <typename T>
void Set(const std::string& key, const T& new_value) void Set(const std::string& key, T&& new_value)
{ {
Set(key, ValueToString(new_value)); Set(key, ValueToString(std::forward<T>(new_value)));
} }
template <typename T> template <typename T>
void Set(const std::string& key, const T& new_value, const std::common_type_t<T>& default_value) void Set(const std::string& key, T&& new_value, const std::common_type_t<T>& default_value)
{ {
if (new_value != default_value) if (new_value != default_value)
Set(key, new_value); Set(key, std::forward<T>(new_value));
else else
Delete(key); Delete(key);
} }
void Set(const std::string& key, const std::vector<std::string>& newValues);
bool Get(const std::string& key, std::string* value, bool Get(const std::string& key, std::string* value,
const std::string& defaultValue = NULL_STRING) const; const std::string& default_value = NULL_STRING) const;
template <typename T> template <typename T>
bool Get(const std::string& key, T* value, bool Get(const std::string& key, T* value,
const std::common_type_t<T>& default_value = {}) const const std::common_type_t<T>& default_value = {}) const
@ -66,10 +66,8 @@ public:
*value = default_value; *value = default_value;
return false; return false;
} }
bool Get(const std::string& key, std::vector<std::string>* values) const;
void SetLines(const std::vector<std::string>& lines); void SetLines(std::vector<std::string> lines);
void SetLines(std::vector<std::string>&& lines);
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const; bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
bool operator<(const Section& other) const { return name < other.name; } bool operator<(const Section& other) const { return name < other.name; }