Move IniFile section chunk handling to IniFile::Section

This commit is contained in:
Ryan Houdek 2016-02-10 12:19:15 -06:00 committed by Léo Lam
parent b7a1c6f504
commit ead8be9d19
2 changed files with 41 additions and 27 deletions

View File

@ -256,6 +256,37 @@ bool IniFile::Section::Delete(const std::string& key)
return true; return true;
} }
void IniFile::Section::SetLines(const std::vector<std::string>& lines)
{
m_lines = lines;
}
bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const
{
for (std::string line : m_lines)
{
line = StripSpaces(line);
if (remove_comments)
{
size_t commentPos = line.find('#');
if (commentPos == 0)
{
continue;
}
if (commentPos != std::string::npos)
{
line = StripSpaces(line.substr(0, commentPos));
}
}
lines->push_back(line);
}
return true;
}
// IniFile // IniFile
const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const
@ -312,7 +343,7 @@ bool IniFile::Exists(const std::string& sectionName, const std::string& key) con
void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string>& lines) void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string>& lines)
{ {
Section* section = GetOrCreateSection(sectionName); Section* section = GetOrCreateSection(sectionName);
section->lines = lines; section->SetLines(lines);
} }
bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key) bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key)
@ -345,28 +376,7 @@ bool IniFile::GetLines(const std::string& sectionName, std::vector<std::string>*
if (!section) if (!section)
return false; return false;
for (std::string line : section->lines) return section->GetLines(lines, remove_comments);
{
line = StripSpaces(line);
if (remove_comments)
{
size_t commentPos = line.find('#');
if (commentPos == 0)
{
continue;
}
if (commentPos != std::string::npos)
{
line = StripSpaces(line.substr(0, commentPos));
}
}
lines->push_back(line);
}
return true;
} }
void IniFile::SortSections() void IniFile::SortSections()
@ -439,7 +449,7 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)
// INI is a hack anyway. // INI is a hack anyway.
if ((key == "" && value == "") || if ((key == "" && value == "") ||
(line.size() >= 1 && (line[0] == '$' || line[0] == '+' || line[0] == '*'))) (line.size() >= 1 && (line[0] == '$' || line[0] == '+' || line[0] == '*')))
current_section->lines.push_back(line); current_section->m_lines.push_back(line);
else else
current_section->Set(key, value); current_section->Set(key, value);
} }
@ -464,12 +474,12 @@ bool IniFile::Save(const std::string& filename)
for (const Section& section : sections) for (const Section& section : sections)
{ {
if (section.keys_order.size() != 0 || section.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)
{ {
for (const std::string& s : section.lines) for (const std::string& s : section.m_lines)
out << s << std::endl; out << s << std::endl;
} }
else else

View File

@ -66,18 +66,22 @@ public:
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; bool Get(const std::string& key, double* value, double defaultValue = 0.0) const;
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);
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; }
using SectionMap = std::map<std::string, std::string, CaseInsensitiveStringCompare>; using SectionMap = std::map<std::string, std::string, CaseInsensitiveStringCompare>;
const std::string& GetName() const { return name; } const std::string& GetName() const { return name; }
const SectionMap& GetValues() const { return values; } const SectionMap& GetValues() const { return values; }
bool HasLines() const { return !m_lines.empty(); }
protected: protected:
std::string name; std::string name;
std::vector<std::string> keys_order; std::vector<std::string> keys_order;
SectionMap values; SectionMap values;
std::vector<std::string> lines; std::vector<std::string> m_lines;
}; };
/** /**