Parent: [f51614] (diff)

Child: [d7f056] (diff)

Download this file

conftree.h    321 lines (283 with data), 9.2 kB

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _CONFTREE_H_
#define _CONFTREE_H_
/**
* A simple configuration file implementation.
*
* Configuration files have lines like 'name = value', and/or like '[subkey]'
*
* Lines like '[subkeyname]' in the file define subsections, with independant
* configuration namespaces.
*
* Whitespace around name and value is insignificant.
*
* The names are case-sensitive but don't count on it.
*
* Values can be queried for, or set.
* Any line without a '=' is discarded when rewriting the file.
* A configuration object can be created empty or by reading from a file or
* a string.
* All 'set' calls cause an immediate rewrite of the backing object if any
* (file or string)
*
* Config objects can be stacked (overlayed).
*/
#include <string>
#include <map>
#include <list>
// rh7.3 likes iostream better...
#if defined(__GNUC__) && __GNUC__ < 3
#include <iostream>
#else
#include <istream>
#endif
#ifndef NO_NAMESPACES
using std::string;
using std::list;
using std::map;
#endif // NO_NAMESPACES
/**
* Manages a simple configuration file with subsections.
*/
class ConfSimple {
public:
enum StatusCode {STATUS_ERROR=0, STATUS_RO=1, STATUS_RW=2};
/**
* Build the object by reading content from file.
* @param filename file to open
* @param readonly if true open readonly, else rw
* @param tildexp try tilde (home dir) expansion for subkey values
*/
ConfSimple(const char *fname, int readonly = 0, bool tildexp = false);
/**
* Build the object by reading content from a string
* @param data points to the data to parse.
* @param readonly if true open readonly, else rw
* @param tildexp try tilde (home dir) expansion for subsection names
*/
ConfSimple(string *data, int readonly = 0, bool tildexp = false);
/**
* Build an empty object. This will be memory only, with no backing store.
* @param readonly if true open read only, else rw
* @param tildexp try tilde (home dir) expansion for subsection names
*/
ConfSimple(int readonly = 0, bool tildexp = false);
virtual ~ConfSimple() {};
/**
* Get value for named parameter, from specified subsection (looks in
* global space if sk is empty).
* @return 0 if name not found, 1 else
*/
virtual int get(const std::string &name, string &value,
const string &sk = string(""));
/* Note: the version returning char* was buggy and has been removed */
/**
* Set value for named parameter in specified subsection (or global)
* @return 0 for error, 1 else
*/
virtual int set(const std::string &nm, const std::string &val,
const std::string &sk);
virtual int set(const char *name, const char *value, const char *sk = 0);
/**
* Remove name and value from config
*/
virtual int erase(const std::string &name, const std::string &sk);
virtual StatusCode getStatus();
virtual bool ok() {return getStatus() != STATUS_ERROR;}
/**
* Walk the configuration values, calling function for each.
* The function is called with a null nm when changing subsections (the
* value is then the new subsection name)
* @return WALK_STOP when/if the callback returns WALK_STOP,
* WALK_CONTINUE else (got to end of config)
*/
enum WalkerCode {WALK_STOP, WALK_CONTINUE};
virtual WalkerCode sortwalk(WalkerCode
(*wlkr)(void *cldata, const std::string &nm,
const std::string &val),
void *clidata);
virtual void list();
/**
* Return all names in given submap
*/
virtual std::list<string> getNames(const string &sk);
virtual std::string getFilename() {return filename;}
/**
* Copy constructor. Expensive but less so than a full rebuild
*/
ConfSimple(const ConfSimple &rhs) : data(0) {
if ((status = rhs.status) == STATUS_ERROR)
return;
filename = rhs.filename;
// Note: we just share the pointer, this doesnt belong to us
data = rhs.data;
submaps = rhs.submaps;
}
/**
* Assignement. This is expensive
*/
ConfSimple& operator=(const ConfSimple &rhs) {
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
filename = rhs.filename;
// Note: we don't own data. Just share the pointer
data = rhs.data;
submaps = rhs.submaps;
}
return *this;
}
protected:
bool dotildexpand;
StatusCode status;
private:
string filename; // set if we're working with a file
string *data; // set if we're working with an in-memory string
map<string, map<string, string> > submaps;
void parseinput(std::istream &input);
};
/**
* This is a configuration class which attaches tree-like signification to the
* submap names.
*
* If a given variable is not found in the specified section, it will be
* looked up the tree of section names, and in the global space.
*
* submap names should be '/' separated paths (ie: /sub1/sub2). No checking
* is done, but else the class adds no functionality to ConfSimple.
*
* NOTE: contrary to common behaviour, the global or root space is NOT
* designated by '/' but by '' (empty subkey). A '/' subkey will not
* be searched at all.
*/
class ConfTree : public ConfSimple {
public:
/**
* Build the object by reading content from file.
*/
ConfTree(const char *fname, int readonly = 0)
: ConfSimple(fname, readonly, true) {}
virtual ~ConfTree() {};
ConfTree(const ConfTree& r) : ConfSimple(r) {};
ConfTree& operator=(const ConfTree& r) {
ConfSimple::operator=(r);
return *this;
}
/**
* Get value for named parameter, from specified subsection, or its
* parents.
* @return 0 if name not found, 1 else
*/
virtual int get(const std::string &name, string &value, const string &sk);
virtual int get(const char *name, string &value, const char *sk) {
return get(string(name), value, sk ? string(sk) : string(""));
}
};
/**
* Use several config files, trying to get values from each in order. Used to
* have a central config, with possible overrides from more specific
* (ie personal) ones.
*
* Notes: it's ok for some of the files in the list to not exist, but the last
* one must or we generate an error. We open all trees readonly.
*/
template <class T> class ConfStack {
public:
ConfStack(const std::list<string> &fns, bool ro = true) {
construct(fns, ro);
}
ConfStack(const char *nm, bool ro = true) {
std::list<string> fns;
fns.push_back(string(nm));
construct(fns, ro);
}
~ConfStack() {
erase();
m_ok = false;
}
ConfStack(const ConfStack &rhs) {
init_from(rhs);
}
ConfStack& operator=(const ConfStack &rhs) {
if (this != &rhs){
erase();
m_ok = rhs.m_ok;
if (m_ok)
init_from(rhs);
}
return *this;
}
int get(const std::string &name, string &value, const string &sk) {
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->get(name, value, sk))
return true;
}
return false;
}
int get(const char *name, string &value, const char *sk) {
return get(string(name), value, sk ? string(sk) : string(""));
}
std::list<string> getNames(const string &sk) {
std::list<string> nms;
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
std::list<string> lst;
lst = (*it)->getNames(sk);
nms.splice(nms.end(), lst);
}
return nms;
}
bool ok() {return m_ok;}
private:
bool m_ok;
std::list<T*> m_confs;
void erase() {
typename std::list<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
delete (*it);
}
m_confs.clear();
}
/// Common code to initialize from existing object
void init_from(const ConfStack &rhs) {
if ((m_ok = rhs.m_ok)) {
typename std::list<T*>::const_iterator it;
for (it = rhs.m_confs.begin();it != rhs.m_confs.end();it++) {
m_confs.push_back(new T(**it));
}
}
}
/// Common constructor code
void construct(const std::list<string> &fns, bool ro) {
if (!ro) {
m_ok = false;
return;
}
std::list<std::string>::const_iterator it;
bool lastok = false;
for (it = fns.begin();it != fns.end();it++) {
T* p = new T(it->c_str(), true);
if (p && p->ok()) {
m_confs.push_back(p);
lastok = true;
} else
lastok = false;
}
m_ok = lastok;
}
};
#endif /*_CONFTREE_H_ */