| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <list> |
| 7 |
#include <map> |
| 8 |
#include <string> |
| 9 |
#include <string_view> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include "Common/StringUtil.h" |
| 13 |
|
| 14 |
namespace Common |
| 15 |
{ |
| 16 |
class IniFile |
| 17 |
{ |
| 18 |
public: |
| 19 |
class Section |
| 20 |
{ |
| 21 |
friend class IniFile; |
| 22 |
|
| 23 |
public: |
| 24 |
Section(); |
| 25 |
explicit Section(std::string name_); |
| 26 |
bool Exists(std::string_view key) const; |
| 27 |
bool Delete(std::string_view key); |
| 28 |
|
| 29 |
void Set(const std::string& key, std::string new_value); |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
void Set(const std::string& key, T&& new_value) |
| 33 |
{ |
| 34 |
Set(key, ValueToString(std::forward<T>(new_value))); |
| 35 |
} |
| 36 |
|
| 37 |
template <typename T> |
| 38 |
void Set(const std::string& key, T&& new_value, const std::common_type_t<T>& default_value) |
| 39 |
{ |
| 40 |
if (new_value != default_value) |
| 41 |
Set(key, std::forward<T>(new_value)); |
| 42 |
else |
| 43 |
Delete(key); |
| 44 |
} |
| 45 |
|
| 46 |
bool Get(std::string_view key, std::string* value, |
| 47 |
const std::string& default_value = NULL_STRING) const; |
| 48 |
|
| 49 |
template <typename T> |
| 50 |
bool Get(std::string_view key, T* value, const std::common_type_t<T>& default_value = {}) const |
| 51 |
{ |
| 52 |
std::string temp; |
| 53 |
bool retval = Get(key, &temp); |
| 54 |
if (retval && TryParse(temp, value)) |
| 55 |
return true; |
| 56 |
*value = default_value; |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
void SetLines(std::vector<std::string> lines); |
| 61 |
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const; |
| 62 |
|
| 63 |
bool operator<(const Section& other) const { return name < other.name; } |
| 64 |
using SectionMap = std::map<std::string, std::string, CaseInsensitiveLess>; |
| 65 |
|
| 66 |
const std::string& GetName() const { return name; } |
| 67 |
const SectionMap& GetValues() const { return values; } |
| 68 |
bool HasLines() const { return !m_lines.empty(); } |
| 69 |
|
| 70 |
protected: |
| 71 |
std::string name; |
| 72 |
|
| 73 |
std::vector<std::string> keys_order; |
| 74 |
SectionMap values; |
| 75 |
|
| 76 |
std::vector<std::string> m_lines; |
| 77 |
}; |
| 78 |
|
| 79 |
IniFile(); |
| 80 |
~IniFile(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
bool Load(const std::string& filename, bool keep_current_data = false); |
| 93 |
|
| 94 |
bool Save(const std::string& filename); |
| 95 |
|
| 96 |
bool Exists(std::string_view section_name) const; |
| 97 |
|
| 98 |
bool Exists(std::string_view section_name, std::string_view key) const; |
| 99 |
|
| 100 |
template <typename T> |
| 101 |
bool GetIfExists(std::string_view section_name, std::string_view key, T* value) |
| 102 |
{ |
| 103 |
if (Exists(section_name, key)) |
| 104 |
return GetOrCreateSection(section_name)->Get(key, value); |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
template <typename T> |
| 110 |
bool GetIfExists(std::string_view section_name, std::string_view key, T* value, T default_value) |
| 111 |
{ |
| 112 |
if (Exists(section_name, key)) |
| 113 |
return GetOrCreateSection(section_name)->Get(key, value, default_value); |
| 114 |
|
| 115 |
*value = default_value; |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
bool GetKeys(std::string_view section_name, std::vector<std::string>* keys) const; |
| 120 |
|
| 121 |
void SetLines(std::string_view section_name, std::vector<std::string> lines); |
| 122 |
bool GetLines(std::string_view section_name, std::vector<std::string>* lines, |
| 123 |
bool remove_comments = true) const; |
| 124 |
|
| 125 |
bool DeleteKey(std::string_view section_name, std::string_view key); |
| 126 |
bool DeleteSection(std::string_view section_name); |
| 127 |
|
| 128 |
void SortSections(); |
| 129 |
|
| 130 |
Section* GetOrCreateSection(std::string_view section_name); |
| 131 |
const Section* GetSection(std::string_view section_name) const; |
| 132 |
Section* GetSection(std::string_view section_name); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut); |
| 138 |
|
| 139 |
const std::list<Section>& GetSections() const { return sections; } |
| 140 |
|
| 141 |
private: |
| 142 |
std::list<Section> sections; |
| 143 |
|
| 144 |
static const std::string& NULL_STRING; |
| 145 |
}; |
| 146 |
} |