Merge pull request #5135 from lioncash/ini

IniFile: Minor changes
This commit is contained in:
Anthony 2017-03-24 19:33:09 -07:00 committed by GitHub
commit b099a1c02a
2 changed files with 32 additions and 21 deletions

View File

@ -23,7 +23,7 @@ void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::strin
if (line[0] == '#') if (line[0] == '#')
return; return;
size_t firstEquals = line.find("=", 0); size_t firstEquals = line.find('=');
if (firstEquals != std::string::npos) if (firstEquals != std::string::npos)
{ {
@ -39,6 +39,12 @@ void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::strin
const std::string& IniFile::NULL_STRING = ""; const std::string& IniFile::NULL_STRING = "";
IniFile::Section::Section() = default;
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, const std::string& newValue)
{ {
auto it = values.find(key); auto it = values.find(key);
@ -62,15 +68,7 @@ void IniFile::Section::Set(const std::string& key, const std::string& newValue,
void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues) void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues)
{ {
std::string temp; Set(key, JoinStrings(newValues, ","));
// Join the strings with ,
for (const std::string& value : newValues)
{
temp = value + ",";
}
// remove last ,
temp.resize(temp.length() - 1);
Set(key, temp);
} }
void IniFile::Section::Set(const std::string& key, u32 newValue) void IniFile::Section::Set(const std::string& key, u32 newValue)
@ -261,15 +259,20 @@ void IniFile::Section::SetLines(const std::vector<std::string>& lines)
m_lines = lines; m_lines = lines;
} }
void IniFile::Section::SetLines(std::vector<std::string>&& lines)
{
m_lines = std::move(lines);
}
bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const
{ {
for (std::string line : m_lines) for (const std::string& line : m_lines)
{ {
line = StripSpaces(line); std::string stripped_line = StripSpaces(line);
if (remove_comments) if (remove_comments)
{ {
size_t commentPos = line.find('#'); size_t commentPos = stripped_line.find('#');
if (commentPos == 0) if (commentPos == 0)
{ {
continue; continue;
@ -277,11 +280,11 @@ bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remo
if (commentPos != std::string::npos) if (commentPos != std::string::npos)
{ {
line = StripSpaces(line.substr(0, commentPos)); stripped_line = StripSpaces(stripped_line.substr(0, commentPos));
} }
} }
lines->push_back(line); lines->push_back(std::move(stripped_line));
} }
return true; return true;
@ -346,6 +349,12 @@ void IniFile::SetLines(const std::string& sectionName, const std::vector<std::st
section->SetLines(lines); section->SetLines(lines);
} }
void IniFile::SetLines(const std::string& section_name, std::vector<std::string>&& lines)
{
Section* section = GetOrCreateSection(section_name);
section->SetLines(std::move(lines));
}
bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key) bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key)
{ {
Section* section = GetSection(sectionName); Section* section = GetSection(sectionName);
@ -418,9 +427,9 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
#ifndef _WIN32 #ifndef _WIN32
// Check for CRLF eol and convert it to LF // Check for CRLF eol and convert it to LF
if (!line.empty() && line.at(line.size() - 1) == '\r') if (!line.empty() && line.back() == '\r')
{ {
line.erase(line.size() - 1); line.pop_back();
} }
#endif #endif
@ -428,7 +437,7 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
{ {
if (line[0] == '[') if (line[0] == '[')
{ {
size_t endpos = line.find("]"); size_t endpos = line.find(']');
if (endpos != std::string::npos) if (endpos != std::string::npos)
{ {
@ -475,7 +484,7 @@ bool IniFile::Save(const std::string& filename)
for (const Section& section : sections) for (const Section& section : sections)
{ {
if (section.keys_order.size() != 0 || section.m_lines.size() != 0) if (section.keys_order.size() != 0 || section.m_lines.size() != 0)
out << "[" << section.name << "]" << std::endl; out << '[' << section.name << ']' << std::endl;
if (section.keys_order.size() == 0) if (section.keys_order.size() == 0)
{ {

View File

@ -29,8 +29,8 @@ public:
friend class IniFile; friend class IniFile;
public: public:
Section() {} Section();
Section(const std::string& _name) : name(_name) {} explicit Section(std::string name_);
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);
@ -67,6 +67,7 @@ public:
bool Get(const std::string& key, std::vector<std::string>* values) const; bool Get(const std::string& key, std::vector<std::string>* values) const;
void SetLines(const std::vector<std::string>& lines); void SetLines(const 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; }
@ -124,6 +125,7 @@ public:
bool GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const; bool GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const;
void SetLines(const std::string& sectionName, const std::vector<std::string>& lines); void SetLines(const std::string& sectionName, const std::vector<std::string>& lines);
void SetLines(const std::string& section_name, std::vector<std::string>&& lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>* lines, bool GetLines(const std::string& sectionName, std::vector<std::string>* lines,
const bool remove_comments = true) const; const bool remove_comments = true) const;