confstack: fail constructor if any bad in stack. Modernize c++

This commit is contained in:
Jean-Francois Dockes 2019-11-08 14:10:11 +01:00
parent 32ad0396d3
commit 1302b185d8

View File

@ -390,9 +390,8 @@ public:
ConfStack(const std::string& nm, const std::vector<std::string>& dirs, ConfStack(const std::string& nm, const std::vector<std::string>& dirs,
bool ro = true) { bool ro = true) {
std::vector<std::string> fns; std::vector<std::string> fns;
for (std::vector<std::string>::const_iterator it = dirs.begin(); for (const auto& dir : dirs) {
it != dirs.end(); it++) { fns.push_back(path_cat(dir, nm));
fns.push_back(path_cat(*it, nm));
} }
ConfStack::construct(fns, ro); ConfStack::construct(fns, ro);
} }
@ -419,9 +418,8 @@ public:
} }
virtual bool sourceChanged() const override { virtual bool sourceChanged() const override {
typename std::vector<T*>::const_iterator it; for (const auto& conf : m_confs) {
for (it = m_confs.begin(); it != m_confs.end(); it++) { if (conf->sourceChanged()) {
if ((*it)->sourceChanged()) {
return true; return true;
} }
} }
@ -447,9 +445,8 @@ public:
} }
virtual bool hasNameAnywhere(const std::string& nm) const override { virtual bool hasNameAnywhere(const std::string& nm) const override {
typename std::vector<T*>::const_iterator it; for (const auto& conf : m_confs) {
for (it = m_confs.begin(); it != m_confs.end(); it++) { if (conf->hasNameAnywhere(nm)) {
if ((*it)->hasNameAnywhere(nm)) {
return true; return true;
} }
} }
@ -466,7 +463,7 @@ public:
// Avoid adding unneeded entries: if the new value matches the // Avoid adding unneeded entries: if the new value matches the
// one out from the deeper configs, erase or dont add it // one out from the deeper configs, erase or dont add it
// from/to the topmost file // from/to the topmost file
typename std::vector<T*>::iterator it = m_confs.begin(); auto it = m_confs.begin();
it++; it++;
while (it != m_confs.end()) { while (it != m_confs.end()) {
std::string value; std::string value;
@ -509,12 +506,11 @@ public:
virtual std::vector<std::string> getNames1( virtual std::vector<std::string> getNames1(
const std::string& sk, const char *pattern, bool shallow) const { const std::string& sk, const char *pattern, bool shallow) const {
std::vector<std::string> nms; std::vector<std::string> nms;
typename std::vector<T*>::const_iterator it;
bool skfound = false; bool skfound = false;
for (it = m_confs.begin(); it != m_confs.end(); it++) { for (const auto& conf : m_confs) {
if ((*it)->hasSubKey(sk)) { if (conf->hasSubKey(sk)) {
skfound = true; skfound = true;
std::vector<std::string> lst = (*it)->getNames(sk, pattern); std::vector<std::string> lst = conf->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) {
@ -532,10 +528,9 @@ public:
} }
virtual std::vector<std::string> getSubKeys(bool shallow) const override { virtual std::vector<std::string> getSubKeys(bool shallow) const override {
std::vector<std::string> sks; std::vector<std::string> sks;
typename std::vector<T*>::const_iterator it; for (const auto& conf : m_confs) {
for (it = m_confs.begin(); it != m_confs.end(); it++) {
std::vector<std::string> lst; std::vector<std::string> lst;
lst = (*it)->getSubKeys(); lst = conf->getSubKeys();
sks.insert(sks.end(), lst.begin(), lst.end()); sks.insert(sks.end(), lst.begin(), lst.end());
if (shallow) { if (shallow) {
break; break;
@ -557,9 +552,8 @@ private:
/// Reset to pristine /// Reset to pristine
void clear() { void clear() {
typename std::vector<T*>::iterator it; for (auto& conf : m_confs) {
for (it = m_confs.begin(); it != m_confs.end(); it++) { delete(conf);
delete(*it);
} }
m_confs.clear(); m_confs.clear();
} }
@ -567,34 +561,29 @@ private:
/// 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 std::vector<T*>::const_iterator it; for (const auto& conf : rhs.m_confs) {
for (it = rhs.m_confs.begin(); it != rhs.m_confs.end(); it++) { m_confs.push_back(new T(*conf));
m_confs.push_back(new T(**it));
} }
} }
} }
/// Common construct from file names code /// Common construct from file names code. We used to be ok even
/// if some files were not readable/parsable. Now fail if any
/// fails.
void construct(const std::vector<std::string>& fns, bool ro) { void construct(const std::vector<std::string>& fns, bool ro) {
std::vector<std::string>::const_iterator it; bool ok = true;
bool lastok = false; for (const auto& fn : fns) {
for (it = fns.begin(); it != fns.end(); it++) { T* p = new T(fn.c_str(), ro);
T* p = new T(it->c_str(), ro);
if (p && p->ok()) { if (p && p->ok()) {
m_confs.push_back(p); m_confs.push_back(p);
lastok = true;
} else { } else {
delete p; delete p;
lastok = false; ok = false;
if (!ro) {
// For rw acccess, the topmost file needs to be ok
// (ro is set to true after the first file)
break;
}
} }
// Only the first file is opened rw
ro = true; ro = true;
} }
m_ok = lastok; m_ok = ok;
} }
}; };