2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2017-02-25 04:56:33 +01:00
|
|
|
#include "Common/IniFile.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-02-17 11:18:15 +01:00
|
|
|
#include <algorithm>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <cstddef>
|
2014-02-17 11:18:15 +01:00
|
|
|
#include <fstream>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <map>
|
2010-06-03 20:05:08 +02:00
|
|
|
#include <string>
|
2019-07-08 13:35:53 +02:00
|
|
|
#include <string_view>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <utility>
|
2010-06-03 20:05:08 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2023-04-13 15:38:09 +02:00
|
|
|
namespace Common
|
|
|
|
{
|
2019-07-08 13:35:53 +02:00
|
|
|
void IniFile::ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut)
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
2019-05-23 03:09:09 +02:00
|
|
|
if (line.empty() || line.front() == '#')
|
2013-08-11 15:19:46 +02:00
|
|
|
return;
|
2010-06-04 21:56:34 +02:00
|
|
|
|
2017-03-23 00:03:14 +01:00
|
|
|
size_t firstEquals = line.find('=');
|
2010-06-04 21:56:34 +02:00
|
|
|
|
2014-05-15 02:36:57 +02:00
|
|
|
if (firstEquals != std::string::npos)
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
|
|
|
// Yes, a valid line!
|
2022-07-20 00:13:26 +02:00
|
|
|
*keyOut = StripWhitespace(line.substr(0, firstEquals));
|
2014-05-15 02:36:57 +02:00
|
|
|
|
|
|
|
if (valueOut)
|
|
|
|
{
|
2022-07-20 00:13:26 +02:00
|
|
|
*valueOut = StripQuotes(StripWhitespace(line.substr(firstEquals + 1, std::string::npos)));
|
2014-05-15 02:36:57 +02:00
|
|
|
}
|
2010-06-04 21:56:34 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2014-02-08 06:50:37 +01:00
|
|
|
const std::string& IniFile::NULL_STRING = "";
|
|
|
|
|
2017-03-22 23:19:53 +01:00
|
|
|
IniFile::Section::Section() = default;
|
|
|
|
|
|
|
|
IniFile::Section::Section(std::string name_) : name{std::move(name_)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:15:32 +01:00
|
|
|
void IniFile::Section::Set(const std::string& key, std::string new_value)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2019-05-23 02:58:47 +02:00
|
|
|
const auto result = values.insert_or_assign(key, std::move(new_value));
|
|
|
|
const bool insertion_occurred = result.second;
|
|
|
|
|
|
|
|
if (insertion_occurred)
|
2013-09-06 19:56:19 +02:00
|
|
|
keys_order.push_back(key);
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::Section::Get(std::string_view key, std::string* value,
|
|
|
|
const std::string& default_value) const
|
2013-09-06 19:56:19 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
const auto it = values.find(key);
|
|
|
|
|
2013-09-06 19:56:19 +02:00
|
|
|
if (it != values.end())
|
|
|
|
{
|
|
|
|
*value = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-16 23:28:00 +02:00
|
|
|
|
|
|
|
if (&default_value != &NULL_STRING)
|
2013-09-06 19:56:19 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
*value = default_value;
|
2013-09-06 19:56:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-08-20 04:22:28 +02:00
|
|
|
return false;
|
2013-09-06 19:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::Section::Exists(std::string_view key) const
|
2009-03-20 12:51:22 +01:00
|
|
|
{
|
2013-09-06 19:56:19 +02:00
|
|
|
return values.find(key) != values.end();
|
2010-06-04 21:56:34 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::Section::Delete(std::string_view key)
|
2010-06-05 07:30:23 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
const auto it = values.find(key);
|
2013-09-06 19:56:19 +02:00
|
|
|
if (it == values.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
values.erase(it);
|
|
|
|
keys_order.erase(std::find(keys_order.begin(), keys_order.end(), key));
|
|
|
|
return true;
|
2010-06-05 07:30:23 +02:00
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2018-12-27 03:15:32 +01:00
|
|
|
void IniFile::Section::SetLines(std::vector<std::string> lines)
|
2017-03-22 23:49:11 +01:00
|
|
|
{
|
|
|
|
m_lines = std::move(lines);
|
|
|
|
}
|
|
|
|
|
2016-02-10 19:19:15 +01:00
|
|
|
bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const
|
|
|
|
{
|
2017-03-23 00:09:22 +01:00
|
|
|
for (const std::string& line : m_lines)
|
2016-02-10 19:19:15 +01:00
|
|
|
{
|
2022-07-20 00:13:26 +02:00
|
|
|
std::string_view stripped_line = StripWhitespace(line);
|
2016-02-10 19:19:15 +01:00
|
|
|
|
|
|
|
if (remove_comments)
|
|
|
|
{
|
2017-03-23 00:09:22 +01:00
|
|
|
size_t commentPos = stripped_line.find('#');
|
2016-02-10 19:19:15 +01:00
|
|
|
if (commentPos == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commentPos != std::string::npos)
|
|
|
|
{
|
2022-07-20 00:13:26 +02:00
|
|
|
stripped_line = StripWhitespace(stripped_line.substr(0, commentPos));
|
2016-02-10 19:19:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:35:53 +02:00
|
|
|
lines->emplace_back(stripped_line);
|
2016-02-10 19:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-04 21:56:34 +02:00
|
|
|
// IniFile
|
|
|
|
|
2019-06-08 00:25:32 +02:00
|
|
|
IniFile::IniFile() = default;
|
|
|
|
|
|
|
|
IniFile::~IniFile() = default;
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
const IniFile::Section* IniFile::GetSection(std::string_view section_name) const
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
2014-02-12 16:00:34 +01:00
|
|
|
for (const Section& sect : sections)
|
Common/IniFile: Make CaseInsensitiveStringCompare usable with heterogenous lookup
Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.
This means cases like:
map.find(some_string_view)
aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.
We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.
We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.
While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
2019-06-16 22:57:05 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name))
|
Common/IniFile: Make CaseInsensitiveStringCompare usable with heterogenous lookup
Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.
This means cases like:
map.find(some_string_view)
aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.
We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.
We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.
While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
2019-06-16 22:57:05 +02:00
|
|
|
return §
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:14:26 +01:00
|
|
|
return nullptr;
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
IniFile::Section* IniFile::GetSection(std::string_view section_name)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2014-02-12 16:00:34 +01:00
|
|
|
for (Section& sect : sections)
|
Common/IniFile: Make CaseInsensitiveStringCompare usable with heterogenous lookup
Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.
This means cases like:
map.find(some_string_view)
aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.
We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.
We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.
While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
2019-06-16 22:57:05 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name))
|
Common/IniFile: Make CaseInsensitiveStringCompare usable with heterogenous lookup
Previously, when performing find() operations or indexing operations on
the section map, it would need to operate on a std::string key.
This means cases like:
map.find(some_string_view)
aren't usable, which kind of sucks, especially given for most cases, we
use regular string literals to perform operations in calling code.
However, since C++14, it's possible to use heterogenous lookup to avoid
needing to construct exact key types. In otherwords, we can perform the
above or use string literals without constructing a std::string instance
around them implicitly.
We simply need to specify a member type within our comparison struct
named is_transparent, to allow std::map to perform automatic type
deduction.
We also slightly alter the algorithm to an equivalent compatible with
std::string_view (which need not be null-terminated), as strcasecmp
requires null-terminated strings.
While we're at it, we can also provide a helper function to the struct
for comparing string equality rather than only less than. This allows
removing other usages of strcasecmp in other functions, allowing for the
transition of them to std::string_view.
2019-06-16 22:57:05 +02:00
|
|
|
return §
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:14:26 +01:00
|
|
|
return nullptr;
|
2010-06-04 21:56:34 +02:00
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
IniFile::Section* IniFile::GetOrCreateSection(std::string_view section_name)
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
Section* section = GetSection(section_name);
|
2010-06-03 20:05:08 +02:00
|
|
|
if (!section)
|
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
sections.emplace_back(std::string(section_name));
|
2014-06-24 19:28:02 +02:00
|
|
|
section = §ions.back();
|
2010-06-04 21:56:34 +02:00
|
|
|
}
|
|
|
|
return section;
|
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::DeleteSection(std::string_view section_name)
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
Section* s = GetSection(section_name);
|
2010-06-04 21:56:34 +02:00
|
|
|
if (!s)
|
|
|
|
return false;
|
2019-06-16 23:28:00 +02:00
|
|
|
|
2014-02-12 16:00:34 +01:00
|
|
|
for (auto iter = sections.begin(); iter != sections.end(); ++iter)
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
|
|
|
if (&(*iter) == s)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2010-06-04 21:56:34 +02:00
|
|
|
sections.erase(iter);
|
2010-06-03 20:05:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 23:28:00 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
return false;
|
2009-03-20 12:51:22 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 07:37:26 +01:00
|
|
|
bool IniFile::Exists(std::string_view section_name) const
|
|
|
|
{
|
|
|
|
return GetSection(section_name) != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::Exists(std::string_view section_name, std::string_view key) const
|
2010-06-04 21:56:34 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
const Section* section = GetSection(section_name);
|
2010-06-04 21:56:34 +02:00
|
|
|
if (!section)
|
|
|
|
return false;
|
2019-06-16 23:28:00 +02:00
|
|
|
|
2010-06-04 21:56:34 +02:00
|
|
|
return section->Exists(key);
|
|
|
|
}
|
|
|
|
|
2020-01-24 05:58:23 +01:00
|
|
|
void IniFile::SetLines(std::string_view section_name, std::vector<std::string> lines)
|
2017-03-22 23:49:11 +01:00
|
|
|
{
|
|
|
|
Section* section = GetOrCreateSection(section_name);
|
|
|
|
section->SetLines(std::move(lines));
|
|
|
|
}
|
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::DeleteKey(std::string_view section_name, std::string_view key)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
Section* section = GetSection(section_name);
|
2010-06-03 20:05:08 +02:00
|
|
|
if (!section)
|
|
|
|
return false;
|
2013-09-06 19:56:19 +02:00
|
|
|
return section->Delete(key);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
// Return a list of all keys in a section
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::GetKeys(std::string_view section_name, std::vector<std::string>* keys) const
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2019-06-16 23:28:00 +02:00
|
|
|
const Section* section = GetSection(section_name);
|
2010-06-03 20:05:08 +02:00
|
|
|
if (!section)
|
2014-03-15 03:29:53 +01:00
|
|
|
{
|
2010-06-03 20:05:08 +02:00
|
|
|
return false;
|
2014-03-15 03:29:53 +01:00
|
|
|
}
|
|
|
|
*keys = section->keys_order;
|
2010-06-03 20:05:08 +02:00
|
|
|
return true;
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
// Return a list of all lines in a section
|
2019-06-16 23:28:00 +02:00
|
|
|
bool IniFile::GetLines(std::string_view section_name, std::vector<std::string>* lines,
|
2014-03-15 03:29:53 +01:00
|
|
|
const bool remove_comments) const
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2015-06-03 01:04:04 +02:00
|
|
|
lines->clear();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-06-16 23:28:00 +02:00
|
|
|
const Section* section = GetSection(section_name);
|
2010-06-03 20:05:08 +02:00
|
|
|
if (!section)
|
|
|
|
return false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-02-10 19:19:15 +01:00
|
|
|
return section->GetLines(lines, remove_comments);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
void IniFile::SortSections()
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2014-06-24 19:28:02 +02:00
|
|
|
sections.sort();
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-02-08 06:50:37 +01:00
|
|
|
bool IniFile::Load(const std::string& filename, bool keep_current_data)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2013-09-06 19:56:19 +02:00
|
|
|
if (!keep_current_data)
|
|
|
|
sections.clear();
|
2010-06-03 20:05:08 +02:00
|
|
|
// first section consists of the comments before the first real section
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
// Open file
|
|
|
|
std::ifstream in;
|
2017-01-15 22:46:43 +01:00
|
|
|
File::OpenFStream(in, filename, std::ios::in);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-05-29 02:15:59 +02:00
|
|
|
if (in.fail())
|
|
|
|
return false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-03-09 21:14:26 +01:00
|
|
|
Section* current_section = nullptr;
|
2015-06-05 18:32:58 +02:00
|
|
|
bool first_line = true;
|
2010-06-03 20:05:08 +02:00
|
|
|
while (!in.eof())
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2019-07-08 13:35:53 +02:00
|
|
|
std::string line_str;
|
|
|
|
if (!std::getline(in, line_str))
|
2014-05-28 18:18:28 +02:00
|
|
|
{
|
|
|
|
if (in.eof())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-07-08 13:35:53 +02:00
|
|
|
std::string_view line = line_str;
|
|
|
|
|
2015-06-05 18:32:58 +02:00
|
|
|
// 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")
|
2019-07-08 13:35:53 +02:00
|
|
|
line.remove_prefix(3);
|
2015-06-05 18:32:58 +02:00
|
|
|
first_line = false;
|
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
// Check for CRLF eol and convert it to LF
|
2017-03-23 00:32:07 +01:00
|
|
|
if (!line.empty() && line.back() == '\r')
|
2019-07-08 13:35:53 +02:00
|
|
|
line.remove_suffix(1);
|
2010-06-03 20:05:08 +02:00
|
|
|
#endif
|
|
|
|
|
2019-02-12 23:47:17 +01:00
|
|
|
if (!line.empty())
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2010-06-03 20:05:08 +02:00
|
|
|
if (line[0] == '[')
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2017-03-23 00:03:14 +01:00
|
|
|
size_t endpos = line.find(']');
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
if (endpos != std::string::npos)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2010-06-03 20:05:08 +02:00
|
|
|
// New section!
|
2019-07-08 13:35:53 +02:00
|
|
|
std::string_view sub = line.substr(1, endpos - 1);
|
2014-02-08 06:50:37 +01:00
|
|
|
current_section = GetOrCreateSection(sub);
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
2010-06-03 20:05:08 +02:00
|
|
|
else
|
|
|
|
{
|
2013-09-06 19:56:19 +02:00
|
|
|
if (current_section)
|
|
|
|
{
|
|
|
|
std::string key, value;
|
|
|
|
ParseLine(line, &key, &value);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-09-17 16:31:51 +02:00
|
|
|
// 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 23:47:17 +01:00
|
|
|
if ((key.empty() && value.empty()) ||
|
|
|
|
(!line.empty() && (line[0] == '$' || line[0] == '+' || line[0] == '*')))
|
2019-07-08 13:35:53 +02:00
|
|
|
{
|
|
|
|
current_section->m_lines.emplace_back(line);
|
|
|
|
}
|
2013-09-06 19:56:19 +02:00
|
|
|
else
|
2019-07-08 13:35:53 +02:00
|
|
|
{
|
2014-02-08 06:50:37 +01:00
|
|
|
current_section->Set(key, value);
|
2019-07-08 13:35:53 +02:00
|
|
|
}
|
2013-09-06 19:56:19 +02:00
|
|
|
}
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
in.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-08 06:50:37 +01:00
|
|
|
bool IniFile::Save(const std::string& filename)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
|
|
|
std::ofstream out;
|
2013-10-15 08:52:06 +02:00
|
|
|
std::string temp = File::GetTempFilenameForAtomicWrite(filename);
|
2017-01-15 22:46:43 +01:00
|
|
|
File::OpenFStream(out, temp, std::ios::out);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
if (out.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-02-12 16:00:34 +01:00
|
|
|
for (const Section& section : sections)
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2019-02-12 23:47:17 +01:00
|
|
|
if (!section.keys_order.empty() || !section.m_lines.empty())
|
2017-03-23 00:03:14 +01:00
|
|
|
out << '[' << section.name << ']' << std::endl;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-02-12 23:47:17 +01:00
|
|
|
if (section.keys_order.empty())
|
2013-09-06 19:56:19 +02:00
|
|
|
{
|
2016-02-10 19:19:15 +01:00
|
|
|
for (const std::string& s : section.m_lines)
|
2013-09-06 19:56:19 +02:00
|
|
|
out << s << std::endl;
|
|
|
|
}
|
|
|
|
else
|
2010-06-03 20:05:08 +02:00
|
|
|
{
|
2014-02-12 16:00:34 +01:00
|
|
|
for (const std::string& kvit : section.keys_order)
|
2013-09-06 19:56:19 +02:00
|
|
|
{
|
2014-02-12 16:00:34 +01:00
|
|
|
auto pair = section.values.find(kvit);
|
2013-09-06 19:56:19 +02:00
|
|
|
out << pair->first << " = " << pair->second << std::endl;
|
|
|
|
}
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-06-03 20:05:08 +02:00
|
|
|
out.close();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-10-15 08:52:06 +02:00
|
|
|
return File::RenameSync(temp, filename);
|
2010-06-03 20:05:08 +02:00
|
|
|
}
|
|
|
|
|
2010-06-04 21:56:34 +02:00
|
|
|
// Unit test. TODO: Move to the real unit test framework.
|
2010-06-03 20:05:08 +02:00
|
|
|
/*
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
IniFile ini;
|
|
|
|
ini.Load("my.ini");
|
2017-02-04 22:51:01 +01:00
|
|
|
ini.Set("Hello", "A", "amaskdfl");
|
|
|
|
ini.Set("Moss", "A", "amaskdfl");
|
2010-06-03 20:05:08 +02:00
|
|
|
ini.Set("Aissa", "A", "amaskdfl");
|
|
|
|
//ini.Read("my.ini");
|
|
|
|
std::string x;
|
2017-02-04 22:51:01 +01:00
|
|
|
ini.Get("Hello", "B", &x, "boo");
|
|
|
|
ini.DeleteKey("Moss", "A");
|
|
|
|
ini.DeleteSection("Moss");
|
2010-06-03 20:05:08 +02:00
|
|
|
ini.SortSections();
|
|
|
|
ini.Save("my.ini");
|
|
|
|
//UpdateVars(ini);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|
2023-04-13 15:38:09 +02:00
|
|
|
} // namespace Common
|