#include #include "xmlToolDir.hh" #include "ExpatInterface/Element.h" // See comments in the include file. namespace xmlTool { Directory::Directory(const xml::Element & elt, const std::string & tag, const std::string & attName) : _elt(elt), _tag(tag), _attName(attName) { _cwd.push_back(&elt); } const xml::Element & Directory::getCurDirElt() const { return **( _cwd.end()-1 ); } const std::string Directory::getCurDirName() const { const xml::Element & curDirElt = this->getCurDirElt(); return ( (&curDirElt != &_elt)?curDirElt[this->_attName]:string("/") ); } const std::vector Directory::getDirNames() const { std::vector dirs; const xml::Element & curDirElt = this->getCurDirElt(); for(xml::Element::ElementCollection::const_iterator it = curDirElt.getChildren().begin(); it != curDirElt.getChildren().end(); it++) { if ((**it).getTagName()==this->_tag) { dirs.push_back((**it)[this->_attName]); } } return dirs; } const bool Directory::changeDir(const std::string & newDirName) { if (newDirName == string("") || newDirName == string("/")) { // Reset to "root" or "home" directory. this->_cwd.erase(this->_cwd.begin()+1, this->_cwd.end()); return true; } else if (newDirName == string("..")) { if (this->_cwd.size() > 1) { this->_cwd.pop_back(); } return true; } else { // So the user is trying to go to a named directory. const xml::Element & curDirElt = this->getCurDirElt(); for(xml::Element::ElementCollection::const_iterator it = curDirElt.getChildren().begin(); it != curDirElt.getChildren().end(); it++) { if ((**it).getTagName()==this->_tag) { if ((**it)[this->_attName] == newDirName) { this->_cwd.push_back(*it); return true; } // end name if } // end tag if } // end for } // end if // If we got this far, then newDirName is not a // valid directory to change to. std::cerr << "*** WARNING *** changeDir failed. " << newDirName << " is not in " << this->getCurDirName() << endl; return false; } // end changeDir() } // end of namespace