2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
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 20:57:05 +00:00
|
|
|
#include <algorithm>
|
2014-06-24 17:28:02 +00:00
|
|
|
#include <list>
|
2013-09-06 17:56:19 +00:00
|
|
|
#include <map>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
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 20:57:05 +00:00
|
|
|
#include <string_view>
|
2010-06-03 18:05:08 +00:00
|
|
|
#include <vector>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-06-03 12:18:58 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2010-06-04 19:56:34 +00:00
|
|
|
class IniFile
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2010-06-03 18:05:08 +00:00
|
|
|
public:
|
2010-06-04 19:56:34 +00:00
|
|
|
class Section
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2010-06-05 05:30:23 +00:00
|
|
|
friend class IniFile;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-06-05 05:30:23 +00:00
|
|
|
public:
|
2017-03-22 22:19:53 +00:00
|
|
|
Section();
|
|
|
|
explicit Section(std::string name_);
|
2019-06-16 21:28:00 +00:00
|
|
|
bool Exists(std::string_view key) const;
|
|
|
|
bool Delete(std::string_view key);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-27 02:15:32 +00:00
|
|
|
void Set(const std::string& key, std::string new_value);
|
|
|
|
|
2018-06-03 12:18:58 +00:00
|
|
|
template <typename T>
|
2018-12-27 02:15:32 +00:00
|
|
|
void Set(const std::string& key, T&& new_value)
|
2018-06-03 12:18:58 +00:00
|
|
|
{
|
2018-12-27 02:15:32 +00:00
|
|
|
Set(key, ValueToString(std::forward<T>(new_value)));
|
2018-06-03 12:18:58 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-09-04 07:29:49 +00:00
|
|
|
template <typename T>
|
2018-12-27 02:15:32 +00:00
|
|
|
void Set(const std::string& key, T&& new_value, const std::common_type_t<T>& default_value)
|
2014-09-04 07:29:49 +00:00
|
|
|
{
|
2018-06-03 12:18:58 +00:00
|
|
|
if (new_value != default_value)
|
2018-12-27 02:15:32 +00:00
|
|
|
Set(key, std::forward<T>(new_value));
|
2014-09-04 07:29:49 +00:00
|
|
|
else
|
|
|
|
Delete(key);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-16 21:28:00 +00:00
|
|
|
bool Get(std::string_view key, std::string* value,
|
2018-12-27 02:15:32 +00:00
|
|
|
const std::string& default_value = NULL_STRING) const;
|
|
|
|
|
2018-06-03 12:39:38 +00:00
|
|
|
template <typename T>
|
2019-06-16 21:28:00 +00:00
|
|
|
bool Get(std::string_view key, T* value, const std::common_type_t<T>& default_value = {}) const
|
2018-06-03 12:39:38 +00:00
|
|
|
{
|
|
|
|
std::string temp;
|
|
|
|
bool retval = Get(key, &temp);
|
|
|
|
if (retval && TryParse(temp, value))
|
|
|
|
return true;
|
|
|
|
*value = default_value;
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-12-27 02:15:32 +00:00
|
|
|
void SetLines(std::vector<std::string> lines);
|
2016-02-10 18:19:15 +00:00
|
|
|
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
|
|
|
|
|
2014-08-30 20:14:56 +00:00
|
|
|
bool operator<(const Section& other) const { return name < other.name; }
|
2024-09-22 05:32:24 +00:00
|
|
|
using SectionMap = std::map<std::string, std::string, CaseInsensitiveLess>;
|
2016-01-14 22:38:06 +00:00
|
|
|
|
|
|
|
const std::string& GetName() const { return name; }
|
|
|
|
const SectionMap& GetValues() const { return values; }
|
2016-02-10 18:19:15 +00:00
|
|
|
bool HasLines() const { return !m_lines.empty(); }
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2010-06-05 05:30:23 +00:00
|
|
|
protected:
|
|
|
|
std::string name;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-09-06 17:56:19 +00:00
|
|
|
std::vector<std::string> keys_order;
|
2016-01-14 22:38:06 +00:00
|
|
|
SectionMap values;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-02-10 18:19:15 +00:00
|
|
|
std::vector<std::string> m_lines;
|
2010-06-05 05:30:23 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-07 22:25:32 +00:00
|
|
|
IniFile();
|
|
|
|
~IniFile();
|
|
|
|
|
2013-09-06 17:56:19 +00:00
|
|
|
/**
|
|
|
|
* Loads sections and keys.
|
|
|
|
* @param filename filename of the ini file which should be loaded
|
|
|
|
* @param keep_current_data If true, "extends" the currently loaded list of sections and keys with
|
|
|
|
* the loaded data (and replaces existing entries). If false, existing data will be erased.
|
|
|
|
* @warning Using any other operations than "Get*" and "Exists" is untested and will behave
|
|
|
|
* unexpectedly
|
|
|
|
* @todo This really is just a hack to support having two levels of gameinis (defaults and
|
|
|
|
* user-specified) and should eventually be replaced with a less stupid system.
|
|
|
|
*/
|
2014-02-08 05:50:37 +00:00
|
|
|
bool Load(const std::string& filename, bool keep_current_data = false);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
bool Save(const std::string& filename);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-02-29 06:37:26 +00:00
|
|
|
bool Exists(std::string_view section_name) const;
|
2010-06-04 19:56:34 +00:00
|
|
|
// Returns true if key exists in section
|
2019-06-16 21:28:00 +00:00
|
|
|
bool Exists(std::string_view section_name, std::string_view key) const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-12-24 14:48:02 +00:00
|
|
|
template <typename T>
|
2019-06-16 21:28:00 +00:00
|
|
|
bool GetIfExists(std::string_view section_name, std::string_view key, T* value)
|
2011-02-13 22:36:12 +00:00
|
|
|
{
|
2019-06-16 21:28:00 +00:00
|
|
|
if (Exists(section_name, key))
|
|
|
|
return GetOrCreateSection(section_name)->Get(key, value);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-12-24 14:48:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-12-24 14:48:02 +00:00
|
|
|
template <typename T>
|
2019-06-16 21:28:00 +00:00
|
|
|
bool GetIfExists(std::string_view section_name, std::string_view key, T* value, T default_value)
|
2015-12-24 14:48:02 +00:00
|
|
|
{
|
2019-06-16 21:28:00 +00:00
|
|
|
if (Exists(section_name, key))
|
|
|
|
return GetOrCreateSection(section_name)->Get(key, value, default_value);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-16 21:28:00 +00:00
|
|
|
*value = default_value;
|
2011-02-13 22:36:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-16 21:28:00 +00:00
|
|
|
bool GetKeys(std::string_view section_name, std::vector<std::string>* keys) const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-01-24 04:58:23 +00:00
|
|
|
void SetLines(std::string_view section_name, std::vector<std::string> lines);
|
2019-06-16 21:28:00 +00:00
|
|
|
bool GetLines(std::string_view section_name, std::vector<std::string>* lines,
|
|
|
|
bool remove_comments = true) const;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-16 21:28:00 +00:00
|
|
|
bool DeleteKey(std::string_view section_name, std::string_view key);
|
|
|
|
bool DeleteSection(std::string_view section_name);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-06-03 18:05:08 +00:00
|
|
|
void SortSections();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-06-16 21:28:00 +00:00
|
|
|
Section* GetOrCreateSection(std::string_view section_name);
|
2021-02-27 18:21:23 +00:00
|
|
|
const Section* GetSection(std::string_view section_name) const;
|
|
|
|
Section* GetSection(std::string_view section_name);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-07-29 16:47:56 +00:00
|
|
|
// This function is related to parsing data from lines of INI files
|
|
|
|
// It's used outside of IniFile, which is why it is exposed publicly
|
|
|
|
// In particular it is used in PostProcessing for its configuration
|
2019-07-08 11:35:53 +00:00
|
|
|
static void ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut);
|
2014-07-29 16:47:56 +00:00
|
|
|
|
2016-01-14 22:38:06 +00:00
|
|
|
const std::list<Section>& GetSections() const { return sections; }
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2008-10-13 19:21:25 +00:00
|
|
|
private:
|
2014-06-24 17:28:02 +00:00
|
|
|
std::list<Section> sections;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-08 05:50:37 +00:00
|
|
|
static const std::string& NULL_STRING;
|
2008-07-12 17:40:22 +00:00
|
|
|
};
|
2023-04-13 13:38:09 +00:00
|
|
|
} // namespace Common
|