Skip to content

Commit

Permalink
Merge pull request #5135 from lioncash/ini
Browse files Browse the repository at this point in the history
IniFile: Minor changes
  • Loading branch information
Helios747 committed Mar 25, 2017
2 parents e2a7e8a + 29ca229 commit b099a1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
47 changes: 28 additions & 19 deletions Source/Core/Common/IniFile.cpp
Expand Up @@ -23,7 +23,7 @@ void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::strin
if (line[0] == '#')
return;

size_t firstEquals = line.find("=", 0);
size_t firstEquals = line.find('=');

if (firstEquals != std::string::npos)
{
Expand All @@ -39,6 +39,12 @@ void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::strin

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)
{
auto it = values.find(key);
Expand All @@ -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)
{
std::string temp;
// Join the strings with ,
for (const std::string& value : newValues)
{
temp = value + ",";
}
// remove last ,
temp.resize(temp.length() - 1);
Set(key, temp);
Set(key, JoinStrings(newValues, ","));
}

void IniFile::Section::Set(const std::string& key, u32 newValue)
Expand Down Expand Up @@ -261,27 +259,32 @@ void IniFile::Section::SetLines(const std::vector<std::string>& 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
{
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)
{
size_t commentPos = line.find('#');
size_t commentPos = stripped_line.find('#');
if (commentPos == 0)
{
continue;
}

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;
Expand Down Expand Up @@ -346,6 +349,12 @@ void IniFile::SetLines(const std::string& sectionName, const std::vector<std::st
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)
{
Section* section = GetSection(sectionName);
Expand Down Expand Up @@ -418,17 +427,17 @@ bool IniFile::Load(const std::string& filename, bool keep_current_data)

#ifndef _WIN32
// 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

if (line.size() > 0)
{
if (line[0] == '[')
{
size_t endpos = line.find("]");
size_t endpos = line.find(']');

if (endpos != std::string::npos)
{
Expand Down Expand Up @@ -475,7 +484,7 @@ bool IniFile::Save(const std::string& filename)
for (const Section& section : sections)
{
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)
{
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/Common/IniFile.h
Expand Up @@ -29,8 +29,8 @@ class IniFile
friend class IniFile;

public:
Section() {}
Section(const std::string& _name) : name(_name) {}
Section();
explicit Section(std::string name_);
bool Exists(const std::string& key) const;
bool Delete(const std::string& key);

Expand Down Expand Up @@ -67,6 +67,7 @@ class IniFile
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);
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;

bool operator<(const Section& other) const { return name < other.name; }
Expand Down Expand Up @@ -124,6 +125,7 @@ class IniFile
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& section_name, std::vector<std::string>&& lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>* lines,
const bool remove_comments = true) const;

Expand Down

0 comments on commit b099a1c

Please sign in to comment.