CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Format/IniFile.h
Views: 1401
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
ParsedIniLine() {}
22
ParsedIniLine(std::string_view key, std::string_view value) {
23
this->key = key;
24
this->value = value;
25
}
26
ParsedIniLine(std::string_view key, std::string_view value, std::string_view comment) {
27
this->key = key;
28
this->value = value;
29
this->comment = comment;
30
}
31
static ParsedIniLine CommentOnly(std::string_view comment) {
32
return ParsedIniLine(std::string_view(), std::string_view(), comment);
33
}
34
35
// Comments only come from "ParseFrom".
36
void ParseFrom(std::string_view line);
37
void Reconstruct(std::string *output) const;
38
39
// Having these as views allows a more efficient internal representation, like one joint string.
40
std::string_view Key() const { return key; }
41
std::string_view Value() const { return value; }
42
std::string_view Comment() const { return comment; }
43
44
void SetValue(std::string_view newValue) { value = newValue; }
45
46
private:
47
std::string key;
48
std::string value;
49
std::string comment;
50
};
51
52
class Section {
53
friend class IniFile;
54
55
public:
56
Section() {}
57
Section(std::string_view name) : name_(name) {}
58
59
bool Exists(std::string_view key) const;
60
bool Delete(std::string_view key);
61
62
void Clear();
63
64
std::map<std::string, std::string> ToMap() const;
65
66
ParsedIniLine *GetLine(std::string_view key);
67
const ParsedIniLine *GetLine(std::string_view key) const;
68
69
void Set(std::string_view key, const char* newValue);
70
void Set(std::string_view key, const std::string& newValue, const std::string& defaultValue);
71
72
void Set(std::string_view key, const std::string &value) {
73
Set(key, value.c_str());
74
}
75
bool Get(std::string_view key, std::string* value, const char* defaultValue) const;
76
77
void Set(std::string_view key, uint32_t newValue);
78
void Set(std::string_view key, uint64_t newValue);
79
void Set(std::string_view key, float newValue);
80
void Set(std::string_view key, const float newValue, const float defaultValue);
81
void Set(std::string_view key, double newValue);
82
83
void Set(std::string_view key, int newValue, int defaultValue);
84
void Set(std::string_view key, int newValue);
85
86
void Set(std::string_view key, bool newValue, bool defaultValue);
87
void Set(std::string_view key, bool newValue) {
88
Set(key, newValue ? "True" : "False");
89
}
90
void Set(std::string_view key, const std::vector<std::string>& newValues);
91
92
// Declare without a body to make it fail to compile. This is to prevent accidentally
93
// setting a pointer as a bool. The failure is in the linker unfortunately, but that's better
94
// than accidentally succeeding in a bad way.
95
template<class T>
96
void Set(std::string_view key, T *ptr);
97
98
void AddComment(const std::string &comment);
99
100
bool Get(std::string_view key, int* value, int defaultValue = 0) const;
101
bool Get(std::string_view key, uint32_t* value, uint32_t defaultValue = 0) const;
102
bool Get(std::string_view key, uint64_t* value, uint64_t defaultValue = 0) const;
103
bool Get(std::string_view key, bool* value, bool defaultValue = false) const;
104
bool Get(std::string_view key, float* value, float defaultValue = false) const;
105
bool Get(std::string_view key, double* value, double defaultValue = false) const;
106
bool Get(std::string_view key, std::vector<std::string>& values) const;
107
108
// Return a list of all keys in this section
109
bool GetKeys(std::vector<std::string> &keys) const;
110
111
bool operator < (const Section& other) const {
112
return name_ < other.name_;
113
}
114
115
const std::string &name() const {
116
return name_;
117
}
118
119
protected:
120
std::vector<ParsedIniLine> lines_;
121
std::string name_;
122
std::string comment;
123
};
124
125
class IniFile {
126
public:
127
bool Load(const Path &path);
128
bool Load(std::istream &istream);
129
bool LoadFromVFS(VFSInterface &vfs, const std::string &filename);
130
131
bool Save(const Path &path);
132
133
// Returns true if key exists in section
134
bool Exists(const char* sectionName, const char* key) const;
135
136
// These will not create the section if it doesn't exist.
137
bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");
138
bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);
139
bool Get(const char* sectionName, const char* key, uint32_t* value, uint32_t defaultValue = 0);
140
bool Get(const char* sectionName, const char* key, uint64_t* value, uint64_t defaultValue = 0);
141
bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);
142
bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);
143
144
bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;
145
146
bool DeleteKey(const char* sectionName, const char* key);
147
bool DeleteSection(const char* sectionName);
148
149
void SortSections();
150
151
std::vector<std::unique_ptr<Section>> &Sections() { return sections; }
152
153
bool HasSection(const char *section) { return GetSection(section) != nullptr; }
154
const Section* GetSection(const char* section) const;
155
Section* GetSection(const char* section);
156
157
Section* GetOrCreateSection(const char* section);
158
159
private:
160
std::vector<std::unique_ptr<Section>> sections;
161
};
162
163