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
#include "conftree.h"
#include <ctype.h>
#include <fnmatch.h>
#include "safesysstat.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
using namespace std;
#include "conftree.h"
#include "pathut.h"
#include "smallut.h"
using namespace std;
#undef DEBUG
#ifdef DEBUG
#define LOGDEB(X) fprintf X
@ -41,7 +43,7 @@ using namespace std;
#define LOGDEB(X)
#endif
void ConfSimple::parseinput(istream &input)
void ConfSimple::parseinput(istream& input)
{
string submapkey;
string cline;
@ -71,22 +73,24 @@ void ConfSimple::parseinput(istream &input)
string::size_type pos = cline.find_last_not_of("\n\r");
if (pos == string::npos) {
cline.clear();
} else if (pos != cline.length()-1) {
cline.erase(pos+1);
} else if (pos != cline.length() - 1) {
cline.erase(pos + 1);
}
}
if (appending)
if (appending) {
line += cline;
else
} else {
line = cline;
}
// Note that we trim whitespace before checking for backslash-eol
// This avoids invisible whitespace problems.
trimstring(line);
if (line.empty() || line.at(0) == '#') {
if (eof)
if (eof) {
break;
}
m_order.push_back(ConfLine(ConfLine::CFL_COMMENT, line));
continue;
}
@ -99,10 +103,13 @@ void ConfSimple::parseinput(istream &input)
if (line[0] == '[') {
trimstring(line, "[]");
if (dotildexpand)
if (dotildexpand) {
submapkey = path_tildexpand(line);
else
} else {
submapkey = line;
}
m_subkeys_unsorted.push_back(submapkey);
// No need for adding sk to order, will be done with first
// variable insert. Also means that empty section are
// expandable (won't be output when rewriting)
@ -122,7 +129,7 @@ void ConfSimple::parseinput(istream &input)
string nm, val;
nm = line.substr(0, eqpos);
trimstring(nm);
val = line.substr(eqpos+1, string::npos);
val = line.substr(eqpos + 1, string::npos);
trimstring(val);
if (nm.length() == 0) {
@ -130,9 +137,10 @@ void ConfSimple::parseinput(istream &input)
continue;
}
i_set(nm, val, submapkey, true);
if (eof)
if (eof) {
break;
}
}
}
@ -167,7 +175,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
if (readonly) {
input.open(fname, ios::in);
} else {
ios::openmode mode = ios::in|ios::out;
ios::openmode mode = ios::in | ios::out;
// It seems that there is no separate 'create if not exists'
// open flag. Have to truncate to create, but dont want to do
// this to an existing file !
@ -198,9 +206,12 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
ConfSimple::StatusCode ConfSimple::getStatus() const
{
switch (status) {
case STATUS_RO: return STATUS_RO;
case STATUS_RW: return STATUS_RW;
default: return STATUS_ERROR;
case STATUS_RO:
return STATUS_RO;
case STATUS_RW:
return STATUS_RW;
default:
return STATUS_ERROR;
}
}
@ -223,8 +234,9 @@ bool ConfSimple::i_changed(bool upd)
struct stat st;
if (stat(m_filename.c_str(), &st) == 0) {
if (m_fmtime != st.st_mtime) {
if (upd)
if (upd) {
m_fmtime = st.st_mtime;
}
return true;
}
}
@ -232,20 +244,23 @@ bool ConfSimple::i_changed(bool upd)
return false;
}
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;
}
// Find submap
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;
}
// Find named value
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;
}
value = s->second;
return 1;
}
@ -264,8 +279,8 @@ int ConfSimple::get(const string &nm, string &value, const string &sk) const
// Note that the choice of break point does not affect the validity of
// the file data (when read back by conftree), only its ease of
// editing with a normal editor.
static ConfSimple::WalkerCode varprinter(void *f, const string &nm,
const string &value)
static ConfSimple::WalkerCode varprinter(void *f, const string& nm,
const string& value)
{
ostream& output = *((ostream *)f);
if (nm.empty()) {
@ -295,23 +310,25 @@ static ConfSimple::WalkerCode varprinter(void *f, const string &nm,
}
// Set variable and rewrite data
int ConfSimple::set(const std::string &nm, const std::string &value,
const string &sk)
int ConfSimple::set(const std::string& nm, const std::string& value,
const string& sk)
{
if (status != STATUS_RW)
if (status != STATUS_RW) {
return 0;
}
LOGDEB((stderr, "ConfSimple::set [%s]:[%s] -> [%s]\n", sk.c_str(),
nm.c_str(), value.c_str()));
if (!i_set(nm, value, sk))
if (!i_set(nm, value, sk)) {
return 0;
}
return write();
}
// Internal set variable: no rw checking or file rewriting. If init is
// set, we're doing initial parsing, else we are changing a parsed
// tree (changes the way we update the order data)
int ConfSimple::i_set(const std::string &nm, const std::string &value,
const string &sk, bool init)
int ConfSimple::i_set(const std::string& nm, const std::string& value,
const string& sk, bool init)
{
LOGDEB((stderr, "ConfSimple::i_set: nm[%s] val[%s] key[%s], init %d\n",
nm.c_str(), value.c_str(), sk.c_str(), init));
@ -344,7 +361,7 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
map<string, string>::iterator it;
it = ss->second.find(nm);
if (it == ss->second.end()) {
ss->second.insert(pair<string,string>(nm, value));
ss->second.insert(pair<string, string>(nm, value));
} else {
it->second = value;
existing = true;
@ -373,7 +390,7 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
vector<ConfLine>::iterator start, fin;
if (sk.empty()) {
start = m_order.begin();
LOGDEB((stderr,"ConfSimple::i_set: null sk, start at top of order\n"));
LOGDEB((stderr, "ConfSimple::i_set: null sk, start at top of order\n"));
} else {
start = find(m_order.begin(), m_order.end(),
ConfLine(ConfLine::CFL_SK, sk));
@ -389,8 +406,9 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
fin = m_order.end();
if (start != m_order.end()) {
// The null subkey has no entry (maybe it should)
if (!sk.empty())
if (!sk.empty()) {
start++;
}
for (vector<ConfLine>::iterator it = start; it != m_order.end(); it++) {
if (it->m_kind == ConfLine::CFL_SK) {
fin = it;
@ -407,10 +425,11 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
return 1;
}
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;
}
map<string, map<string, string> >::iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
@ -424,7 +443,7 @@ int ConfSimple::erase(const string &nm, const string &sk)
return write();
}
int ConfSimple::eraseKey(const string &sk)
int ConfSimple::eraseKey(const string& sk)
{
vector<string> nms = getNames(sk);
for (vector<string>::iterator it = nms.begin(); it != nms.end(); it++) {
@ -435,11 +454,12 @@ int ConfSimple::eraseKey(const string &sk)
// Walk the tree, calling user function at each node
ConfSimple::WalkerCode
ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
ConfSimple::sortwalk(WalkerCode(*walker)(void *, const string&, const string&),
void *clidata) const
{
if (!ok())
if (!ok()) {
return WALK_STOP;
}
// For all submaps:
for (map<string, map<string, string> >::const_iterator sit =
m_submaps.begin();
@ -447,17 +467,19 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
// Possibly emit submap name:
if (!sit->first.empty() && walker(clidata, string(), sit->first.c_str())
== WALK_STOP)
== WALK_STOP) {
return WALK_STOP;
}
// Walk submap
const map<string, string> &sm = sit->second;
for (map<string, string>::const_iterator it = sm.begin();it != sm.end();
const map<string, string>& sm = sit->second;
for (map<string, string>::const_iterator it = sm.begin(); it != sm.end();
it++) {
if (walker(clidata, it->first, it->second) == WALK_STOP)
if (walker(clidata, it->first, it->second) == WALK_STOP) {
return WALK_STOP;
}
}
}
return WALK_CONTINUE;
}
@ -465,14 +487,17 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&),
// a file
bool ConfSimple::write()
{
if (!ok())
if (!ok()) {
return false;
if (m_holdWrites)
}
if (m_holdWrites) {
return true;
}
if (m_filename.length()) {
ofstream output(m_filename.c_str(), ios::out|ios::trunc);
if (!output.is_open())
ofstream output(m_filename.c_str(), ios::out | ios::trunc);
if (!output.is_open()) {
return 0;
}
return write(output);
} else {
// 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
bool ConfSimple::write(ostream& out) const
{
if (!ok())
if (!ok()) {
return false;
}
string sk;
for (vector<ConfLine>::const_iterator it = m_order.begin();
it != m_order.end(); it++) {
switch(it->m_kind) {
switch (it->m_kind) {
case ConfLine::CFL_COMMENT:
out << it->m_data << endl;
if (!out.good())
if (!out.good()) {
return false;
}
break;
case ConfLine::CFL_SK:
sk = it->m_data;
@ -506,9 +533,10 @@ bool ConfSimple::write(ostream& out) const
// does
if (m_submaps.find(sk) != m_submaps.end()) {
out << "[" << it->m_data << "]" << endl;
if (!out.good())
if (!out.good()) {
return false;
}
}
break;
case ConfLine::CFL_VAR:
string nm = it->m_data;
@ -522,8 +550,9 @@ bool ConfSimple::write(ostream& out) const
string value;
if (ConfSimple::get(nm, value, sk)) {
varprinter(&out, nm, value);
if (!out.good())
if (!out.good()) {
return false;
}
break;
}
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
{
if (!ok())
if (!ok()) {
return;
}
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;
if (!ok())
if (!ok()) {
return mylist;
}
map<string, map<string, string> >::const_iterator ss;
if ((ss = m_submaps.find(sk)) == m_submaps.end()) {
return mylist;
@ -553,8 +584,9 @@ vector<string> ConfSimple::getNames(const string &sk, const char *pattern) const
mylist.reserve(ss->second.size());
map<string, string>::const_iterator 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;
}
mylist.push_back(it->first);
}
return mylist;
@ -563,8 +595,9 @@ vector<string> ConfSimple::getNames(const string &sk, const char *pattern) const
vector<string> ConfSimple::getSubKeys() const
{
vector<string> mylist;
if (!ok())
if (!ok()) {
return mylist;
}
mylist.reserve(m_submaps.size());
map<string, map<string, string> >::const_iterator 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();
it != keys.end(); it++) {
string val;
if (get(nm, val, *it))
if (get(nm, val, *it)) {
return true;
}
}
return false;
}
@ -589,11 +623,12 @@ bool ConfSimple::hasNameAnywhere(const string& nm) const
// ConfTree Methods: conftree interpret keys like a hierarchical file tree
// //////////////////////////////////////////////////////////////////////////
int ConfTree::get(const std::string &name, string &value, const string &sk)
const
int ConfTree::get(const std::string& name, string& value, const string& sk)
const
{
if (sk.empty() || !path_isabsolute(sk) ) {
// LOGDEB((stderr, "ConfTree::get: looking in global space for sk [%s]\n", sk.c_str()));
if (sk.empty() || !path_isabsolute(sk)) {
// LOGDEB((stderr, "ConfTree::get: looking in global space for [%s]\n",
// sk.c_str()));
return ConfSimple::get(name, value, sk);
}
@ -608,16 +643,17 @@ int ConfTree::get(const std::string &name, string &value, const string &sk)
for (;;) {
// LOGDEB((stderr,"ConfTree::get: looking for '%s' in '%s'\n",
// name.c_str(), msk.c_str()));
if (ConfSimple::get(name, value, msk))
if (ConfSimple::get(name, value, msk)) {
return 1;
}
string::size_type pos = msk.rfind("/");
if (pos != string::npos) {
msk.replace(pos, string::npos, string());
} else {
#ifdef _WIN32
if (msk.size() == 2 && isalpha(msk[0]) && msk[1] == ':')
if (msk.size() == 2 && isalpha(msk[0]) && msk[1] == ':') {
msk.clear();
else
} else
#endif
break;
}
@ -647,7 +683,7 @@ static char *thisprog;
bool complex_updates(const string& fn)
{
int fd;
if ((fd = open(fn.c_str(), O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
if ((fd = open(fn.c_str(), O_RDWR | O_TRUNC | O_CREAT, 0666)) < 0) {
perror("open/create");
return false;
}
@ -714,21 +750,22 @@ bool complex_updates(const string& fn)
return true;
}
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());
else
} else {
printf("'%s' -> '%s'\n", nm.c_str(), value.c_str());
}
return ConfSimple::WALK_CONTINUE;
}
const char *longvalue =
"Donnees012345678901234567890123456789012345678901234567890123456789AA"
"0123456789012345678901234567890123456789012345678901234567890123456789FIN"
"Donnees012345678901234567890123456789012345678901234567890123456789AA"
"0123456789012345678901234567890123456789012345678901234567890123456789FIN"
;
void memtest(ConfSimple &c)
void memtest(ConfSimple& c)
{
cout << "Initial:" << endl;
c.showall();
@ -766,7 +803,7 @@ bool readwrite(ConfNull *conf)
}
if (conf->get("unstring", value)) {
cout << "Value for unstring is ["<< value << "]" << endl;
cout << "Value for unstring is [" << value << "]" << endl;
} else {
cout << "unstring not set" << endl;
}
@ -811,7 +848,7 @@ bool erase(ConfNull *conf, const string& nm, const string& sub)
}
if (!conf->erase(nm, sub)) {
cerr << "delete name [" << nm << "] in ["<< sub << "] failed" << endl;
cerr << "delete name [" << nm << "] in [" << sub << "] failed" << endl;
return false;
}
return true;
@ -858,7 +895,8 @@ static char usage [] =
"-U : complex update test. Will erase the named file parameter\n"
;
void Usage() {
void Usage()
{
fprintf(stderr, "%s:%s\n", thisprog, usage);
exit(1);
}
@ -882,75 +920,109 @@ int main(int argc, char **argv)
const char *value = 0;
thisprog = argv[0];
argc--; argv++;
argc--;
argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
{
Usage();
}
while (**argv)
switch (*(*argv)++) {
case 'a':
op_flags |= OPT_a;
if (argc < 4)
if (argc < 4) {
Usage();
nm = *(++argv);argc--;
value = *(++argv);argc--;
sub = *(++argv);argc--;
}
nm = *(++argv);
argc--;
value = *(++argv);
argc--;
sub = *(++argv);
argc--;
goto b1;
case 'd':
op_flags |= OPT_d;
if (argc < 3)
if (argc < 3) {
Usage();
nm = *(++argv);argc--;
sub = *(++argv);argc--;
}
nm = *(++argv);
argc--;
sub = *(++argv);
argc--;
goto b1;
case 'E':
op_flags |= OPT_E;
if (argc < 2)
if (argc < 2) {
Usage();
sub = *(++argv);argc--;
}
sub = *(++argv);
argc--;
goto b1;
case 'k': op_flags |= OPT_k; break;
case 'k':
op_flags |= OPT_k;
break;
case 'q':
op_flags |= OPT_q;
if (argc < 3)
if (argc < 3) {
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)) {
// String storage test
if (argc != 0)
if (argc != 0) {
Usage();
}
string s;
ConfSimple c(s);
memtest(c);
exit(0);
} else if ((op_flags & OPT_V)) {
// No storage test
if (argc != 0)
if (argc != 0) {
Usage();
}
ConfSimple c;
memtest(c);
exit(0);
}
// Other tests use file(s) as backing store
if (argc < 1)
if (argc < 1) {
Usage();
}
if (op_flags & OPT_U) {
exit(!complex_updates(argv[0]));
@ -959,7 +1031,7 @@ int main(int argc, char **argv)
while (argc--) {
flist.push_back(*argv++);
}
bool ro = !(op_flags & (OPT_w|OPT_a|OPT_d|OPT_E));
bool ro = !(op_flags & (OPT_w | OPT_a | OPT_d | OPT_E));
ConfNull *conf = 0;
switch (flist.size()) {
case 0:
@ -1028,14 +1100,16 @@ int main(int argc, char **argv)
printf("\nNAMES in global space:\n");
vector<string> names = conf->getNames("");
for (vector<string>::iterator it = names.begin();
it!=names.end(); it++)
it != names.end(); it++) {
cout << *it << " ";
}
cout << endl;
printf("\nNAMES in global space matching t* \n");
names = conf->getNames("", "t*");
for (vector<string>::iterator it = names.begin();
it!=names.end(); it++)
it != names.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
}

View file

@ -49,10 +49,10 @@
* (useful to have central/personal config files)
*/
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
// rh7.3 likes iostream better...
#if defined(__GNUC__) && __GNUC__ < 3
@ -62,15 +62,13 @@
#include <ostream>
#endif
#ifndef NO_NAMESPACES
#include "pathut.h"
using std::string;
using std::vector;
using std::map;
using std::istream;
using std::ostream;
#endif // NO_NAMESPACES
#include "pathut.h"
/** Internal class used for storing presentation information */
class ConfLine {
@ -79,11 +77,9 @@ public:
Kind m_kind;
string m_data;
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;
}
};
@ -93,17 +89,17 @@ public:
*/
class ConfNull {
public:
enum StatusCode {STATUS_ERROR=0, STATUS_RO=1, STATUS_RW=2};
enum StatusCode {STATUS_ERROR = 0, STATUS_RO = 1, STATUS_RW = 2};
virtual ~ConfNull() {};
virtual int get(const string &name, string &value,
const string &sk = string()) const = 0;
virtual int get(const string& name, string& value,
const string& sk = string()) const = 0;
virtual bool hasNameAnywhere(const string& nm) const = 0;
virtual int set(const string &nm, const string &val,
const string &sk = string()) = 0;
virtual int set(const string& nm, const string& val,
const string& sk = string()) = 0;
virtual bool ok() const = 0;
virtual vector<string> getNames(const string &sk, const char* = 0)const = 0;
virtual int erase(const string &, const string &) = 0;
virtual int eraseKey(const string &) = 0;
virtual vector<string> getNames(const string& sk, const char* = 0)const = 0;
virtual int erase(const string&, const string&) = 0;
virtual int eraseKey(const string&) = 0;
virtual void showall() const {};
virtual vector<string> getSubKeys() const = 0;
virtual vector<string> getSubKeys(bool) const = 0;
@ -149,21 +145,20 @@ public:
* Decide if we actually rewrite the backing-store after modifying the
* tree.
*/
virtual bool holdWrites(bool on)
{
virtual bool holdWrites(bool on) {
m_holdWrites = on;
if (on == false) {
return write();
} else
} else {
return true;
}
}
/** Clear, then reparse from string */
void reparse(const string& in);
/** Clear all content */
void clear()
{
void clear() {
m_submaps.clear();
m_order.clear();
}
@ -173,28 +168,30 @@ public:
* global space if sk is empty).
* @return 0 if name not found, 1 else
*/
virtual int get(const string &name, string &value,
const string &sk = string()) const;
virtual int get(const string& name, string& value,
const string& sk = string()) const;
/**
* Set value for named parameter in specified subsection (or global)
* @return 0 for error, 1 else
*/
virtual int set(const string &nm, const string &val,
const string &sk = string());
virtual int set(const string& nm, const string& val,
const string& sk = string());
/**
* Remove name and value from config
*/
virtual int erase(const string &name, const string &sk);
virtual int erase(const string& name, const string& sk);
/**
* Erase all names under given subkey (and subkey itself)
*/
virtual int eraseKey(const string &sk);
virtual int eraseKey(const string& sk);
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.
@ -205,15 +202,15 @@ public:
*/
enum WalkerCode {WALK_STOP, WALK_CONTINUE};
virtual WalkerCode sortwalk(WalkerCode
(*wlkr)(void *cldata, const string &nm,
const string &val),
(*wlkr)(void *cldata, const string& nm,
const string& val),
void *clidata) const;
/** Print all values to stdout */
virtual void showall() const;
/** Return all names in given submap. */
virtual vector<string> getNames(const string &sk, const char *pattern = 0)
virtual vector<string> getNames(const string& sk, const char *pattern = 0)
const;
/** Check if name is present in any submap. This is relatively expensive
@ -223,28 +220,30 @@ public:
/**
* Return all subkeys
*/
virtual vector<string> getSubKeys(bool) const
{
virtual vector<string> getSubKeys(bool) const {
return getSubKeys();
}
virtual vector<string> getSubKeys_unsorted(bool = false) const {
return m_subkeys_unsorted;
}
virtual vector<string> getSubKeys() const;
/** 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();
}
virtual string getFilename() const
{return m_filename;}
virtual string getFilename() const {
return m_filename;
}
/**
* Copy constructor. Expensive but less so than a full rebuild
*/
ConfSimple(const ConfSimple &rhs)
: ConfNull()
{
if ((status = rhs.status) == STATUS_ERROR)
ConfSimple(const ConfSimple& rhs)
: ConfNull() {
if ((status = rhs.status) == STATUS_ERROR) {
return;
}
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
}
@ -252,8 +251,7 @@ public:
/**
* Assignement. This is expensive
*/
ConfSimple& operator=(const ConfSimple &rhs)
{
ConfSimple& operator=(const ConfSimple& rhs) {
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
@ -276,6 +274,7 @@ private:
// Configuration data submaps (one per subkey, the main data has a
// null subkey)
map<string, map<string, string> > m_submaps;
vector<string> m_subkeys_unsorted;
// Presentation data. We keep the comments, empty lines and
// variable and subkey ordering information in there (for
// rewriting the file while keeping hand-edited information)
@ -286,8 +285,8 @@ private:
void parseinput(istream& input);
bool write();
// Internal version of set: no RW checking
virtual int i_set(const string &nm, const string &val,
const string &sk, bool init = false);
virtual int i_set(const string& nm, const string& val,
const string& sk, bool init = false);
bool i_changed(bool upd);
};
@ -315,14 +314,13 @@ public:
* expansion */
ConfTree(const char *fname, int readonly = 0)
: ConfSimple(fname, readonly, true) {}
ConfTree(const string &data, int readonly = 0)
ConfTree(const string& data, int readonly = 0)
: ConfSimple(data, readonly, true) {}
ConfTree(int readonly = 0)
: ConfSimple(readonly, true) {}
virtual ~ConfTree() {};
ConfTree(const ConfTree& r) : ConfSimple(r) {};
ConfTree& operator=(const ConfTree& r)
{
ConfTree& operator=(const ConfTree& r) {
ConfSimple::operator=(r);
return *this;
}
@ -332,7 +330,7 @@ public:
* parents.
* @return 0 if name not found, 1 else
*/
virtual int get(const string &name, string &value, const string &sk) const;
virtual int get(const string& name, string& value, const string& sk) const;
};
/**
@ -351,85 +349,82 @@ public:
/// Construct from configuration file names. The earler
/// files in have priority when fetching values. Only the first
/// 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 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;
for (vector<string>::const_iterator it = dirs.begin();
it != dirs.end(); it++){
it != dirs.end(); it++) {
fns.push_back(path_cat(*it, nm));
}
ConfStack::construct(fns, ro);
}
ConfStack(const ConfStack &rhs)
: ConfNull()
{
ConfStack(const ConfStack& rhs)
: ConfNull() {
init_from(rhs);
}
virtual ~ConfStack()
{
virtual ~ConfStack() {
clear();
m_ok = false;
}
ConfStack& operator=(const ConfStack &rhs)
{
if (this != &rhs){
ConfStack& operator=(const ConfStack& rhs) {
if (this != &rhs) {
clear();
m_ok = rhs.m_ok;
if (m_ok)
if (m_ok) {
init_from(rhs);
}
}
return *this;
}
virtual bool sourceChanged() const
{
virtual bool sourceChanged() const {
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->sourceChanged())
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->sourceChanged()) {
return true;
}
}
return false;
}
virtual int get(const string &name, string &value, const string &sk,
bool shallow) const
{
virtual int get(const string& name, string& value, const string& sk,
bool shallow) const {
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->get(name, value, sk))
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->get(name, value, sk)) {
return true;
if (shallow)
}
if (shallow) {
break;
}
}
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);
}
virtual bool hasNameAnywhere(const string& nm) const
{
virtual bool hasNameAnywhere(const string& nm) const {
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->hasNameAnywhere(nm))
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasNameAnywhere(nm)) {
return true;
}
}
return false;
}
virtual int set(const string &nm, const string &val,
const string &sk = string())
{
if (!m_ok)
virtual int set(const string& nm, const string& val,
const string& sk = string()) {
if (!m_ok) {
return 0;
}
//LOGDEB2(("ConfStack::set [%s]:[%s] -> [%s]\n", sk.c_str(),
//nm.c_str(), val.c_str()));
// Avoid adding unneeded entries: if the new value matches the
@ -456,33 +451,27 @@ public:
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);
}
virtual int eraseKey(const string &sk)
{
virtual int eraseKey(const string& sk) {
return m_confs.front()->eraseKey(sk);
}
virtual bool holdWrites(bool on)
{
virtual bool holdWrites(bool on) {
return m_confs.front()->holdWrites(on);
}
virtual vector<string> getNames(const string &sk, const char *pattern = 0)
const
{
virtual vector<string> getNames(const string& sk, const char *pattern = 0)
const {
return getNames1(sk, pattern, false);
}
virtual vector<string> getNamesShallow(const string &sk,
const char *patt = 0) const
{
virtual vector<string> getNamesShallow(const string& sk,
const char *patt = 0) const {
return getNames1(sk, patt, true);
}
virtual vector<string> getNames1(const string &sk, const char *pattern,
bool shallow) const
{
virtual vector<string> getNames1(const string& sk, const char *pattern,
bool shallow) const {
vector<string> nms;
typename vector<T*>::const_iterator it;
bool skfound = false;
@ -492,37 +481,39 @@ public:
vector<string> lst = (*it)->getNames(sk, pattern);
nms.insert(nms.end(), lst.begin(), lst.end());
}
if (shallow && skfound)
if (shallow && skfound) {
break;
}
}
sort(nms.begin(), nms.end());
vector<string>::iterator uit = unique(nms.begin(), nms.end());
nms.resize(uit - nms.begin());
return nms;
}
virtual vector<string> getSubKeys() const
{
virtual vector<string> getSubKeys() const {
return getSubKeys(false);
}
virtual vector<string> getSubKeys(bool shallow) const
{
virtual vector<string> getSubKeys(bool shallow) const {
vector<string> sks;
typename vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
vector<string> lst;
lst = (*it)->getSubKeys();
sks.insert(sks.end(), lst.begin(), lst.end());
if (shallow)
if (shallow) {
break;
}
}
sort(sks.begin(), sks.end());
vector<string>::iterator uit = unique(sks.begin(), sks.end());
sks.resize(uit - sks.begin());
return sks;
}
virtual bool ok() const {return m_ok;}
virtual bool ok() const {
return m_ok;
}
private:
bool m_ok;
@ -531,24 +522,24 @@ private:
/// Reset to pristine
void clear() {
typename vector<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
delete (*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) {
void init_from(const ConfStack& rhs) {
if ((m_ok = rhs.m_ok)) {
typename vector<T*>::const_iterator it;
for (it = rhs.m_confs.begin();it != rhs.m_confs.end();it++) {
for (it = rhs.m_confs.begin(); it != rhs.m_confs.end(); it++) {
m_confs.push_back(new T(**it));
}
}
}
/// Common construct from file names code
void construct(const vector<string> &fns, bool ro) {
void construct(const vector<string>& fns, bool ro) {
vector<string>::const_iterator it;
bool lastok = false;
for (it = fns.begin(); it != fns.end(); it++) {