-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.h
144 lines (117 loc) · 3.23 KB
/
Config.h
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2004-2011 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#ifndef CONFIG_H
#define CONFIG_H
#include "zncconfig.h"
#include "ZNCString.h"
class CFile;
class CConfig;
struct CConfigEntry {
CConfigEntry();
CConfigEntry(const CConfig& Config);
CConfigEntry(const CConfigEntry& other);
~CConfigEntry();
CConfigEntry& operator=(const CConfigEntry& other);
CConfig* m_pSubConfig;
};
class CConfig {
public:
typedef map<CString, VCString> EntryMap;
typedef map<CString, CConfigEntry> SubConfig;
typedef map<CString, SubConfig> SubConfigMap;
typedef EntryMap::const_iterator EntryMapIterator;
typedef SubConfigMap::const_iterator SubConfigMapIterator;
EntryMapIterator BeginEntries() const {
return m_ConfigEntries.begin();
}
EntryMapIterator EndEntries() const {
return m_ConfigEntries.end();
}
SubConfigMapIterator BeginSubConfigs() const {
return m_SubConfigs.begin();
}
SubConfigMapIterator EndSubConfigs() const {
return m_SubConfigs.end();
}
void AddKeyValuePair(const CString& sName, const CString& sValue) {
if (sName.empty() || sValue.empty()) {
return;
}
m_ConfigEntries[sName].push_back(sValue);
}
bool AddSubConfig(const CString& sTag, const CString& sName, CConfig Config) {
SubConfig &conf = m_SubConfigs[sTag];
SubConfig::const_iterator it = conf.find(sName);
if (it != conf.end()) {
return false;
}
conf[sName] = Config;
return true;
}
bool FindStringVector(const CString& sName, VCString& vsList, bool bErase = true) {
EntryMap::iterator it = m_ConfigEntries.find(sName);
vsList.clear();
if (it == m_ConfigEntries.end())
return false;
vsList = it->second;
if (bErase) {
m_ConfigEntries.erase(it);
}
return true;
}
bool FindStringEntry(const CString& sName, CString& sRes, const CString& sDefault = "") {
EntryMap::iterator it = m_ConfigEntries.find(sName);
sRes = sDefault;
if (it == m_ConfigEntries.end() || it->second.empty())
return false;
sRes = it->second.front();
it->second.erase(it->second.begin());
if (it->second.empty())
m_ConfigEntries.erase(it);
return true;
}
bool FindBoolEntry(const CString& sName, bool& bRes, bool bDefault = false) {
CString s;
if (FindStringEntry(sName, s)) {
bRes = s.ToBool();
return true;
}
bRes = bDefault;
return false;
}
bool FindUIntEntry(const CString& sName, unsigned int& uRes, unsigned int uDefault = 0) {
CString s;
if (FindStringEntry(sName, s)) {
uRes = s.ToUInt();
return true;
}
uRes = uDefault;
return false;
}
bool FindSubConfig(const CString& sName, SubConfig& Config, bool bErase = true) {
SubConfigMap::iterator it = m_SubConfigs.find(sName);
if (it == m_SubConfigs.end()) {
Config.clear();
return false;
}
Config = it->second;
if (bErase) {
m_SubConfigs.erase(it);
}
return true;
}
bool empty() const {
return m_ConfigEntries.empty() && m_SubConfigs.empty();
}
bool Parse(CFile& file, CString& sErrorMsg);
void Write(CFile& file, unsigned int iIndentation = 0);
private:
EntryMap m_ConfigEntries;
SubConfigMap m_SubConfigs;
};
#endif // !CONFIG_H