conftree: astyle + merge with upmpdcli version

This commit is contained in:
Jean-Francois Dockes 2016-02-22 17:27:06 +01:00
parent 111dd7a7e5
commit 3f3b3a15c7
2 changed files with 828 additions and 763 deletions

View file

@ -18,22 +18,24 @@
#ifndef TEST_CONFTREE #ifndef TEST_CONFTREE
#include "conftree.h"
#include <ctype.h> #include <ctype.h>
#include <fnmatch.h> #include <fnmatch.h>
#include "safesysstat.h" #include "safesysstat.h"
#include <fstream>
#include <sstream>
#include <algorithm> #include <algorithm>
#include <iostream>
#include <cstring> #include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
using namespace std;
#include "conftree.h"
#include "pathut.h" #include "pathut.h"
#include "smallut.h" #include "smallut.h"
using namespace std;
#undef DEBUG #undef DEBUG
#ifdef DEBUG #ifdef DEBUG
#define LOGDEB(X) fprintf X #define LOGDEB(X) fprintf X
@ -76,17 +78,19 @@ void ConfSimple::parseinput(istream &input)
} }
} }
if (appending) if (appending) {
line += cline; line += cline;
else } else {
line = cline; line = cline;
}
// Note that we trim whitespace before checking for backslash-eol // Note that we trim whitespace before checking for backslash-eol
// This avoids invisible whitespace problems. // This avoids invisible whitespace problems.
trimstring(line); trimstring(line);
if (line.empty() || line.at(0) == '#') { if (line.empty() || line.at(0) == '#') {
if (eof) if (eof) {
break; break;
}
m_order.push_back(ConfLine(ConfLine::CFL_COMMENT, line)); m_order.push_back(ConfLine(ConfLine::CFL_COMMENT, line));
continue; continue;
} }
@ -99,10 +103,13 @@ void ConfSimple::parseinput(istream &input)
if (line[0] == '[') { if (line[0] == '[') {
trimstring(line, "[]"); trimstring(line, "[]");
if (dotildexpand) if (dotildexpand) {
submapkey = path_tildexpand(line); submapkey = path_tildexpand(line);
else } else {
submapkey = line; submapkey = line;
}
m_subkeys_unsorted.push_back(submapkey);
// No need for adding sk to order, will be done with first // No need for adding sk to order, will be done with first
// variable insert. Also means that empty section are // variable insert. Also means that empty section are
// expandable (won't be output when rewriting) // expandable (won't be output when rewriting)
@ -130,10 +137,11 @@ void ConfSimple::parseinput(istream &input)
continue; continue;
} }
i_set(nm, val, submapkey, true); i_set(nm, val, submapkey, true);
if (eof) if (eof) {
break; break;
} }
} }
}
ConfSimple::ConfSimple(int readonly, bool tildexp) ConfSimple::ConfSimple(int readonly, bool tildexp)
@ -198,9 +206,12 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
ConfSimple::StatusCode ConfSimple::getStatus() const ConfSimple::StatusCode ConfSimple::getStatus() const
{ {
switch (status) { switch (status) {
case STATUS_RO: return STATUS_RO; case STATUS_RO:
case STATUS_RW: return STATUS_RW; return STATUS_RO;
default: return STATUS_ERROR; case STATUS_RW:
return STATUS_RW;
default:
return STATUS_ERROR;
} }
} }
@ -223,8 +234,9 @@ bool ConfSimple::i_changed(bool upd)
struct stat st; struct stat st;
if (stat(m_filename.c_str(), &st) == 0) { if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) { if (m_fmtime != st.st_mtime) {
if (upd) if (upd) {
m_fmtime = st.st_mtime; m_fmtime = st.st_mtime;
}
return true; return true;
} }
} }
@ -234,18 +246,21 @@ bool ConfSimple::i_changed(bool upd)
int ConfSimple::get(const string& nm, string& value, const string& sk) const int ConfSimple::get(const string& nm, string& value, const string& sk) const
{ {
if (!ok()) if (!ok()) {
return 0; return 0;
}
// Find submap // Find submap
map<string, map<string, string> >::const_iterator ss; map<string, map<string, string> >::const_iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
return 0; return 0;
}
// Find named value // Find named value
map<string, string>::const_iterator s; map<string, string>::const_iterator s;
if ((s = ss->second.find(nm)) == ss->second.end()) if ((s = ss->second.find(nm)) == ss->second.end()) {
return 0; return 0;
}
value = s->second; value = s->second;
return 1; return 1;
} }
@ -298,12 +313,14 @@ static ConfSimple::WalkerCode varprinter(void *f, const string &nm,
int ConfSimple::set(const std::string& nm, const std::string& value, int ConfSimple::set(const std::string& nm, const std::string& value,
const string& sk) const string& sk)
{ {
if (status != STATUS_RW) if (status != STATUS_RW) {
return 0; return 0;
}
LOGDEB((stderr, "ConfSimple::set [%s]:[%s] -> [%s]\n", sk.c_str(), LOGDEB((stderr, "ConfSimple::set [%s]:[%s] -> [%s]\n", sk.c_str(),
nm.c_str(), value.c_str())); nm.c_str(), value.c_str()));
if (!i_set(nm, value, sk)) if (!i_set(nm, value, sk)) {
return 0; return 0;
}
return write(); return write();
} }
@ -389,8 +406,9 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
fin = m_order.end(); fin = m_order.end();
if (start != m_order.end()) { if (start != m_order.end()) {
// The null subkey has no entry (maybe it should) // The null subkey has no entry (maybe it should)
if (!sk.empty()) if (!sk.empty()) {
start++; start++;
}
for (vector<ConfLine>::iterator it = start; it != m_order.end(); it++) { for (vector<ConfLine>::iterator it = start; it != m_order.end(); it++) {
if (it->m_kind == ConfLine::CFL_SK) { if (it->m_kind == ConfLine::CFL_SK) {
fin = it; fin = it;
@ -409,8 +427,9 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
int ConfSimple::erase(const string& nm, const string& sk) int ConfSimple::erase(const string& nm, const string& sk)
{ {
if (status != STATUS_RW) if (status != STATUS_RW) {
return 0; return 0;
}
map<string, map<string, string> >::iterator ss; map<string, map<string, string> >::iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) { if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
@ -438,8 +457,9 @@ ConfSimple::WalkerCode
ConfSimple::sortwalk(WalkerCode(*walker)(void *, const string&, const string&), ConfSimple::sortwalk(WalkerCode(*walker)(void *, const string&, const string&),
void *clidata) const void *clidata) const
{ {
if (!ok()) if (!ok()) {
return WALK_STOP; return WALK_STOP;
}
// For all submaps: // For all submaps:
for (map<string, map<string, string> >::const_iterator sit = for (map<string, map<string, string> >::const_iterator sit =
m_submaps.begin(); m_submaps.begin();
@ -447,17 +467,19 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
// Possibly emit submap name: // Possibly emit submap name:
if (!sit->first.empty() && walker(clidata, string(), sit->first.c_str()) if (!sit->first.empty() && walker(clidata, string(), sit->first.c_str())
== WALK_STOP) == WALK_STOP) {
return WALK_STOP; return WALK_STOP;
}
// Walk submap // Walk submap
const map<string, string>& sm = sit->second; const map<string, string>& sm = sit->second;
for (map<string, string>::const_iterator it = sm.begin(); it != sm.end(); for (map<string, string>::const_iterator it = sm.begin(); it != sm.end();
it++) { it++) {
if (walker(clidata, it->first, it->second) == WALK_STOP) if (walker(clidata, it->first, it->second) == WALK_STOP) {
return WALK_STOP; return WALK_STOP;
} }
} }
}
return WALK_CONTINUE; return WALK_CONTINUE;
} }
@ -465,14 +487,17 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
// a file // a file
bool ConfSimple::write() bool ConfSimple::write()
{ {
if (!ok()) if (!ok()) {
return false; return false;
if (m_holdWrites) }
if (m_holdWrites) {
return true; return true;
}
if (m_filename.length()) { if (m_filename.length()) {
ofstream output(m_filename.c_str(), ios::out | ios::trunc); ofstream output(m_filename.c_str(), ios::out | ios::trunc);
if (!output.is_open()) if (!output.is_open()) {
return 0; return 0;
}
return write(output); return write(output);
} else { } else {
// No backing store, no writing. Maybe one day we'll need it with // No backing store, no writing. Maybe one day we'll need it with
@ -488,16 +513,18 @@ bool ConfSimple::write()
// lets ie: showall work even when holdWrites is set // lets ie: showall work even when holdWrites is set
bool ConfSimple::write(ostream& out) const bool ConfSimple::write(ostream& out) const
{ {
if (!ok()) if (!ok()) {
return false; return false;
}
string sk; string sk;
for (vector<ConfLine>::const_iterator it = m_order.begin(); for (vector<ConfLine>::const_iterator it = m_order.begin();
it != m_order.end(); it++) { it != m_order.end(); it++) {
switch (it->m_kind) { switch (it->m_kind) {
case ConfLine::CFL_COMMENT: case ConfLine::CFL_COMMENT:
out << it->m_data << endl; out << it->m_data << endl;
if (!out.good()) if (!out.good()) {
return false; return false;
}
break; break;
case ConfLine::CFL_SK: case ConfLine::CFL_SK:
sk = it->m_data; sk = it->m_data;
@ -506,9 +533,10 @@ bool ConfSimple::write(ostream& out) const
// does // does
if (m_submaps.find(sk) != m_submaps.end()) { if (m_submaps.find(sk) != m_submaps.end()) {
out << "[" << it->m_data << "]" << endl; out << "[" << it->m_data << "]" << endl;
if (!out.good()) if (!out.good()) {
return false; return false;
} }
}
break; break;
case ConfLine::CFL_VAR: case ConfLine::CFL_VAR:
string nm = it->m_data; string nm = it->m_data;
@ -522,8 +550,9 @@ bool ConfSimple::write(ostream& out) const
string value; string value;
if (ConfSimple::get(nm, value, sk)) { if (ConfSimple::get(nm, value, sk)) {
varprinter(&out, nm, value); varprinter(&out, nm, value);
if (!out.good()) if (!out.good()) {
return false; return false;
}
break; break;
} }
LOGDEB((stderr, "ConfSimple::write: no value: nm[%s] sk[%s]\n", LOGDEB((stderr, "ConfSimple::write: no value: nm[%s] sk[%s]\n",
@ -536,16 +565,18 @@ bool ConfSimple::write(ostream& out) const
void ConfSimple::showall() const void ConfSimple::showall() const
{ {
if (!ok()) if (!ok()) {
return; return;
}
write(std::cout); write(std::cout);
} }
vector<string> ConfSimple::getNames(const string& sk, const char *pattern) const vector<string> ConfSimple::getNames(const string& sk, const char *pattern) const
{ {
vector<string> mylist; vector<string> mylist;
if (!ok()) if (!ok()) {
return mylist; return mylist;
}
map<string, map<string, string> >::const_iterator ss; map<string, map<string, string> >::const_iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) { if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
return mylist; return mylist;
@ -553,8 +584,9 @@ vector<string> ConfSimple::getNames(const string &sk, const char *pattern) const
mylist.reserve(ss->second.size()); mylist.reserve(ss->second.size());
map<string, string>::const_iterator it; map<string, string>::const_iterator it;
for (it = ss->second.begin(); it != ss->second.end(); it++) { for (it = ss->second.begin(); it != ss->second.end(); it++) {
if (pattern && 0 != fnmatch(pattern, it->first.c_str(), 0)) if (pattern && 0 != fnmatch(pattern, it->first.c_str(), 0)) {
continue; continue;
}
mylist.push_back(it->first); mylist.push_back(it->first);
} }
return mylist; return mylist;
@ -563,8 +595,9 @@ vector<string> ConfSimple::getNames(const string &sk, const char *pattern) const
vector<string> ConfSimple::getSubKeys() const vector<string> ConfSimple::getSubKeys() const
{ {
vector<string> mylist; vector<string> mylist;
if (!ok()) if (!ok()) {
return mylist; return mylist;
}
mylist.reserve(m_submaps.size()); mylist.reserve(m_submaps.size());
map<string, map<string, string> >::const_iterator ss; map<string, map<string, string> >::const_iterator ss;
for (ss = m_submaps.begin(); ss != m_submaps.end(); ss++) { for (ss = m_submaps.begin(); ss != m_submaps.end(); ss++) {
@ -579,9 +612,10 @@ bool ConfSimple::hasNameAnywhere(const string& nm) const
for (vector<string>::const_iterator it = keys.begin(); for (vector<string>::const_iterator it = keys.begin();
it != keys.end(); it++) { it != keys.end(); it++) {
string val; string val;
if (get(nm, val, *it)) if (get(nm, val, *it)) {
return true; return true;
} }
}
return false; return false;
} }
@ -593,7 +627,8 @@ int ConfTree::get(const std::string &name, string &value, const string &sk)
const const
{ {
if (sk.empty() || !path_isabsolute(sk)) { if (sk.empty() || !path_isabsolute(sk)) {
// LOGDEB((stderr, "ConfTree::get: looking in global space for sk [%s]\n", sk.c_str())); // LOGDEB((stderr, "ConfTree::get: looking in global space for [%s]\n",
// sk.c_str()));
return ConfSimple::get(name, value, sk); return ConfSimple::get(name, value, sk);
} }
@ -608,16 +643,17 @@ int ConfTree::get(const std::string &name, string &value, const string &sk)
for (;;) { for (;;) {
// LOGDEB((stderr,"ConfTree::get: looking for '%s' in '%s'\n", // LOGDEB((stderr,"ConfTree::get: looking for '%s' in '%s'\n",
// name.c_str(), msk.c_str())); // name.c_str(), msk.c_str()));
if (ConfSimple::get(name, value, msk)) if (ConfSimple::get(name, value, msk)) {
return 1; return 1;
}
string::size_type pos = msk.rfind("/"); string::size_type pos = msk.rfind("/");
if (pos != string::npos) { if (pos != string::npos) {
msk.replace(pos, string::npos, string()); msk.replace(pos, string::npos, string());
} else { } else {
#ifdef _WIN32 #ifdef _WIN32
if (msk.size() == 2 && isalpha(msk[0]) && msk[1] == ':') if (msk.size() == 2 && isalpha(msk[0]) && msk[1] == ':') {
msk.clear(); msk.clear();
else } else
#endif #endif
break; break;
} }
@ -716,10 +752,11 @@ bool complex_updates(const string& fn)
ConfSimple::WalkerCode mywalker(void *, const string& nm, const string& value) ConfSimple::WalkerCode mywalker(void *, const string& nm, const string& value)
{ {
if (nm.empty()) if (nm.empty()) {
printf("\n[%s]\n", value.c_str()); printf("\n[%s]\n", value.c_str());
else } else {
printf("'%s' -> '%s'\n", nm.c_str(), value.c_str()); printf("'%s' -> '%s'\n", nm.c_str(), value.c_str());
}
return ConfSimple::WALK_CONTINUE; return ConfSimple::WALK_CONTINUE;
} }
@ -858,7 +895,8 @@ static char usage [] =
"-U : complex update test. Will erase the named file parameter\n" "-U : complex update test. Will erase the named file parameter\n"
; ;
void Usage() { void Usage()
{
fprintf(stderr, "%s:%s\n", thisprog, usage); fprintf(stderr, "%s:%s\n", thisprog, usage);
exit(1); exit(1);
} }
@ -882,75 +920,109 @@ int main(int argc, char **argv)
const char *value = 0; const char *value = 0;
thisprog = argv[0]; thisprog = argv[0];
argc--; argv++; argc--;
argv++;
while (argc > 0 && **argv == '-') { while (argc > 0 && **argv == '-') {
(*argv)++; (*argv)++;
if (!(**argv)) if (!(**argv))
/* Cas du "adb - core" */ /* Cas du "adb - core" */
{
Usage(); Usage();
}
while (**argv) while (**argv)
switch (*(*argv)++) { switch (*(*argv)++) {
case 'a': case 'a':
op_flags |= OPT_a; op_flags |= OPT_a;
if (argc < 4) if (argc < 4) {
Usage(); Usage();
nm = *(++argv);argc--; }
value = *(++argv);argc--; nm = *(++argv);
sub = *(++argv);argc--; argc--;
value = *(++argv);
argc--;
sub = *(++argv);
argc--;
goto b1; goto b1;
case 'd': case 'd':
op_flags |= OPT_d; op_flags |= OPT_d;
if (argc < 3) if (argc < 3) {
Usage(); Usage();
nm = *(++argv);argc--; }
sub = *(++argv);argc--; nm = *(++argv);
argc--;
sub = *(++argv);
argc--;
goto b1; goto b1;
case 'E': case 'E':
op_flags |= OPT_E; op_flags |= OPT_E;
if (argc < 2) if (argc < 2) {
Usage(); Usage();
sub = *(++argv);argc--; }
sub = *(++argv);
argc--;
goto b1; goto b1;
case 'k': op_flags |= OPT_k; break; case 'k':
op_flags |= OPT_k;
break;
case 'q': case 'q':
op_flags |= OPT_q; op_flags |= OPT_q;
if (argc < 3) if (argc < 3) {
Usage(); Usage();
nm = *(++argv);argc--;
sub = *(++argv);argc--;
goto b1;
case 's': op_flags |= OPT_s; break;
case 'S': op_flags |= OPT_S; break;
case 'V': op_flags |= OPT_V; break;
case 'U': op_flags |= OPT_U; break;
case 'w': op_flags |= OPT_w; break;
default: Usage(); break;
} }
b1: argc--; argv++; nm = *(++argv);
argc--;
sub = *(++argv);
argc--;
goto b1;
case 's':
op_flags |= OPT_s;
break;
case 'S':
op_flags |= OPT_S;
break;
case 'V':
op_flags |= OPT_V;
break;
case 'U':
op_flags |= OPT_U;
break;
case 'w':
op_flags |= OPT_w;
break;
default:
Usage();
break;
}
b1:
argc--;
argv++;
} }
if ((op_flags & OPT_S)) { if ((op_flags & OPT_S)) {
// String storage test // String storage test
if (argc != 0) if (argc != 0) {
Usage(); Usage();
}
string s; string s;
ConfSimple c(s); ConfSimple c(s);
memtest(c); memtest(c);
exit(0); exit(0);
} else if ((op_flags & OPT_V)) { } else if ((op_flags & OPT_V)) {
// No storage test // No storage test
if (argc != 0) if (argc != 0) {
Usage(); Usage();
}
ConfSimple c; ConfSimple c;
memtest(c); memtest(c);
exit(0); exit(0);
} }
// Other tests use file(s) as backing store // Other tests use file(s) as backing store
if (argc < 1) if (argc < 1) {
Usage(); Usage();
}
if (op_flags & OPT_U) { if (op_flags & OPT_U) {
exit(!complex_updates(argv[0])); exit(!complex_updates(argv[0]));
@ -1028,14 +1100,16 @@ int main(int argc, char **argv)
printf("\nNAMES in global space:\n"); printf("\nNAMES in global space:\n");
vector<string> names = conf->getNames(""); vector<string> names = conf->getNames("");
for (vector<string>::iterator it = names.begin(); for (vector<string>::iterator it = names.begin();
it!=names.end(); it++) it != names.end(); it++) {
cout << *it << " "; cout << *it << " ";
}
cout << endl; cout << endl;
printf("\nNAMES in global space matching t* \n"); printf("\nNAMES in global space matching t* \n");
names = conf->getNames("", "t*"); names = conf->getNames("", "t*");
for (vector<string>::iterator it = names.begin(); for (vector<string>::iterator it = names.begin();
it!=names.end(); it++) it != names.end(); it++) {
cout << *it << " "; cout << *it << " ";
}
cout << endl; cout << endl;
} }
} }

View file

@ -49,10 +49,10 @@
* (useful to have central/personal config files) * (useful to have central/personal config files)
*/ */
#include <string>
#include <map>
#include <vector>
#include <algorithm> #include <algorithm>
#include <map>
#include <string>
#include <vector>
// rh7.3 likes iostream better... // rh7.3 likes iostream better...
#if defined(__GNUC__) && __GNUC__ < 3 #if defined(__GNUC__) && __GNUC__ < 3
@ -62,15 +62,13 @@
#include <ostream> #include <ostream>
#endif #endif
#ifndef NO_NAMESPACES #include "pathut.h"
using std::string; using std::string;
using std::vector; using std::vector;
using std::map; using std::map;
using std::istream; using std::istream;
using std::ostream; using std::ostream;
#endif // NO_NAMESPACES
#include "pathut.h"
/** Internal class used for storing presentation information */ /** Internal class used for storing presentation information */
class ConfLine { class ConfLine {
@ -79,11 +77,9 @@ public:
Kind m_kind; Kind m_kind;
string m_data; string m_data;
ConfLine(Kind k, const string& d) ConfLine(Kind k, const string& d)
: m_kind(k), m_data(d) : m_kind(k), m_data(d) {
{
} }
bool operator==(const ConfLine& o) bool operator==(const ConfLine& o) {
{
return o.m_kind == m_kind && o.m_data == m_data; return o.m_kind == m_kind && o.m_data == m_data;
} }
}; };
@ -149,21 +145,20 @@ public:
* Decide if we actually rewrite the backing-store after modifying the * Decide if we actually rewrite the backing-store after modifying the
* tree. * tree.
*/ */
virtual bool holdWrites(bool on) virtual bool holdWrites(bool on) {
{
m_holdWrites = on; m_holdWrites = on;
if (on == false) { if (on == false) {
return write(); return write();
} else } else {
return true; return true;
} }
}
/** Clear, then reparse from string */ /** Clear, then reparse from string */
void reparse(const string& in); void reparse(const string& in);
/** Clear all content */ /** Clear all content */
void clear() void clear() {
{
m_submaps.clear(); m_submaps.clear();
m_order.clear(); m_order.clear();
} }
@ -194,7 +189,9 @@ public:
virtual int eraseKey(const string& sk); virtual int eraseKey(const string& sk);
virtual StatusCode getStatus() const; virtual StatusCode getStatus() const;
virtual bool ok() const {return getStatus() != STATUS_ERROR;} virtual bool ok() const {
return getStatus() != STATUS_ERROR;
}
/** /**
* Walk the configuration values, calling function for each. * Walk the configuration values, calling function for each.
@ -223,28 +220,30 @@ public:
/** /**
* Return all subkeys * Return all subkeys
*/ */
virtual vector<string> getSubKeys(bool) const virtual vector<string> getSubKeys(bool) const {
{
return getSubKeys(); return getSubKeys();
} }
virtual vector<string> getSubKeys_unsorted(bool = false) const {
return m_subkeys_unsorted;
}
virtual vector<string> getSubKeys() const; virtual vector<string> getSubKeys() const;
/** Test for subkey existence */ /** Test for subkey existence */
virtual bool hasSubKey(const string& sk) const virtual bool hasSubKey(const string& sk) const {
{
return m_submaps.find(sk) != m_submaps.end(); return m_submaps.find(sk) != m_submaps.end();
} }
virtual string getFilename() const virtual string getFilename() const {
{return m_filename;} return m_filename;
}
/** /**
* Copy constructor. Expensive but less so than a full rebuild * Copy constructor. Expensive but less so than a full rebuild
*/ */
ConfSimple(const ConfSimple& rhs) ConfSimple(const ConfSimple& rhs)
: ConfNull() : ConfNull() {
{ if ((status = rhs.status) == STATUS_ERROR) {
if ((status = rhs.status) == STATUS_ERROR)
return; return;
}
m_filename = rhs.m_filename; m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps; m_submaps = rhs.m_submaps;
} }
@ -252,8 +251,7 @@ public:
/** /**
* Assignement. This is expensive * Assignement. This is expensive
*/ */
ConfSimple& operator=(const ConfSimple &rhs) ConfSimple& operator=(const ConfSimple& rhs) {
{
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) { if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
m_filename = rhs.m_filename; m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps; m_submaps = rhs.m_submaps;
@ -276,6 +274,7 @@ private:
// Configuration data submaps (one per subkey, the main data has a // Configuration data submaps (one per subkey, the main data has a
// null subkey) // null subkey)
map<string, map<string, string> > m_submaps; map<string, map<string, string> > m_submaps;
vector<string> m_subkeys_unsorted;
// Presentation data. We keep the comments, empty lines and // Presentation data. We keep the comments, empty lines and
// variable and subkey ordering information in there (for // variable and subkey ordering information in there (for
// rewriting the file while keeping hand-edited information) // rewriting the file while keeping hand-edited information)
@ -321,8 +320,7 @@ public:
: ConfSimple(readonly, true) {} : ConfSimple(readonly, true) {}
virtual ~ConfTree() {}; virtual ~ConfTree() {};
ConfTree(const ConfTree& r) : ConfSimple(r) {}; ConfTree(const ConfTree& r) : ConfSimple(r) {};
ConfTree& operator=(const ConfTree& r) ConfTree& operator=(const ConfTree& r) {
{
ConfSimple::operator=(r); ConfSimple::operator=(r);
return *this; return *this;
} }
@ -351,13 +349,11 @@ public:
/// Construct from configuration file names. The earler /// Construct from configuration file names. The earler
/// files in have priority when fetching values. Only the first /// files in have priority when fetching values. Only the first
/// file will be updated if ro is false and set() is used. /// file will be updated if ro is false and set() is used.
ConfStack(const vector<string> &fns, bool ro = true) ConfStack(const vector<string>& fns, bool ro = true) {
{
construct(fns, ro); construct(fns, ro);
} }
/// Construct out of single file name and multiple directories /// Construct out of single file name and multiple directories
ConfStack(const string& nm, const vector<string>& dirs, bool ro = true) ConfStack(const string& nm, const vector<string>& dirs, bool ro = true) {
{
vector<string> fns; vector<string> fns;
for (vector<string>::const_iterator it = dirs.begin(); for (vector<string>::const_iterator it = dirs.begin();
it != dirs.end(); it++) { it != dirs.end(); it++) {
@ -367,69 +363,68 @@ public:
} }
ConfStack(const ConfStack& rhs) ConfStack(const ConfStack& rhs)
: ConfNull() : ConfNull() {
{
init_from(rhs); init_from(rhs);
} }
virtual ~ConfStack() virtual ~ConfStack() {
{
clear(); clear();
m_ok = false; m_ok = false;
} }
ConfStack& operator=(const ConfStack &rhs) ConfStack& operator=(const ConfStack& rhs) {
{
if (this != &rhs) { if (this != &rhs) {
clear(); clear();
m_ok = rhs.m_ok; m_ok = rhs.m_ok;
if (m_ok) if (m_ok) {
init_from(rhs); init_from(rhs);
} }
}
return *this; return *this;
} }
virtual bool sourceChanged() const virtual bool sourceChanged() const {
{
typename vector<T*>::const_iterator it; typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) { for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->sourceChanged()) if ((*it)->sourceChanged()) {
return true; return true;
} }
}
return false; return false;
} }
virtual int get(const string& name, string& value, const string& sk, virtual int get(const string& name, string& value, const string& sk,
bool shallow) const bool shallow) const {
{
typename vector<T*>::const_iterator it; typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) { for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->get(name, value, sk)) if ((*it)->get(name, value, sk)) {
return true; return true;
if (shallow) }
if (shallow) {
break; break;
} }
}
return false; return false;
} }
virtual int get(const string& name, string& value, const string& sk) const { virtual int get(const string& name, string& value, const string& sk) const {
return get(name, value, sk, false); return get(name, value, sk, false);
} }
virtual bool hasNameAnywhere(const string& nm) const virtual bool hasNameAnywhere(const string& nm) const {
{
typename vector<T*>::const_iterator it; typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) { for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasNameAnywhere(nm)) if ((*it)->hasNameAnywhere(nm)) {
return true; return true;
} }
}
return false; return false;
} }
virtual int set(const string& nm, const string& val, virtual int set(const string& nm, const string& val,
const string &sk = string()) const string& sk = string()) {
{ if (!m_ok) {
if (!m_ok)
return 0; return 0;
}
//LOGDEB2(("ConfStack::set [%s]:[%s] -> [%s]\n", sk.c_str(), //LOGDEB2(("ConfStack::set [%s]:[%s] -> [%s]\n", sk.c_str(),
//nm.c_str(), val.c_str())); //nm.c_str(), val.c_str()));
// Avoid adding unneeded entries: if the new value matches the // Avoid adding unneeded entries: if the new value matches the
@ -456,33 +451,27 @@ public:
return m_confs.front()->set(nm, val, sk); return m_confs.front()->set(nm, val, sk);
} }
virtual int erase(const string &nm, const string &sk) virtual int erase(const string& nm, const string& sk) {
{
return m_confs.front()->erase(nm, sk); return m_confs.front()->erase(nm, sk);
} }
virtual int eraseKey(const string &sk) virtual int eraseKey(const string& sk) {
{
return m_confs.front()->eraseKey(sk); return m_confs.front()->eraseKey(sk);
} }
virtual bool holdWrites(bool on) virtual bool holdWrites(bool on) {
{
return m_confs.front()->holdWrites(on); return m_confs.front()->holdWrites(on);
} }
virtual vector<string> getNames(const string& sk, const char *pattern = 0) virtual vector<string> getNames(const string& sk, const char *pattern = 0)
const const {
{
return getNames1(sk, pattern, false); return getNames1(sk, pattern, false);
} }
virtual vector<string> getNamesShallow(const string& sk, virtual vector<string> getNamesShallow(const string& sk,
const char *patt = 0) const const char *patt = 0) const {
{
return getNames1(sk, patt, true); return getNames1(sk, patt, true);
} }
virtual vector<string> getNames1(const string& sk, const char *pattern, virtual vector<string> getNames1(const string& sk, const char *pattern,
bool shallow) const bool shallow) const {
{
vector<string> nms; vector<string> nms;
typename vector<T*>::const_iterator it; typename vector<T*>::const_iterator it;
bool skfound = false; bool skfound = false;
@ -492,37 +481,39 @@ public:
vector<string> lst = (*it)->getNames(sk, pattern); vector<string> lst = (*it)->getNames(sk, pattern);
nms.insert(nms.end(), lst.begin(), lst.end()); nms.insert(nms.end(), lst.begin(), lst.end());
} }
if (shallow && skfound) if (shallow && skfound) {
break; break;
} }
}
sort(nms.begin(), nms.end()); sort(nms.begin(), nms.end());
vector<string>::iterator uit = unique(nms.begin(), nms.end()); vector<string>::iterator uit = unique(nms.begin(), nms.end());
nms.resize(uit - nms.begin()); nms.resize(uit - nms.begin());
return nms; return nms;
} }
virtual vector<string> getSubKeys() const virtual vector<string> getSubKeys() const {
{
return getSubKeys(false); return getSubKeys(false);
} }
virtual vector<string> getSubKeys(bool shallow) const virtual vector<string> getSubKeys(bool shallow) const {
{
vector<string> sks; vector<string> sks;
typename vector<T*>::const_iterator it; typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) { for (it = m_confs.begin(); it != m_confs.end(); it++) {
vector<string> lst; vector<string> lst;
lst = (*it)->getSubKeys(); lst = (*it)->getSubKeys();
sks.insert(sks.end(), lst.begin(), lst.end()); sks.insert(sks.end(), lst.begin(), lst.end());
if (shallow) if (shallow) {
break; break;
} }
}
sort(sks.begin(), sks.end()); sort(sks.begin(), sks.end());
vector<string>::iterator uit = unique(sks.begin(), sks.end()); vector<string>::iterator uit = unique(sks.begin(), sks.end());
sks.resize(uit - sks.begin()); sks.resize(uit - sks.begin());
return sks; return sks;
} }
virtual bool ok() const {return m_ok;} virtual bool ok() const {
return m_ok;
}
private: private:
bool m_ok; bool m_ok;