Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Format/IniFile.h
3189 views
1
// IniFile
2
// Taken from Dolphin but relicensed by me, Henrik Rydgard, under the MIT
3
// license as I wrote the whole thing originally and it has barely changed.
4
5
#pragma once
6
7
#include <istream>
8
#include <memory>
9
#include <map>
10
#include <string>
11
#include <string_view>
12
#include <vector>
13
#include <cstdint>
14
15
#include "Common/File/Path.h"
16
17
class VFSInterface;
18
19
class ParsedIniLine {
20
public:
21
explicit ParsedIniLine(std::string_view line);
22
23
ParsedIniLine(std::string_view key, std::string_view value) {
24
this->key = key;
25
this->value = value;
26
}
27
ParsedIniLine(std::string_view key, std::string_view value, std::string_view comment) {
28
this->key = key;
29
this->value = value;
30
this->comment = comment;
31
}
32
static ParsedIniLine CommentOnly(std::string_view comment) {
33
return ParsedIniLine(std::string_view(), std::string_view(), comment);
34
}
35
36
void Reconstruct(std::string *output) const;
37
38
// Having these as views allows a more efficient internal representation, like one joint string.
39
std::string_view Key() const { return key; }
40
std::string_view Value() const { return value; }
41
std::string_view Comment() const { return comment; }
42
43
void SetValue(std::string_view newValue) { value = newValue; }
44
45
private:
46
std::string key;
47
std::string value;
48
std::string comment;
49
};
50
51
class Section {
52
friend class IniFile;
53
54
public:
55
Section() {}
56
Section(std::string_view name) : name_(name) {}
57
58
bool Exists(std::string_view key) const;
59
bool Delete(std::string_view key);
60
61
void Clear();
62
63
std::map<std::string, std::string> ToMap() const;
64
65
ParsedIniLine *GetLine(std::string_view key);
66
const ParsedIniLine *GetLine(std::string_view key) const;
67
68
void Set(std::string_view key, const char* newValue);
69
void Set(std::string_view key, const std::string& newValue, const std::string& defaultValue);
70
71
void Set(std::string_view key, const std::string &value) {
72
Set(key, value.c_str());
73
}
74
bool Get(std::string_view key, std::string* value, const char* defaultValue) const;
75
76
void Set(std::string_view key, uint32_t newValue);
77
void Set(std::string_view key, uint64_t newValue);
78
void Set(std::string_view key, float newValue);
79
void Set(std::string_view key, const float newValue, const float defaultValue);
80
void Set(std::string_view key, double newValue);
81
82
void Set(std::string_view key, int newValue, int defaultValue);
83
void Set(std::string_view key, int newValue);
84
85
void Set(std::string_view key, bool newValue, bool defaultValue);
86
void Set(std::string_view key, bool newValue) {
87
Set(key, newValue ? "True" : "False");
88
}
89
void Set(std::string_view key, const std::vector<std::string>& newValues);
90
91
// Declare without a body to make it fail to compile. This is to prevent accidentally
92
// setting a pointer as a bool. The failure is in the linker unfortunately, but that's better
93
// than accidentally succeeding in a bad way.
94
template<class T>
95
void Set(std::string_view key, T *ptr);
96
97
void AddComment(const std::string &comment);
98
99
bool Get(std::string_view key, int* value, int defaultValue = 0) const;
100
bool Get(std::string_view key, uint32_t* value, uint32_t defaultValue = 0) const;
101
bool Get(std::string_view key, uint64_t* value, uint64_t defaultValue = 0) const;
102
bool Get(std::string_view key, bool* value, bool defaultValue = false) const;
103
bool Get(std::string_view key, float* value, float defaultValue = false) const;
104
bool Get(std::string_view key, double* value, double defaultValue = false) const;
105
bool Get(std::string_view key, std::vector<std::string>& values) const;
106
107
// Return a list of all keys in this section
108
bool GetKeys(std::vector<std::string> &keys) const;
109
110
bool operator < (const Section& other) const {
111
return name_ < other.name_;
112
}
113
114
const std::string &name() const {
115
return name_;
116
}
117
118
// For reading without copying. Note: You may have to ignore lines with empty keys.
119
const std::vector<ParsedIniLine> &Lines() const {
120
return lines_;
121
}
122
123
protected:
124
std::vector<ParsedIniLine> lines_;
125
std::string name_;
126
std::string comment;
127
};
128
129
class IniFile {
130
public:
131
bool Load(const Path &path);
132
bool Load(std::istream &istream);
133
bool LoadFromVFS(VFSInterface &vfs, const std::string &filename);
134
135
bool Save(const Path &path);
136
137
// Returns true if key exists in section
138
bool Exists(const char* sectionName, const char* key) const;
139
140
// These will not create the section if it doesn't exist.
141
bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");
142
bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);
143
bool Get(const char* sectionName, const char* key, uint32_t* value, uint32_t defaultValue = 0);
144
bool Get(const char* sectionName, const char* key, uint64_t* value, uint64_t defaultValue = 0);
145
bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);
146
bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);
147
148
bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;
149
150
bool DeleteKey(const char* sectionName, const char* key);
151
bool DeleteSection(const char* sectionName);
152
153
void SortSections();
154
155
std::vector<std::unique_ptr<Section>> &Sections() { return sections; }
156
157
bool HasSection(const char *section) { return GetSection(section) != nullptr; }
158
const Section* GetSection(const char* section) const;
159
Section* GetSection(const char* section);
160
161
Section* GetOrCreateSection(const char* section);
162
163
private:
164
std::vector<std::unique_ptr<Section>> sections;
165
};
166
167