ZNC  trunk
Config.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZNC_CONFIG_H
18 #define ZNC_CONFIG_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 
23 class CFile;
24 class CConfig;
25 
26 struct CConfigEntry {
28  CConfigEntry(const CConfig& Config);
29  CConfigEntry(const CConfigEntry& other);
32 
34 };
35 
36 class CConfig {
37  public:
38  CConfig() : m_ConfigEntries(), m_SubConfigs(), m_SubConfigNameSets() {}
39 
40  typedef std::map<CString, VCString> EntryMap;
41  typedef std::pair<CString, CConfigEntry> SubConfigEntry;
42  typedef std::vector<SubConfigEntry> SubConfig;
43  typedef std::map<CString, SubConfig> SubConfigMap;
44 
45  typedef EntryMap::const_iterator EntryMapIterator;
46  typedef SubConfigMap::const_iterator SubConfigMapIterator;
47 
48  EntryMapIterator BeginEntries() const { return m_ConfigEntries.begin(); }
49  EntryMapIterator EndEntries() const { return m_ConfigEntries.end(); }
50 
52  return m_SubConfigs.begin();
53  }
54  SubConfigMapIterator EndSubConfigs() const { return m_SubConfigs.end(); }
55 
56  void AddKeyValuePair(const CString& sName, const CString& sValue) {
57  if (sName.empty() || sValue.empty()) {
58  return;
59  }
60 
61  m_ConfigEntries[sName].push_back(sValue);
62  }
63 
64  bool AddSubConfig(const CString& sTag, const CString& sName,
65  CConfig Config) {
66  auto& nameset = m_SubConfigNameSets[sTag];
67 
68  if (nameset.find(sName) != nameset.end()) return false;
69 
70  nameset.insert(sName);
71  m_SubConfigs[sTag].emplace_back(sName, Config);
72 
73  return true;
74  }
75 
76  bool FindStringVector(const CString& sName, VCString& vsList,
77  bool bErase = true) {
78  EntryMap::iterator it = m_ConfigEntries.find(sName);
79  vsList.clear();
80  if (it == m_ConfigEntries.end()) return false;
81  vsList = it->second;
82 
83  if (bErase) {
84  m_ConfigEntries.erase(it);
85  }
86 
87  return true;
88  }
89 
90  bool FindStringEntry(const CString& sName, CString& sRes,
91  const CString& sDefault = "") {
92  EntryMap::iterator it = m_ConfigEntries.find(sName);
93  sRes = sDefault;
94  if (it == m_ConfigEntries.end() || it->second.empty()) return false;
95  sRes = it->second.front();
96  it->second.erase(it->second.begin());
97  if (it->second.empty()) m_ConfigEntries.erase(it);
98  return true;
99  }
100 
101  bool FindBoolEntry(const CString& sName, bool& bRes,
102  bool bDefault = false) {
103  CString s;
104  if (FindStringEntry(sName, s)) {
105  bRes = s.ToBool();
106  return true;
107  }
108  bRes = bDefault;
109  return false;
110  }
111 
112  bool FindUIntEntry(const CString& sName, unsigned int& uRes,
113  unsigned int uDefault = 0) {
114  CString s;
115  if (FindStringEntry(sName, s)) {
116  uRes = s.ToUInt();
117  return true;
118  }
119  uRes = uDefault;
120  return false;
121  }
122 
123  bool FindUShortEntry(const CString& sName, unsigned short& uRes,
124  unsigned short uDefault = 0) {
125  CString s;
126  if (FindStringEntry(sName, s)) {
127  uRes = s.ToUShort();
128  return true;
129  }
130  uRes = uDefault;
131  return false;
132  }
133 
134  bool FindDoubleEntry(const CString& sName, double& fRes,
135  double fDefault = 0) {
136  CString s;
137  if (FindStringEntry(sName, s)) {
138  fRes = s.ToDouble();
139  return true;
140  }
141  fRes = fDefault;
142  return false;
143  }
144 
145  bool FindSubConfig(const CString& sTag, SubConfig& Config,
146  bool bErase = true) {
147  auto it = m_SubConfigs.find(sTag);
148  if (it == m_SubConfigs.end()) {
149  Config.clear();
150  return false;
151  }
152  Config = it->second;
153 
154  if (bErase) {
155  m_SubConfigs.erase(it);
156  m_SubConfigNameSets.erase(sTag);
157  }
158 
159  return true;
160  }
161 
162  bool empty() const {
163  return m_ConfigEntries.empty() && m_SubConfigs.empty();
164  }
165 
166  bool Parse(CFile& file, CString& sErrorMsg);
167  void Write(CFile& file, unsigned int iIndentation = 0);
168 
169  private:
170  typedef SCString SubConfigNameSet;
171  typedef std::map<CString, SubConfigNameSet> SubConfigNameSetMap;
172 
173  EntryMap m_ConfigEntries;
174  SubConfigMap m_SubConfigs;
175  SubConfigNameSetMap m_SubConfigNameSets;
176 };
177 
178 #endif // !ZNC_CONFIG_H
std::set< CString > SCString
Definition: ZNCString.h:35
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Config.h:36
bool FindStringVector(const CString &sName, VCString &vsList, bool bErase=true)
Definition: Config.h:76
std::map< CString, VCString > EntryMap
Definition: Config.h:40
void AddKeyValuePair(const CString &sName, const CString &sValue)
Definition: Config.h:56
std::vector< SubConfigEntry > SubConfig
Definition: Config.h:42
SubConfigMapIterator BeginSubConfigs() const
Definition: Config.h:51
bool FindBoolEntry(const CString &sName, bool &bRes, bool bDefault=false)
Definition: Config.h:101
bool AddSubConfig(const CString &sTag, const CString &sName, CConfig Config)
Definition: Config.h:64
bool FindUShortEntry(const CString &sName, unsigned short &uRes, unsigned short uDefault=0)
Definition: Config.h:123
bool FindStringEntry(const CString &sName, CString &sRes, const CString &sDefault="")
Definition: Config.h:90
bool Parse(CFile &file, CString &sErrorMsg)
EntryMapIterator EndEntries() const
Definition: Config.h:49
void Write(CFile &file, unsigned int iIndentation=0)
bool FindUIntEntry(const CString &sName, unsigned int &uRes, unsigned int uDefault=0)
Definition: Config.h:112
std::pair< CString, CConfigEntry > SubConfigEntry
Definition: Config.h:41
bool empty() const
Definition: Config.h:162
bool FindSubConfig(const CString &sTag, SubConfig &Config, bool bErase=true)
Definition: Config.h:145
CConfig()
Definition: Config.h:38
std::map< CString, SubConfig > SubConfigMap
Definition: Config.h:43
EntryMapIterator BeginEntries() const
Definition: Config.h:48
SubConfigMapIterator EndSubConfigs() const
Definition: Config.h:54
bool FindDoubleEntry(const CString &sName, double &fRes, double fDefault=0)
Definition: Config.h:134
EntryMap::const_iterator EntryMapIterator
Definition: Config.h:45
SubConfigMap::const_iterator SubConfigMapIterator
Definition: Config.h:46
Definition: FileUtils.h:30
String class that is used inside ZNC.
Definition: ZNCString.h:68
unsigned short ToUShort() const
bool ToBool() const
unsigned int ToUInt() const
double ToDouble() const
Definition: Config.h:26
CConfigEntry(const CConfigEntry &other)
CConfig * m_pSubConfig
Definition: Config.h:33
CConfigEntry(const CConfig &Config)
CConfigEntry & operator=(const CConfigEntry &other)