|
a/src/utils/conftree.h |
|
b/src/utils/conftree.h |
|
... |
|
... |
21 |
* A simple configuration file implementation.
|
21 |
* A simple configuration file implementation.
|
22 |
*
|
22 |
*
|
23 |
* Configuration files have lines like 'name = value', and/or like '[subkey]'
|
23 |
* Configuration files have lines like 'name = value', and/or like '[subkey]'
|
24 |
*
|
24 |
*
|
25 |
* Lines like '[subkeyname]' in the file define subsections, with independant
|
25 |
* Lines like '[subkeyname]' in the file define subsections, with independant
|
26 |
* configuration namespaces.
|
26 |
* configuration namespaces. Only subsections holding at least one variable are
|
|
|
27 |
* significant (empty subsections may be deleted during an update, but
|
|
|
28 |
not always).
|
27 |
*
|
29 |
*
|
28 |
* Whitespace around name and value is insignificant.
|
30 |
* Whitespace around name and value is insignificant.
|
29 |
*
|
31 |
*
|
30 |
* The names are case-sensitive but don't count on it.
|
32 |
* The names are case-sensitive but don't count on it.
|
31 |
*
|
33 |
*
|
|
... |
|
... |
46 |
// rh7.3 likes iostream better...
|
48 |
// rh7.3 likes iostream better...
|
47 |
#if defined(__GNUC__) && __GNUC__ < 3
|
49 |
#if defined(__GNUC__) && __GNUC__ < 3
|
48 |
#include <iostream>
|
50 |
#include <iostream>
|
49 |
#else
|
51 |
#else
|
50 |
#include <istream>
|
52 |
#include <istream>
|
|
|
53 |
#include <ostream>
|
51 |
#endif
|
54 |
#endif
|
52 |
|
55 |
|
53 |
#ifndef NO_NAMESPACES
|
56 |
#ifndef NO_NAMESPACES
|
54 |
using std::string;
|
57 |
using std::string;
|
55 |
using std::list;
|
58 |
using std::list;
|
56 |
using std::map;
|
59 |
using std::map;
|
57 |
using std::istream;
|
60 |
using std::istream;
|
|
|
61 |
using std::ostream;
|
58 |
#endif // NO_NAMESPACES
|
62 |
#endif // NO_NAMESPACES
|
59 |
|
63 |
|
60 |
#include "pathut.h"
|
64 |
#include "pathut.h"
|
|
|
65 |
|
|
|
66 |
/** Internal class used for storing presentation information */
|
|
|
67 |
class ConfLine {
|
|
|
68 |
public:
|
|
|
69 |
enum Kind {CFL_COMMENT, CFL_SK, CFL_VAR};
|
|
|
70 |
Kind m_kind;
|
|
|
71 |
string m_data;
|
|
|
72 |
ConfLine(Kind k, const string& d)
|
|
|
73 |
: m_kind(k), m_data(d)
|
|
|
74 |
{
|
|
|
75 |
}
|
|
|
76 |
bool operator==(const ConfLine& o)
|
|
|
77 |
{
|
|
|
78 |
return o.m_kind == m_kind && o.m_data == m_data;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
};
|
61 |
|
82 |
|
62 |
/**
|
83 |
/**
|
63 |
* Manages a simple configuration file with subsections.
|
84 |
* Manages a simple configuration file with subsections.
|
64 |
*/
|
85 |
*/
|
65 |
class ConfSimple {
|
86 |
class ConfSimple {
|
|
... |
|
... |
133 |
/**
|
154 |
/**
|
134 |
* Return all names in given submap
|
155 |
* Return all names in given submap
|
135 |
*/
|
156 |
*/
|
136 |
virtual list<string> getNames(const string &sk);
|
157 |
virtual list<string> getNames(const string &sk);
|
137 |
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Return all subkeys
|
|
|
161 |
*/
|
|
|
162 |
virtual list<string> getSubKeys();
|
|
|
163 |
|
138 |
virtual string getFilename() {return filename;}
|
164 |
virtual string getFilename() {return m_filename;}
|
139 |
|
165 |
|
140 |
/**
|
166 |
/**
|
141 |
* Copy constructor. Expensive but less so than a full rebuild
|
167 |
* Copy constructor. Expensive but less so than a full rebuild
|
142 |
*/
|
168 |
*/
|
143 |
ConfSimple(const ConfSimple &rhs) : data(0) {
|
169 |
ConfSimple(const ConfSimple &rhs)
|
|
|
170 |
: m_data(0)
|
|
|
171 |
{
|
144 |
if ((status = rhs.status) == STATUS_ERROR)
|
172 |
if ((status = rhs.status) == STATUS_ERROR)
|
145 |
return;
|
173 |
return;
|
146 |
filename = rhs.filename;
|
174 |
m_filename = rhs.m_filename;
|
147 |
// Note: we just share the pointer, this doesnt belong to us
|
175 |
// Note: we just share the pointer, this doesnt belong to us
|
148 |
data = rhs.data;
|
176 |
m_data = rhs.m_data;
|
149 |
submaps = rhs.submaps;
|
177 |
m_submaps = rhs.m_submaps;
|
150 |
}
|
178 |
}
|
151 |
|
179 |
|
152 |
/**
|
180 |
/**
|
153 |
* Assignement. This is expensive
|
181 |
* Assignement. This is expensive
|
154 |
*/
|
182 |
*/
|
155 |
ConfSimple& operator=(const ConfSimple &rhs) {
|
183 |
ConfSimple& operator=(const ConfSimple &rhs)
|
|
|
184 |
{
|
156 |
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
|
185 |
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
|
157 |
filename = rhs.filename;
|
186 |
m_filename = rhs.m_filename;
|
158 |
// Note: we don't own data. Just share the pointer
|
187 |
// Note: we don't own data. Just share the pointer
|
159 |
data = rhs.data;
|
188 |
m_data = rhs.m_data;
|
160 |
submaps = rhs.submaps;
|
189 |
m_submaps = rhs.m_submaps;
|
161 |
}
|
190 |
}
|
162 |
return *this;
|
191 |
return *this;
|
163 |
}
|
192 |
}
|
164 |
|
193 |
|
165 |
protected:
|
194 |
protected:
|
166 |
bool dotildexpand;
|
195 |
bool dotildexpand;
|
167 |
StatusCode status;
|
196 |
StatusCode status;
|
168 |
private:
|
197 |
private:
|
169 |
string filename; // set if we're working with a file
|
198 |
// Set if we're working with a file
|
|
|
199 |
string m_filename;
|
170 |
string *data; // set if we're working with an in-memory string
|
200 |
// Set if we're working with an in-memory string
|
|
|
201 |
string *m_data;
|
|
|
202 |
// Configuration data submaps (one per subkey, the main data has a
|
|
|
203 |
// null subkey)
|
171 |
map<string, map<string, string> > submaps;
|
204 |
map<string, map<string, string> > m_submaps;
|
|
|
205 |
// Presentation data. We keep the comments, empty lines and
|
|
|
206 |
// variable and subkey ordering information in there (for
|
|
|
207 |
// rewriting the file while keeping hand-edited information)
|
|
|
208 |
list<ConfLine> m_order;
|
172 |
|
209 |
|
173 |
void parseinput(istream &input);
|
210 |
void parseinput(istream& input);
|
|
|
211 |
bool write();
|
|
|
212 |
bool write(ostream& out);
|
|
|
213 |
// Internal version of set: no RW checking
|
|
|
214 |
virtual int i_set(const string &nm, const string &val,
|
|
|
215 |
const string &sk, bool init = false);
|
174 |
};
|
216 |
};
|
175 |
|
217 |
|
176 |
/**
|
218 |
/**
|
177 |
* This is a configuration class which attaches tree-like signification to the
|
219 |
* This is a configuration class which attaches tree-like signification to the
|
178 |
* submap names.
|
220 |
* submap names.
|