ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Config.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2014 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 CONFIG_H
18 #define CONFIG_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 
23 class CFile;
24 class CConfig;
25 
26 struct CConfigEntry {
27  CConfigEntry();
28  CConfigEntry(const CConfig& Config);
29  CConfigEntry(const CConfigEntry& other);
30  ~CConfigEntry();
31  CConfigEntry& operator=(const CConfigEntry& other);
32 
34 };
35 
36 class CConfig {
37 public:
38  typedef std::map<CString, VCString> EntryMap;
39  typedef std::map<CString, CConfigEntry> SubConfig;
40  typedef std::map<CString, SubConfig> SubConfigMap;
41 
42  typedef EntryMap::const_iterator EntryMapIterator;
43  typedef SubConfigMap::const_iterator SubConfigMapIterator;
44 
46  return m_ConfigEntries.begin();
47  }
49  return m_ConfigEntries.end();
50  }
51 
53  return m_SubConfigs.begin();
54  }
56  return m_SubConfigs.end();
57  }
58 
59  void AddKeyValuePair(const CString& sName, const CString& sValue) {
60  if (sName.empty() || sValue.empty()) {
61  return;
62  }
63 
64  m_ConfigEntries[sName].push_back(sValue);
65  }
66 
67  bool AddSubConfig(const CString& sTag, const CString& sName, CConfig Config) {
68  SubConfig &conf = m_SubConfigs[sTag];
69  SubConfig::const_iterator it = conf.find(sName);
70 
71  if (it != conf.end()) {
72  return false;
73  }
74 
75  conf[sName] = Config;
76  return true;
77  }
78 
79  bool FindStringVector(const CString& sName, VCString& vsList, bool bErase = true) {
80  EntryMap::iterator it = m_ConfigEntries.find(sName);
81  vsList.clear();
82  if (it == m_ConfigEntries.end())
83  return false;
84  vsList = it->second;
85 
86  if (bErase) {
87  m_ConfigEntries.erase(it);
88  }
89 
90  return true;
91  }
92 
93  bool FindStringEntry(const CString& sName, CString& sRes, const CString& sDefault = "") {
94  EntryMap::iterator it = m_ConfigEntries.find(sName);
95  sRes = sDefault;
96  if (it == m_ConfigEntries.end() || it->second.empty())
97  return false;
98  sRes = it->second.front();
99  it->second.erase(it->second.begin());
100  if (it->second.empty())
101  m_ConfigEntries.erase(it);
102  return true;
103  }
104 
105  bool FindBoolEntry(const CString& sName, bool& bRes, bool bDefault = false) {
106  CString s;
107  if (FindStringEntry(sName, s)) {
108  bRes = s.ToBool();
109  return true;
110  }
111  bRes = bDefault;
112  return false;
113  }
114 
115  bool FindUIntEntry(const CString& sName, unsigned int& uRes, unsigned int uDefault = 0) {
116  CString s;
117  if (FindStringEntry(sName, s)) {
118  uRes = s.ToUInt();
119  return true;
120  }
121  uRes = uDefault;
122  return false;
123  }
124 
125  bool FindUShortEntry(const CString& sName, unsigned short& uRes, unsigned short uDefault = 0) {
126  CString s;
127  if (FindStringEntry(sName, s)) {
128  uRes = s.ToUShort();
129  return true;
130  }
131  uRes = uDefault;
132  return false;
133  }
134 
135  bool FindDoubleEntry(const CString& sName, double& fRes, 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& sName, SubConfig& Config, bool bErase = true) {
146  SubConfigMap::iterator it = m_SubConfigs.find(sName);
147  if (it == m_SubConfigs.end()) {
148  Config.clear();
149  return false;
150  }
151  Config = it->second;
152 
153  if (bErase) {
154  m_SubConfigs.erase(it);
155  }
156 
157  return true;
158  }
159 
160  bool empty() const {
161  return m_ConfigEntries.empty() && m_SubConfigs.empty();
162  }
163 
164  bool Parse(CFile& file, CString& sErrorMsg);
165  void Write(CFile& file, unsigned int iIndentation = 0);
166 
167 private:
168  EntryMap m_ConfigEntries;
169  SubConfigMap m_SubConfigs;
170 };
171 
172 #endif // !CONFIG_H
bool FindBoolEntry(const CString &sName, bool &bRes, bool bDefault=false)
Definition: Config.h:105
bool FindStringEntry(const CString &sName, CString &sRes, const CString &sDefault="")
Definition: Config.h:93
CConfigEntry & operator=(const CConfigEntry &other)
Definition: Config.h:26
std::map< CString, SubConfig > SubConfigMap
Definition: Config.h:40
EntryMap::const_iterator EntryMapIterator
Definition: Config.h:42
bool FindStringVector(const CString &sName, VCString &vsList, bool bErase=true)
Definition: Config.h:79
void AddKeyValuePair(const CString &sName, const CString &sValue)
Definition: Config.h:59
std::map< CString, CConfigEntry > SubConfig
Definition: Config.h:39
bool FindDoubleEntry(const CString &sName, double &fRes, double fDefault=0)
Definition: Config.h:135
std::map< CString, VCString > EntryMap
Definition: Config.h:38
CConfig * m_pSubConfig
Definition: Config.h:33
bool empty() const
Definition: Config.h:160
double ToDouble() const
Definition: FileUtils.h:30
std::vector< CString > VCString
Definition: ZNCString.h:37
SubConfigMap::const_iterator SubConfigMapIterator
Definition: Config.h:43
String class that is used inside ZNC.
Definition: ZNCString.h:67
bool AddSubConfig(const CString &sTag, const CString &sName, CConfig Config)
Definition: Config.h:67
bool FindSubConfig(const CString &sName, SubConfig &Config, bool bErase=true)
Definition: Config.h:145
bool FindUIntEntry(const CString &sName, unsigned int &uRes, unsigned int uDefault=0)
Definition: Config.h:115
unsigned int ToUInt() const
bool ToBool() const
SubConfigMapIterator BeginSubConfigs() const
Definition: Config.h:52
EntryMapIterator BeginEntries() const
Definition: Config.h:45
bool FindUShortEntry(const CString &sName, unsigned short &uRes, unsigned short uDefault=0)
Definition: Config.h:125
unsigned short ToUShort() const
void Write(CFile &file, unsigned int iIndentation=0)
SubConfigMapIterator EndSubConfigs() const
Definition: Config.h:55
bool Parse(CFile &file, CString &sErrorMsg)
EntryMapIterator EndEntries() const
Definition: Config.h:48
Definition: Config.h:36