2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-02-25 03:56:33 +00:00
|
|
|
#include "Common/IniFile.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2017-02-25 03:56:33 +00:00
|
|
|
#include <cinttypes>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <fstream>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <map>
|
2010-06-03 18:05:08 +00:00
|
|
|
#include <string>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <utility>
|
2010-06-03 18:05:08 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-07-29 16:47:56 +00:00
|
|
|
void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut)
|
2010-06-04 19:56:34 +00:00
|
|
|
{
|
2019-05-23 01:09:09 +00:00
|
|
|
if (line.empty() || line.front() == '#')
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2010-06-04 19:56:34 +00:00
|
|
|
|
2017-03-22 23:03:14 +00:00
|
|
|
size_t firstEquals = line.find('=');
|
2010-06-04 19:56:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (firstEquals != std::string::npos)
|
|
|
|
{
|
|
|
|
// Yes, a valid line!
|
|
|
|
*keyOut = StripSpaces(line.substr(0, firstEquals));
|
2014-05-15 00:36:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (valueOut)
|
|
|
|
{
|
|
|
|
*valueOut = StripQuotes(StripSpaces(line.substr(firstEquals + 1, std::string::npos)));
|
|
|
|
}
|
|
|
|
}
|
2010-06-04 19:56:34 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
const std::string& IniFile::NULL_STRING = "";
|
|
|
|
|
2017-03-22 22:19:53 +00:00
|
|
|
IniFile::Section::Section() = default;
|
|
|
|
|
|
|
|
IniFile::Section::Section(std::string name_) : name{std::move(name_)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-27 02:15:32 +00:00
|
|
|
void IniFile::Section::Set(const std::string& key, std::string new_value)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2019-05-23 00:58:47 +00:00
|
|
|
const auto result = values.insert_or_assign(key, std::move(new_value));
|
|
|
|
const bool insertion_occurred = result.second;
|
|
|
|
|
|
|
|
if (insertion_occurred)
|
2016-06-24 08:43:46 +00:00
|
|
|
keys_order.push_back(key);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
bool IniFile::Section::Get(const std::string& key, std::string* value,
|
|
|
|
const std::string& defaultValue) const
|
2013-09-06 17:56:19 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
auto it = values.find(key);
|
|
|
|
if (it != values.end())
|
|
|
|
{
|
|
|
|
*value = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (&defaultValue != &NULL_STRING)
|
|
|
|
{
|
|
|
|
*value = defaultValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-06 17:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::Section::Exists(const std::string& key) const
|
2009-03-20 11:51:22 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return values.find(key) != values.end();
|
2010-06-04 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::Section::Delete(const std::string& key)
|
2010-06-05 05:30:23 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
auto it = values.find(key);
|
|
|
|
if (it == values.end())
|
|
|
|
return false;
|
2013-09-06 17:56:19 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
values.erase(it);
|
|
|
|
keys_order.erase(std::find(keys_order.begin(), keys_order.end(), key));
|
|
|
|
return true;
|
2010-06-05 05:30:23 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2018-12-27 02:15:32 +00:00
|
|
|
void IniFile::Section::SetLines(std::vector<std::string> lines)
|
2017-03-22 22:49:11 +00:00
|
|
|
{
|
|
|
|
m_lines = std::move(lines);
|
|
|
|
}
|
|
|
|
|
2016-02-10 18:19:15 +00:00
|
|
|
bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const
|
|
|
|
{
|
2017-03-22 23:09:22 +00:00
|
|
|
for (const std::string& line : m_lines)
|
2016-02-10 18:19:15 +00:00
|
|
|
{
|
2017-03-22 23:09:22 +00:00
|
|
|
std::string stripped_line = StripSpaces(line);
|
2016-02-10 18:19:15 +00:00
|
|
|
|
|
|
|
if (remove_comments)
|
|
|
|
{
|
2017-03-22 23:09:22 +00:00
|
|
|
size_t commentPos = stripped_line.find('#');
|
2016-02-10 18:19:15 +00:00
|
|
|
if (commentPos == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commentPos != std::string::npos)
|
|
|
|
{
|
2017-03-22 23:09:22 +00:00
|
|
|
stripped_line = StripSpaces(stripped_line.substr(0, commentPos));
|
2016-02-10 18:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:09:22 +00:00
|
|
|
lines->push_back(std::move(stripped_line));
|
2016-02-10 18:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
// IniFile
|
|
|
|
|
2019-06-07 22:25:32 +00:00
|
|
|
IniFile::IniFile() = default;
|
|
|
|
|
|
|
|
IniFile::~IniFile() = default;
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const
|
2010-06-04 19:56:34 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (const Section& sect : sections)
|
|
|
|
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
|
|
|
|
return (&(sect));
|
|
|
|
return nullptr;
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
IniFile::Section* IniFile::GetSection(const std::string& sectionName)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (Section& sect : sections)
|
|
|
|
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
|
|
|
|
return (&(sect));
|
|
|
|
return nullptr;
|
2010-06-04 19:56:34 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
IniFile::Section* IniFile::GetOrCreateSection(const std::string& sectionName)
|
2010-06-04 19:56:34 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Section* section = GetSection(sectionName);
|
|
|
|
if (!section)
|
|
|
|
{
|
|
|
|
sections.emplace_back(sectionName);
|
|
|
|
section = §ions.back();
|
|
|
|
}
|
|
|
|
return section;
|
2010-06-04 19:56:34 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::DeleteSection(const std::string& sectionName)
|
2010-06-04 19:56:34 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Section* s = GetSection(sectionName);
|
|
|
|
if (!s)
|
|
|
|
return false;
|
|
|
|
for (auto iter = sections.begin(); iter != sections.end(); ++iter)
|
|
|
|
{
|
|
|
|
if (&(*iter) == s)
|
|
|
|
{
|
|
|
|
sections.erase(iter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2009-03-20 11:51:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::Exists(const std::string& sectionName, const std::string& key) const
|
2010-06-04 19:56:34 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const Section* section = GetSection(sectionName);
|
|
|
|
if (!section)
|
|
|
|
return false;
|
|
|
|
return section->Exists(key);
|
2010-06-04 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string>& lines)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Section* section = GetOrCreateSection(sectionName);
|
2016-02-10 18:19:15 +00:00
|
|
|
section->SetLines(lines);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-01-28 16:09:08 +00:00
|
|
|
|
2017-03-22 22:49:11 +00:00
|
|
|
void IniFile::SetLines(const std::string& section_name, std::vector<std::string>&& lines)
|
|
|
|
{
|
|
|
|
Section* section = GetOrCreateSection(section_name);
|
|
|
|
section->SetLines(std::move(lines));
|
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Section* section = GetSection(sectionName);
|
|
|
|
if (!section)
|
|
|
|
return false;
|
|
|
|
return section->Delete(key);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
// Return a list of all keys in a section
|
2014-03-15 02:29:53 +00:00
|
|
|
bool IniFile::GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const Section* section = GetSection(sectionName);
|
|
|
|
if (!section)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*keys = section->keys_order;
|
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
// Return a list of all lines in a section
|
2016-06-24 08:43:46 +00:00
|
|
|
bool IniFile::GetLines(const std::string& sectionName, std::vector<std::string>* lines,
|
|
|
|
const bool remove_comments) const
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
lines->clear();
|
|
|
|
|
|
|
|
const Section* section = GetSection(sectionName);
|
|
|
|
if (!section)
|
|
|
|
return false;
|
|
|
|
|
2016-02-10 18:19:15 +00:00
|
|
|
return section->GetLines(lines, remove_comments);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
void IniFile::SortSections()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
sections.sort();
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!keep_current_data)
|
|
|
|
sections.clear();
|
|
|
|
// first section consists of the comments before the first real section
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
std::ifstream in;
|
2017-01-15 21:46:43 +00:00
|
|
|
File::OpenFStream(in, filename, std::ios::in);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (in.fail())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Section* current_section = nullptr;
|
|
|
|
bool first_line = true;
|
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
if (!std::getline(in, line))
|
|
|
|
{
|
|
|
|
if (in.eof())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skips the UTF-8 BOM at the start of files. Notepad likes to add this.
|
|
|
|
if (first_line && line.substr(0, 3) == "\xEF\xBB\xBF")
|
|
|
|
line = line.substr(3);
|
|
|
|
first_line = false;
|
2015-06-05 16:32:58 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
#ifndef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
// Check for CRLF eol and convert it to LF
|
2017-03-22 23:32:07 +00:00
|
|
|
if (!line.empty() && line.back() == '\r')
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-03-22 23:32:07 +00:00
|
|
|
line.pop_back();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-06-03 18:05:08 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-12 22:47:17 +00:00
|
|
|
if (!line.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (line[0] == '[')
|
|
|
|
{
|
2017-03-22 23:03:14 +00:00
|
|
|
size_t endpos = line.find(']');
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (endpos != std::string::npos)
|
|
|
|
{
|
|
|
|
// New section!
|
|
|
|
std::string sub = line.substr(1, endpos - 1);
|
|
|
|
current_section = GetOrCreateSection(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (current_section)
|
|
|
|
{
|
|
|
|
std::string key, value;
|
|
|
|
ParseLine(line, &key, &value);
|
|
|
|
|
|
|
|
// Lines starting with '$', '*' or '+' are kept verbatim.
|
|
|
|
// Kind of a hack, but the support for raw lines inside an
|
|
|
|
// INI is a hack anyway.
|
2019-02-12 22:47:17 +00:00
|
|
|
if ((key.empty() && value.empty()) ||
|
|
|
|
(!line.empty() && (line[0] == '$' || line[0] == '+' || line[0] == '*')))
|
2016-02-10 18:19:15 +00:00
|
|
|
current_section->m_lines.push_back(line);
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
|
|
|
current_section->Set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
return true;
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool IniFile::Save(const std::string& filename)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::ofstream out;
|
|
|
|
std::string temp = File::GetTempFilenameForAtomicWrite(filename);
|
2017-01-15 21:46:43 +00:00
|
|
|
File::OpenFStream(out, temp, std::ios::out);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (out.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Section& section : sections)
|
|
|
|
{
|
2019-02-12 22:47:17 +00:00
|
|
|
if (!section.keys_order.empty() || !section.m_lines.empty())
|
2017-03-22 23:03:14 +00:00
|
|
|
out << '[' << section.name << ']' << std::endl;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-12 22:47:17 +00:00
|
|
|
if (section.keys_order.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-02-10 18:19:15 +00:00
|
|
|
for (const std::string& s : section.m_lines)
|
2016-06-24 08:43:46 +00:00
|
|
|
out << s << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const std::string& kvit : section.keys_order)
|
|
|
|
{
|
|
|
|
auto pair = section.values.find(kvit);
|
|
|
|
out << pair->first << " = " << pair->second << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
return File::RenameSync(temp, filename);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2010-06-04 19:56:34 +00:00
|
|
|
// Unit test. TODO: Move to the real unit test framework.
|
2010-06-03 18:05:08 +00:00
|
|
|
/*
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
IniFile ini;
|
|
|
|
ini.Load("my.ini");
|
2017-02-04 21:51:01 +00:00
|
|
|
ini.Set("Hello", "A", "amaskdfl");
|
|
|
|
ini.Set("Moss", "A", "amaskdfl");
|
2010-06-03 18:05:08 +00:00
|
|
|
ini.Set("Aissa", "A", "amaskdfl");
|
|
|
|
//ini.Read("my.ini");
|
|
|
|
std::string x;
|
2017-02-04 21:51:01 +00:00
|
|
|
ini.Get("Hello", "B", &x, "boo");
|
|
|
|
ini.DeleteKey("Moss", "A");
|
|
|
|
ini.DeleteSection("Moss");
|
2010-06-03 18:05:08 +00:00
|
|
|
ini.SortSections();
|
|
|
|
ini.Save("my.ini");
|
|
|
|
//UpdateVars(ini);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|