Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Format/IniFile.h
5696 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 HasKey(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, std::string_view newValue);
69
void Set(std::string_view key, std::string_view newValue, std::string_view defaultValue);
70
71
void Set(std::string_view key, uint32_t newValue);
72
void Set(std::string_view key, uint64_t newValue);
73
void Set(std::string_view key, float newValue);
74
void Set(std::string_view key, const float newValue, const float defaultValue);
75
void Set(std::string_view key, double newValue);
76
77
void Set(std::string_view key, int newValue, int defaultValue);
78
void Set(std::string_view key, int newValue);
79
80
void Set(std::string_view key, bool newValue, bool defaultValue);
81
void Set(std::string_view key, const char *newValue) { Set(key, std::string_view{newValue}); }
82
void Set(std::string_view key, bool newValue) {
83
Set(key, std::string_view(newValue ? "True" : "False"));
84
}
85
void Set(std::string_view key, const std::vector<std::string>& newValues);
86
87
void AddComment(std::string_view comment);
88
89
bool Get(std::string_view key, std::string *value) const;
90
bool Get(std::string_view key, int* value) const;
91
bool Get(std::string_view key, uint32_t* value) const;
92
bool Get(std::string_view key, uint64_t* value) const;
93
bool Get(std::string_view key, bool* value) const;
94
bool Get(std::string_view key, float* value) const;
95
bool Get(std::string_view key, double* value) const;
96
bool Get(std::string_view key, std::vector<std::string> *values) const;
97
98
// Return a list of all keys in this section
99
bool GetKeys(std::vector<std::string> *keys) const;
100
101
bool operator < (const Section& other) const {
102
return name_ < other.name_;
103
}
104
105
const std::string &name() const {
106
return name_;
107
}
108
109
// For reading without copying. Note: You may have to ignore lines with empty keys.
110
const std::vector<ParsedIniLine> &Lines() const {
111
return lines_;
112
}
113
114
protected:
115
std::vector<ParsedIniLine> lines_;
116
std::string name_;
117
std::string comment;
118
};
119
120
class IniFile {
121
public:
122
bool Load(const Path &path);
123
bool Load(std::istream &istream);
124
bool LoadFromVFS(VFSInterface &vfs, const std::string &filename);
125
126
bool Save(const Path &path);
127
128
bool DeleteSection(std::string_view sectionName);
129
130
void SortSections();
131
132
std::vector<std::unique_ptr<Section>> &Sections() { return sections; }
133
134
bool HasSection(std::string_view section) { return GetSection(section) != nullptr; }
135
const Section* GetSection(std::string_view section) const;
136
Section* GetSection(std::string_view section);
137
138
Section* GetOrCreateSection(std::string_view section);
139
140
private:
141
std::vector<std::unique_ptr<Section>> sections;
142
};
143
144