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
@ -41,7 +43,7 @@ using namespace std;
#define LOGDEB(X) #define LOGDEB(X)
#endif #endif
void ConfSimple::parseinput(istream &input) void ConfSimple::parseinput(istream& input)
{ {
string submapkey; string submapkey;
string cline; string cline;
@ -71,22 +73,24 @@ void ConfSimple::parseinput(istream &input)
string::size_type pos = cline.find_last_not_of("\n\r"); string::size_type pos = cline.find_last_not_of("\n\r");
if (pos == string::npos) { if (pos == string::npos) {
cline.clear(); cline.clear();
} else if (pos != cline.length()-1) { } else if (pos != cline.length() - 1) {
cline.erase(pos+1); cline.erase(pos + 1);
} }
} }
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)
@ -122,7 +129,7 @@ void ConfSimple::parseinput(istream &input)
string nm, val; string nm, val;
nm = line.substr(0, eqpos); nm = line.substr(0, eqpos);
trimstring(nm); trimstring(nm);
val = line.substr(eqpos+1, string::npos); val = line.substr(eqpos + 1, string::npos);
trimstring(val); trimstring(val);
if (nm.length() == 0) { if (nm.length() == 0) {
@ -130,9 +137,10 @@ 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;
} }
}
} }
@ -167,7 +175,7 @@ ConfSimple::ConfSimple(const char *fname, int readonly, bool tildexp)
if (readonly) { if (readonly) {
input.open(fname, ios::in); input.open(fname, ios::in);
} else { } 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' // It seems that there is no separate 'create if not exists'
// open flag. Have to truncate to create, but dont want to do // open flag. Have to truncate to create, but dont want to do
// this to an existing file ! // this to an existing file !
@ -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;
} }
} }
@ -232,20 +244,23 @@ bool ConfSimple::i_changed(bool upd)
return false; 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; 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;
} }
@ -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 // 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 // the file data (when read back by conftree), only its ease of
// editing with a normal editor. // editing with a normal editor.
static ConfSimple::WalkerCode varprinter(void *f, const string &nm, static ConfSimple::WalkerCode varprinter(void *f, const string& nm,
const string &value) const string& value)
{ {
ostream& output = *((ostream *)f); ostream& output = *((ostream *)f);
if (nm.empty()) { if (nm.empty()) {
@ -295,23 +310,25 @@ static ConfSimple::WalkerCode varprinter(void *f, const string &nm,
} }
// Set variable and rewrite data // Set variable and rewrite data
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();
} }
// Internal set variable: no rw checking or file rewriting. If init is // Internal set variable: no rw checking or file rewriting. If init is
// set, we're doing initial parsing, else we are changing a parsed // set, we're doing initial parsing, else we are changing a parsed
// tree (changes the way we update the order data) // tree (changes the way we update the order data)
int ConfSimple::i_set(const std::string &nm, const std::string &value, int ConfSimple::i_set(const std::string& nm, const std::string& value,
const string &sk, bool init) const string& sk, bool init)
{ {
LOGDEB((stderr, "ConfSimple::i_set: nm[%s] val[%s] key[%s], init %d\n", LOGDEB((stderr, "ConfSimple::i_set: nm[%s] val[%s] key[%s], init %d\n",
nm.c_str(), value.c_str(), sk.c_str(), init)); 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; map<string, string>::iterator it;
it = ss->second.find(nm); it = ss->second.find(nm);
if (it == ss->second.end()) { if (it == ss->second.end()) {
ss->second.insert(pair<string,string>(nm, value)); ss->second.insert(pair<string, string>(nm, value));
} else { } else {
it->second = value; it->second = value;
existing = true; existing = true;
@ -373,7 +390,7 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
vector<ConfLine>::iterator start, fin; vector<ConfLine>::iterator start, fin;
if (sk.empty()) { if (sk.empty()) {
start = m_order.begin(); 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 { } else {
start = find(m_order.begin(), m_order.end(), start = find(m_order.begin(), m_order.end(),
ConfLine(ConfLine::CFL_SK, sk)); 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(); 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;
@ -407,10 +425,11 @@ int ConfSimple::i_set(const std::string &nm, const std::string &value,
return 1; 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; 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()) {
@ -424,7 +443,7 @@ int ConfSimple::erase(const string &nm, const string &sk)
return write(); return write();
} }
int ConfSimple::eraseKey(const string &sk) int ConfSimple::eraseKey(const string& sk)
{ {
vector<string> nms = getNames(sk); vector<string> nms = getNames(sk);
for (vector<string>::iterator it = nms.begin(); it != nms.end(); it++) { 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 // Walk the tree, calling user function at each node
ConfSimple::WalkerCode 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;
} }
@ -589,11 +623,12 @@ bool ConfSimple::hasNameAnywhere(const string& nm) const
// ConfTree Methods: conftree interpret keys like a hierarchical file tree // ConfTree Methods: conftree interpret keys like a hierarchical file tree
// ////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////
int ConfTree::get(const std::string &name, string &value, const string &sk) 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;
} }
@ -647,7 +683,7 @@ static char *thisprog;
bool complex_updates(const string& fn) bool complex_updates(const string& fn)
{ {
int fd; 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"); perror("open/create");
return false; return false;
} }
@ -714,21 +750,22 @@ bool complex_updates(const string& fn)
return true; 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()); 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;
} }
const char *longvalue = const char *longvalue =
"Donnees012345678901234567890123456789012345678901234567890123456789AA" "Donnees012345678901234567890123456789012345678901234567890123456789AA"
"0123456789012345678901234567890123456789012345678901234567890123456789FIN" "0123456789012345678901234567890123456789012345678901234567890123456789FIN"
; ;
void memtest(ConfSimple &c) void memtest(ConfSimple& c)
{ {
cout << "Initial:" << endl; cout << "Initial:" << endl;
c.showall(); c.showall();
@ -766,7 +803,7 @@ bool readwrite(ConfNull *conf)
} }
if (conf->get("unstring", value)) { if (conf->get("unstring", value)) {
cout << "Value for unstring is ["<< value << "]" << endl; cout << "Value for unstring is [" << value << "]" << endl;
} else { } else {
cout << "unstring not set" << endl; cout << "unstring not set" << endl;
} }
@ -811,7 +848,7 @@ bool erase(ConfNull *conf, const string& nm, const string& sub)
} }
if (!conf->erase(nm, 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 false;
} }
return true; return true;
@ -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]));
@ -959,7 +1031,7 @@ int main(int argc, char **argv)
while (argc--) { while (argc--) {
flist.push_back(*argv++); 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; ConfNull *conf = 0;
switch (flist.size()) { switch (flist.size()) {
case 0: case 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;
} }
}; };
@ -93,17 +89,17 @@ public:
*/ */
class ConfNull { class ConfNull {
public: 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 ~ConfNull() {};
virtual int get(const string &name, string &value, virtual int get(const string& name, string& value,
const string &sk = string()) const = 0; const string& sk = string()) const = 0;
virtual bool hasNameAnywhere(const string& nm) const = 0; virtual bool hasNameAnywhere(const string& nm) const = 0;
virtual int set(const string &nm, const string &val, virtual int set(const string& nm, const string& val,
const string &sk = string()) = 0; const string& sk = string()) = 0;
virtual bool ok() const = 0; virtual bool ok() const = 0;
virtual vector<string> getNames(const string &sk, const char* = 0)const = 0; virtual vector<string> getNames(const string& sk, const char* = 0)const = 0;
virtual int erase(const string &, const string &) = 0; virtual int erase(const string&, const string&) = 0;
virtual int eraseKey(const string &) = 0; virtual int eraseKey(const string&) = 0;
virtual void showall() const {}; virtual void showall() const {};
virtual vector<string> getSubKeys() const = 0; virtual vector<string> getSubKeys() const = 0;
virtual vector<string> getSubKeys(bool) 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 * 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();
} }
@ -173,28 +168,30 @@ public:
* global space if sk is empty). * global space if sk is empty).
* @return 0 if name not found, 1 else * @return 0 if name not found, 1 else
*/ */
virtual int get(const string &name, string &value, virtual int get(const string& name, string& value,
const string &sk = string()) const; const string& sk = string()) const;
/** /**
* Set value for named parameter in specified subsection (or global) * Set value for named parameter in specified subsection (or global)
* @return 0 for error, 1 else * @return 0 for error, 1 else
*/ */
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());
/** /**
* Remove name and value from config * 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) * 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 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.
@ -205,15 +202,15 @@ public:
*/ */
enum WalkerCode {WALK_STOP, WALK_CONTINUE}; enum WalkerCode {WALK_STOP, WALK_CONTINUE};
virtual WalkerCode sortwalk(WalkerCode virtual WalkerCode sortwalk(WalkerCode
(*wlkr)(void *cldata, const string &nm, (*wlkr)(void *cldata, const string& nm,
const string &val), const string& val),
void *clidata) const; void *clidata) const;
/** Print all values to stdout */ /** Print all values to stdout */
virtual void showall() const; virtual void showall() const;
/** Return all names in given submap. */ /** 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; const;
/** Check if name is present in any submap. This is relatively expensive /** Check if name is present in any submap. This is relatively expensive
@ -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)
@ -286,8 +285,8 @@ private:
void parseinput(istream& input); void parseinput(istream& input);
bool write(); bool write();
// Internal version of set: no RW checking // Internal version of set: no RW checking
virtual int i_set(const string &nm, const string &val, virtual int i_set(const string& nm, const string& val,
const string &sk, bool init = false); const string& sk, bool init = false);
bool i_changed(bool upd); bool i_changed(bool upd);
}; };
@ -315,14 +314,13 @@ public:
* expansion */ * expansion */
ConfTree(const char *fname, int readonly = 0) ConfTree(const char *fname, int readonly = 0)
: ConfSimple(fname, readonly, true) {} : ConfSimple(fname, readonly, true) {}
ConfTree(const string &data, int readonly = 0) ConfTree(const string& data, int readonly = 0)
: ConfSimple(data, readonly, true) {} : ConfSimple(data, readonly, true) {}
ConfTree(int readonly = 0) ConfTree(int readonly = 0)
: 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;
} }
@ -332,7 +330,7 @@ public:
* parents. * parents.
* @return 0 if name not found, 1 else * @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 /// 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++) {
fns.push_back(path_cat(*it, nm)); fns.push_back(path_cat(*it, nm));
} }
ConfStack::construct(fns, ro); ConfStack::construct(fns, ro);
} }
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;
@ -531,24 +522,24 @@ private:
/// Reset to pristine /// Reset to pristine
void clear() { void clear() {
typename vector<T*>::iterator it; typename vector<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) { for (it = m_confs.begin(); it != m_confs.end(); it++) {
delete (*it); delete(*it);
} }
m_confs.clear(); m_confs.clear();
} }
/// Common code to initialize from existing object /// 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)) { if ((m_ok = rhs.m_ok)) {
typename vector<T*>::const_iterator it; 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)); m_confs.push_back(new T(**it));
} }
} }
} }
/// Common construct from file names code /// 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; vector<string>::const_iterator it;
bool lastok = false; bool lastok = false;
for (it = fns.begin(); it != fns.end(); it++) { for (it = fns.begin(); it != fns.end(); it++) {