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,
bool ro = true) {
std::vector<std::string> fns;
for (std::vector<std::string>::const_iterator it = dirs.begin();
it != dirs.end(); it++) {
fns.push_back(path_cat(*it, nm));
for (const auto& dir : dirs) {
fns.push_back(path_cat(dir, nm));
}
ConfStack::construct(fns, ro);
}
@ -419,9 +418,8 @@ public:
}
virtual bool sourceChanged() const override {
typename std::vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->sourceChanged()) {
for (const auto& conf : m_confs) {
if (conf->sourceChanged()) {
return true;
}
}
@ -447,9 +445,8 @@ public:
}
virtual bool hasNameAnywhere(const std::string& nm) const override {
typename std::vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasNameAnywhere(nm)) {
for (const auto& conf : m_confs) {
if (conf->hasNameAnywhere(nm)) {
return true;
}
}
@ -466,7 +463,7 @@ public:
// 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 std::vector<T*>::iterator it = m_confs.begin();
auto it = m_confs.begin();
it++;
while (it != m_confs.end()) {
std::string value;
@ -509,12 +506,11 @@ public:
virtual std::vector<std::string> getNames1(
const std::string& sk, const char *pattern, bool shallow) const {
std::vector<std::string> nms;
typename std::vector<T*>::const_iterator it;
bool skfound = false;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
if ((*it)->hasSubKey(sk)) {
for (const auto& conf : m_confs) {
if (conf->hasSubKey(sk)) {
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());
}
if (shallow && skfound) {
@ -532,10 +528,9 @@ public:
}
virtual std::vector<std::string> getSubKeys(bool shallow) const override {
std::vector<std::string> sks;
typename std::vector<T*>::const_iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
for (const auto& conf : m_confs) {
std::vector<std::string> lst;
lst = (*it)->getSubKeys();
lst = conf->getSubKeys();
sks.insert(sks.end(), lst.begin(), lst.end());
if (shallow) {
break;
@ -557,9 +552,8 @@ private:
/// Reset to pristine
void clear() {
typename std::vector<T*>::iterator it;
for (it = m_confs.begin(); it != m_confs.end(); it++) {
delete(*it);
for (auto& conf : m_confs) {
delete(conf);
}
m_confs.clear();
}
@ -567,34 +561,29 @@ private:
/// Common code to initialize from existing object
void init_from(const ConfStack& rhs) {
if ((m_ok = rhs.m_ok)) {
typename std::vector<T*>::const_iterator it;
for (it = rhs.m_confs.begin(); it != rhs.m_confs.end(); it++) {
m_confs.push_back(new T(**it));
for (const auto& conf : rhs.m_confs) {
m_confs.push_back(new T(*conf));
}
}
}
/// 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) {
std::vector<std::string>::const_iterator it;
bool lastok = false;
for (it = fns.begin(); it != fns.end(); it++) {
T* p = new T(it->c_str(), ro);
bool ok = true;
for (const auto& fn : fns) {
T* p = new T(fn.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;
}
ok = false;
}
// Only the first file is opened rw
ro = true;
}
m_ok = lastok;
m_ok = ok;
}
};