1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include <cstring>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"
struct CaseInsensitiveStringCompare
{
bool operator() (const std::string& a, const std::string& b) const
{
return strcasecmp(a.c_str(), b.c_str()) < 0;
}
};
class IniFile
{
public:
class Section
{
friend class IniFile;
public:
Section() {}
Section(const std::string& _name) : name(_name) {}
bool Exists(const std::string& key) const;
bool Delete(const std::string& key);
void Set(const std::string& key, const std::string& newValue);
void Set(const std::string& key, const std::string& newValue, const std::string& defaultValue);
bool Get(const std::string& key, std::string* value, const std::string& defaultValue = NULL_STRING);
void Set(const std::string& key, u32 newValue) {
Set(key, StringFromFormat("0x%08x", newValue));
}
void Set(const std::string& key, float newValue) {
Set(key, StringFromFormat("%f", newValue));
}
void Set(const std::string& key, const float newValue, const float defaultValue);
void Set(const std::string& key, double newValue) {
Set(key, StringFromFormat("%f", newValue));
}
void Set(const std::string& key, int newValue, int defaultValue);
void Set(const std::string& key, int newValue) {
Set(key, StringFromInt(newValue));
}
void Set(const std::string& key, bool newValue, bool defaultValue);
void Set(const std::string& key, bool newValue) {
Set(key, StringFromBool(newValue));
}
void Set(const std::string& key, const std::vector<std::string>& newValues);
bool Get(const std::string& key, int* value, int defaultValue = 0);
bool Get(const std::string& key, u32* value, u32 defaultValue = 0);
bool Get(const std::string& key, bool* value, bool defaultValue = false);
bool Get(const std::string& key, float* value, float defaultValue = false);
bool Get(const std::string& key, double* value, double defaultValue = false);
bool Get(const std::string& key, std::vector<std::string>* values);
bool operator < (const Section& other) const {
return name < other.name;
}
protected:
std::string name;
std::vector<std::string> keys_order;
std::map<std::string, std::string, CaseInsensitiveStringCompare> values;
std::vector<std::string> lines;
};
/**
* 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.
*/
bool Load(const std::string& filename, bool keep_current_data = false);
bool Save(const std::string& filename);
// Returns true if key exists in section
bool Exists(const std::string& sectionName, const std::string& key) const;
template<typename T> bool GetIfExists(const std::string& sectionName, const std::string& key, T value)
{
if (Exists(sectionName, key))
return GetOrCreateSection(sectionName)->Get(key, value);
return false;
}
bool GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const;
void SetLines(const std::string& sectionName, const std::vector<std::string>& lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>* lines, const bool remove_comments = true) const;
bool DeleteKey(const std::string& sectionName, const std::string& key);
bool DeleteSection(const std::string& sectionName);
void SortSections();
Section* GetOrCreateSection(const std::string& section);
private:
std::list<Section> sections;
const Section* GetSection(const std::string& section) const;
Section* GetSection(const std::string& section);
std::string* GetLine(const std::string& section, const std::string& key);
void CreateSection(const std::string& section);
static const std::string& NULL_STRING;
};
|