diff --git a/src/utils/conftree.cpp b/src/utils/conftree.cpp index 6bf2b057..a4eb1ccc 100644 --- a/src/utils/conftree.cpp +++ b/src/utils/conftree.cpp @@ -201,9 +201,17 @@ ConfSimple::StatusCode ConfSimple::getStatus() const } } -bool ConfSimple::sourceChanged() +bool ConfSimple::sourceChanged() const { - return i_changed(false); + if (!m_filename.empty()) { + struct stat st; + if (stat(m_filename.c_str(), &st) == 0) { + if (m_fmtime != st.st_mtime) { + return true; + } + } + } + return false; } bool ConfSimple::i_changed(bool upd) @@ -408,12 +416,13 @@ 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&), - void *clidata) + void *clidata) const { if (!ok()) return WALK_STOP; // For all submaps: - for (map >::iterator sit = m_submaps.begin(); + for (map >::const_iterator sit = + m_submaps.begin(); sit != m_submaps.end(); sit++) { // Possibly emit submap name: @@ -422,8 +431,8 @@ ConfSimple::sortwalk(WalkerCode (*walker)(void *,const string&,const string&), return WALK_STOP; // Walk submap - map &sm = sit->second; - for (map::iterator it = sm.begin();it != sm.end(); + const map &sm = sit->second; + for (map::const_iterator it = sm.begin();it != sm.end(); it++) { if (walker(clidata, it->first, it->second) == WALK_STOP) return WALK_STOP; @@ -505,19 +514,19 @@ bool ConfSimple::write(ostream& out) const return true; } -void ConfSimple::showall() +void ConfSimple::showall() const { if (!ok()) return; write(std::cout); } -vector ConfSimple::getNames(const string &sk, const char *pattern) +vector ConfSimple::getNames(const string &sk, const char *pattern) const { vector mylist; if (!ok()) return mylist; - map >::iterator ss; + map >::const_iterator ss; if ((ss = m_submaps.find(sk)) == m_submaps.end()) { return mylist; } @@ -531,20 +540,20 @@ vector ConfSimple::getNames(const string &sk, const char *pattern) return mylist; } -vector ConfSimple::getSubKeys() +vector ConfSimple::getSubKeys() const { vector mylist; if (!ok()) return mylist; mylist.reserve(m_submaps.size()); - map >::iterator ss; + map >::const_iterator ss; for (ss = m_submaps.begin(); ss != m_submaps.end(); ss++) { mylist.push_back(ss->first); } return mylist; } -bool ConfSimple::hasNameAnywhere(const string& nm) +bool ConfSimple::hasNameAnywhere(const string& nm) const { vectorkeys = getSubKeys(); for (vector::const_iterator it = keys.begin(); diff --git a/src/utils/conftree.h b/src/utils/conftree.h index 3750fadd..8bbe1bfd 100644 --- a/src/utils/conftree.h +++ b/src/utils/conftree.h @@ -97,18 +97,18 @@ public: virtual ~ConfNull() {}; virtual int get(const string &name, string &value, const string &sk = string()) const = 0; - virtual bool hasNameAnywhere(const string& nm) = 0; + virtual bool hasNameAnywhere(const string& nm) const = 0; virtual int set(const string &nm, const string &val, const string &sk = string()) = 0; virtual bool ok() const = 0; - virtual vector getNames(const string &sk, const char* = 0) = 0; + virtual vector 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() {}; - virtual vector getSubKeys() = 0; - virtual vector getSubKeys(bool) = 0; + virtual void showall() const {}; + virtual vector getSubKeys() const = 0; + virtual vector getSubKeys(bool) const = 0; virtual bool holdWrites(bool) = 0; - virtual bool sourceChanged() = 0; + virtual bool sourceChanged() const = 0; }; /** @@ -143,7 +143,7 @@ public: virtual ~ConfSimple() {}; /** Origin file changed. Only makes sense if we read the data from a file */ - virtual bool sourceChanged(); + virtual bool sourceChanged() const; /** * Decide if we actually rewrite the backing-store after modifying the @@ -197,30 +197,35 @@ public: virtual WalkerCode sortwalk(WalkerCode (*wlkr)(void *cldata, const string &nm, const string &val), - void *clidata); + void *clidata) const; /** Print all values to stdout */ - virtual void showall(); + virtual void showall() const; /** Return all names in given submap. */ - virtual vector getNames(const string &sk, const char *pattern = 0); + virtual vector 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 */ - virtual bool hasNameAnywhere(const string& nm); + virtual bool hasNameAnywhere(const string& nm) const; /** * Return all subkeys */ - virtual vector getSubKeys(bool) {return getSubKeys();} - virtual vector getSubKeys(); + virtual vector getSubKeys(bool) const + { + return getSubKeys(); + } + virtual vector getSubKeys() const; /** Test for subkey existence */ - virtual bool hasSubKey(const string& sk) + virtual bool hasSubKey(const string& sk) const { return m_submaps.find(sk) != m_submaps.end(); } - virtual string getFilename() {return m_filename;} + virtual string getFilename() const + {return m_filename;} /** * Copy constructor. Expensive but less so than a full rebuild @@ -306,7 +311,8 @@ public: : 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; } @@ -373,7 +379,7 @@ public: return *this; } - virtual bool sourceChanged() + virtual bool sourceChanged() const { typename vector::const_iterator it; for (it = m_confs.begin();it != m_confs.end();it++) { @@ -393,9 +399,9 @@ public: return false; } - virtual bool hasNameAnywhere(const string& nm) + virtual bool hasNameAnywhere(const string& nm) const { - typename vector::iterator it; + typename vector::const_iterator it; for (it = m_confs.begin();it != m_confs.end();it++) { if ((*it)->hasNameAnywhere(nm)) return true; @@ -448,21 +454,23 @@ public: } virtual vector getNames(const string &sk, const char *pattern = 0) + const { return getNames1(sk, pattern, false); } - virtual vector getNamesShallow(const string &sk, const char *patt = 0) + virtual vector getNamesShallow(const string &sk, + const char *patt = 0) const { return getNames1(sk, patt, true); } virtual vector getNames1(const string &sk, const char *pattern, - bool shallow) + bool shallow) const { vector nms; - typename vector::iterator it; + typename vector::const_iterator it; bool skfound = false; - for (it = m_confs.begin();it != m_confs.end(); it++) { + for (it = m_confs.begin(); it != m_confs.end(); it++) { if ((*it)->hasSubKey(sk)) { skfound = true; vector lst = (*it)->getNames(sk, pattern); @@ -477,12 +485,15 @@ public: return nms; } - virtual vector getSubKeys(){return getSubKeys(false);} - virtual vector getSubKeys(bool shallow) + virtual vector getSubKeys() const + { + return getSubKeys(false); + } + virtual vector getSubKeys(bool shallow) const { vector sks; - typename vector::iterator it; - for (it = m_confs.begin();it != m_confs.end(); it++) { + typename vector::const_iterator it; + for (it = m_confs.begin(); it != m_confs.end(); it++) { vector lst; lst = (*it)->getSubKeys(); sks.insert(sks.end(), lst.begin(), lst.end());