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

File diff suppressed because it is too large Load diff

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,12 +77,10 @@ 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)
{
return o.m_kind == m_kind && o.m_data == m_data;
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,23 +145,22 @@ public:
* Decide if we actually rewrite the backing-store after modifying the
* tree.
*/
virtual bool holdWrites(bool on)
{
m_holdWrites = on;
if (on == false) {
return write();
} else
return true;
virtual bool holdWrites(bool on) {
m_holdWrites = on;
if (on == false) {
return write();
} else {
return true;
}
}
/** Clear, then reparse from string */
void reparse(const string& in);
/** Clear all content */
void clear()
{
m_submaps.clear();
m_order.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,16 +202,16 @@ public:
*/
enum WalkerCode {WALK_STOP, WALK_CONTINUE};
virtual WalkerCode sortwalk(WalkerCode
(*wlkr)(void *cldata, const string &nm,
const string &val),
void *clidata) const;
(*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)
const;
virtual vector<string> getNames(const string& sk, const char *pattern = 0)
const;
/** Check if name is present in any submap. This is relatively expensive
* but useful for saving further processing sometimes */
@ -223,42 +220,43 @@ public:
/**
* Return all subkeys
*/
virtual vector<string> getSubKeys(bool) const
{
return getSubKeys();
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
{
return m_submaps.find(sk) != m_submaps.end();
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)
return;
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
ConfSimple(const ConfSimple& rhs)
: ConfNull() {
if ((status = rhs.status) == STATUS_ERROR) {
return;
}
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
}
/**
* Assignement. This is expensive
*/
ConfSimple& operator=(const ConfSimple &rhs)
{
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
}
return *this;
ConfSimple& operator=(const ConfSimple& rhs) {
if (this != &rhs && (status = rhs.status) != STATUS_ERROR) {
m_filename = rhs.m_filename;
m_submaps = rhs.m_submaps;
}
return *this;
}
/**
@ -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);
};
@ -314,17 +313,16 @@ public:
/* The constructors just call ConfSimple's, asking for key tilde
* expansion */
ConfTree(const char *fname, int readonly = 0)
: ConfSimple(fname, readonly, true) {}
ConfTree(const string &data, int readonly = 0)
: ConfSimple(data, readonly, true) {}
: ConfSimple(fname, readonly, true) {}
ConfTree(const string& data, int readonly = 0)
: ConfSimple(data, readonly, true) {}
ConfTree(int readonly = 0)
: ConfSimple(readonly, true) {}
: ConfSimple(readonly, true) {}
virtual ~ConfTree() {};
ConfTree(const ConfTree& r) : ConfSimple(r) {};
ConfTree& operator=(const ConfTree& r)
{
ConfSimple::operator=(r);
return *this;
ConfTree(const ConfTree& r) : ConfSimple(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,178 +349,171 @@ 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)
{
construct(fns, ro);
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)
{
vector<string> fns;
for (vector<string>::const_iterator it = dirs.begin();
it != dirs.end(); it++){
fns.push_back(path_cat(*it, nm));
}
ConfStack::construct(fns, ro);
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++) {
fns.push_back(path_cat(*it, nm));
}
ConfStack::construct(fns, ro);
}
ConfStack(const ConfStack &rhs)
: ConfNull()
{
init_from(rhs);
ConfStack(const ConfStack& rhs)
: ConfNull() {
init_from(rhs);
}
virtual ~ConfStack()
{
clear();
m_ok = false;
virtual ~ConfStack() {
clear();
m_ok = false;
}
ConfStack& operator=(const ConfStack &rhs)
{
if (this != &rhs){
clear();
m_ok = rhs.m_ok;
if (m_ok)
init_from(rhs);
}
return *this;
ConfStack& operator=(const ConfStack& rhs) {
if (this != &rhs) {
clear();
m_ok = rhs.m_ok;
if (m_ok) {
init_from(rhs);
}
}
return *this;
}
virtual bool sourceChanged() const
{
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->sourceChanged())
return true;
}
return false;
virtual bool sourceChanged() const {
typename vector<T*>::const_iterator it;
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
{
typename vector<T*>::const_iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
if ((*it)->get(name, value, sk))
return true;
if (shallow)
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)) {
return true;
}
if (shallow) {
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);
}
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))
return true;
}
return false;
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)) {
return true;
}
}
return false;
}
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
// one out from the deeper configs, erase or dont add it
// from/to the topmost file
typename vector<T*>::iterator it = m_confs.begin();
it++;
while (it != m_confs.end()) {
string value;
if ((*it)->get(nm, value, sk)) {
// This file has value for nm/sk. If it is the same as the new
// one, no need for an entry in the topmost file. Else, stop
// looking and add the new entry
if (value == val) {
m_confs.front()->erase(nm, sk);
return true;
} else {
break;
}
}
it++;
}
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
// one out from the deeper configs, erase or dont add it
// from/to the topmost file
typename vector<T*>::iterator it = m_confs.begin();
it++;
while (it != m_confs.end()) {
string value;
if ((*it)->get(nm, value, sk)) {
// This file has value for nm/sk. If it is the same as the new
// one, no need for an entry in the topmost file. Else, stop
// looking and add the new entry
if (value == val) {
m_confs.front()->erase(nm, sk);
return true;
} else {
break;
}
}
it++;
}
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)
{
return m_confs.front()->erase(nm, sk);
virtual int erase(const string& nm, const string& sk) {
return m_confs.front()->erase(nm, sk);
}
virtual int eraseKey(const string &sk)
{
return m_confs.front()->eraseKey(sk);
virtual int eraseKey(const string& sk) {
return m_confs.front()->eraseKey(sk);
}
virtual bool holdWrites(bool on)
{
return m_confs.front()->holdWrites(on);
virtual bool holdWrites(bool on) {
return m_confs.front()->holdWrites(on);
}
virtual vector<string> getNames(const string &sk, const char *pattern = 0)
const
{
return getNames1(sk, pattern, false);
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
{
return getNames1(sk, patt, true);
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
{
vector<string> nms;
typename vector<T*>::const_iterator it;
bool skfound = false;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasSubKey(sk)) {
skfound = true;
vector<string> lst = (*it)->getNames(sk, pattern);
nms.insert(nms.end(), lst.begin(), lst.end());
}
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> getNames1(const string& sk, const char *pattern,
bool shallow) const {
vector<string> nms;
typename vector<T*>::const_iterator it;
bool skfound = false;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasSubKey(sk)) {
skfound = true;
vector<string> lst = (*it)->getNames(sk, pattern);
nms.insert(nms.end(), lst.begin(), lst.end());
}
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
{
return getSubKeys(false);
virtual vector<string> getSubKeys() const {
return getSubKeys(false);
}
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)
break;
}
sort(sks.begin(), sks.end());
vector<string>::iterator uit = unique(sks.begin(), sks.end());
sks.resize(uit - sks.begin());
return sks;
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) {
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;
@ -530,44 +521,44 @@ private:
/// Reset to pristine
void clear() {
typename vector<T*>::iterator it;
for (it = m_confs.begin();it != m_confs.end();it++) {
delete (*it);
}
m_confs.clear();
typename vector<T*>::iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
delete(*it);
}
m_confs.clear();
}
/// Common code to initialize from existing object
void init_from(const ConfStack &rhs) {
if ((m_ok = rhs.m_ok)) {
typename vector<T*>::const_iterator it;
for (it = rhs.m_confs.begin();it != rhs.m_confs.end();it++) {
m_confs.push_back(new T(**it));
}
}
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++) {
m_confs.push_back(new T(**it));
}
}
}
/// Common construct from file names code
void construct(const vector<string> &fns, bool ro) {
vector<string>::const_iterator it;
bool lastok = false;
for (it = fns.begin(); it != fns.end(); it++) {
T* p = new T(it->c_str(), ro);
if (p && p->ok()) {
m_confs.push_back(p);
lastok = true;
} else {
delete p;
lastok = false;
if (!ro) {
// For rw acccess, the topmost file needs to be ok
// (ro is set to true after the first file)
break;
}
}
ro = true;
}
m_ok = lastok;
void construct(const vector<string>& fns, bool ro) {
vector<string>::const_iterator it;
bool lastok = false;
for (it = fns.begin(); it != fns.end(); it++) {
T* p = new T(it->c_str(), ro);
if (p && p->ok()) {
m_confs.push_back(p);
lastok = true;
} else {
delete p;
lastok = false;
if (!ro) {
// For rw acccess, the topmost file needs to be ok
// (ro is set to true after the first file)
break;
}
}
ro = true;
}
m_ok = lastok;
}
};