// ---------------------------------------------------------------------- // // HepDir.cc - implementation of HepFileManager directories // // HepFileManager::absStart indicates the start of an absolute path // HepFileManager::dirSep delimitates directory level in a path // // History: // 24-Jul-1998 Philippe Canal Initial draft // 21-Feb-2000 Walter Brown Improved C++ standard compliance // // ---------------------------------------------------------------------- #ifndef HEPTRACE_H #include "HepTuple/HepTrace.h" #endif #ifndef HEPDIR_H #include "HepTuple/HepDir.h" #endif #ifndef HEPFILEMANAGER_H #include "HepTuple/HepFileManager.h" #endif #include "ZMutility/iostream" USING( namespace std ) ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ // type identifier ( is equal to type() for this object ) const char HepDir::typeId = 'D'; #define BADCALL(fctnName) \ ZMthrow(ZMxHepUnsupported("your directory class does not support the "\ #fctnName " call.")); HepDir::HepDir( // constructor HepFileManager * mgr, HepDir* parent, const string& title ) : HepObj( mgr, title, 0 ) { HEP_DEBUG( "HepDir::constructor( \"" << title << "\", " << *id_ << " )" ); type_ = 'D'; up_ = parent; // the signIn NEED to be last so that the manager has a proper object to // deal with mgr->signIn( this ); } // Used for dummy construction ... // the object is then not valid ... HepDir::HepDir() : HepObj() { HEP_DEBUG( "HepDir::constructor( )" ); type_ = 'D'; } HepDir::~HepDir() { // destructor HEP_DEBUG( "HepDir::destructor( \"" << *title_ << "\", " << *id_ << " )" ); while ( ! down_.empty() ) { HepObj * o = down_.back(); // force the actual deletion of the HepObj by claiming that // no-one is using it anymore o->isNowUnmanaged(); o->isInUse(false); delete o; down_.pop_back(); } } string HepDir::absTitle() const { string nrv = up_ ? up_->absTitle() + HepFileManager::dirSep + *title_ : HepFileManager::absStart + *title_; return nrv; } HepDir * HepDir::makeDirClone( // clone existing directory type HepDir* parent, const string& title ) { return new HepDir( mgr_, parent, title); } // ************************************************************************ // // get a sub directory HepDir* HepDir::getsubdir( const string& path ) const { if ( ! isRelPath( path ) ) { ZMthrow(ZMxHepImproperUse( string("HepDir::getsubdir need a relative path instead of ")+ path)); return NULL; } bool last_level = false; string top = topname(path); if ( top.length() == path.length() ) { // we just have a name top = path; last_level = true; } if ( top == ".." ) { if ( up_ == 0 ) { ZMthrow( ZMxHepUnknownHepObj( string( "No directory \"" ) + absTitle() + HepFileManager::dirSep + top + "\"" ) ); return NULL; } if ( last_level ) { return up_; } else { return up_->getsubdir( path.substr(top.length()+1, path.length())); } } HepObjList::const_iterator i = findDir( top ); if ( i == down_.end() ) { ZMthrow( ZMxHepUnknownHepObj( string( "No directory \"" ) + absTitle() + HepFileManager::dirSep + top + "\"" ) ); return NULL; } if ( last_level ) { return ((HepDir*)(*i)); } else { return ((HepDir*)(*i))->getsubdir( path.substr(top.length()+1, path.length())); } } // ************************************************************************ // // make a sub directory HepDir* HepDir::mksubdir( const string& path ) { if ( ! isRelPath( path ) ) { ZMthrow(ZMxHepImproperUse( string("HepDir::mkdirsubdir need a relative path instead of ")+ path)); return NULL; } string top = topname(path); string base = basename(path); if ( top.length() == path.length() ) { // we just have a name if ( findDir( path ) != down_.end() ) { ZMthrow(ZMxHepCantMakeDir(absTitle()+HepFileManager::dirSep+path+ " already exists. Did you forget to delete an old file?")); return NULL; } HepDir* newDir = makeDirClone( this, path.c_str() ); down_.push_back( newDir ); return newDir; } else { // we have a composite name and we ask the sub-directory of this directory // to create the new directory. HepObjList::const_iterator i = findDir( top ); if ( i == down_.end() ) { ZMthrow( ZMxHepUnknownHepObj( string( "No directory \"" ) + absTitle() + HepFileManager::dirSep + top + "\"" ) ); return NULL; } return ((HepDir*)(*i))->mksubdir( path.substr(top.length()+1, path.length() ) ); } } // ************************************************************************ // // make a sub directory void HepDir::rmdir(HepDir* dir) { HEP_DEBUG( "HepDir::rmdir( \"" << dir->absTitle() << "\" )" ); for ( HepObjList::iterator i = down_.begin() ; i != down_.end() ; ++i ) { HepObj * o = *i; if ( o == dir ) { o->isNowUnmanaged(); o->isInUse(false); delete o; down_.erase(i); return; } } ZMthrow(ZMxHepBadRequest("In HepDir::rmdir "+dir->absTitle() +" is not a sub-directory of " +absTitle())); } // ************************************************************************ // // find a item in this directory HepDir::HepObjList::const_iterator HepDir::findDir( const string & title ) const { HEP_DEBUG( "HepDir::findDir( \"" << title << "\" )" ); for ( HepObjList::const_iterator i = down_.begin() ; i != down_.end() ; ++i ) { HepObj * o = *i; HEP_DEBUG( " comparing to \"" << *(o->title_) << "\" (" << *(o->id_) << ")" ); if ( *(o->title_) == title ) return i; } return down_.end(); } // HepFileManager::findDir() HepDir::HepObjList::const_iterator HepDir::findObj( const string & title ) const { HEP_DEBUG( "HepDir::findObj( \"" << title << "\" )" ); for ( HepObjList::const_iterator i = objList_.begin() ; i != objList_.end() ; ++i ) { HepObj * o = *i; HEP_DEBUG( " comparing to \"" << *(o->title_) << "\" (" << *(o->id_) << ")" ); if ( *(o->title_) == title ) return i; } return objList_.end(); } // HepFileManager::findObj() HepDir::HepObjList::const_iterator HepDir::findObj( int uid ) const { HEP_DEBUG( "HepDir::findObj( \"" << uid << "\" )" ); for ( HepObjList::const_iterator i = objList_.begin() ; i != objList_.end() ; ++i ) { HepObj * o = *i; HEP_DEBUG( " comparing to \"" << *(o->title_) << "\" (" << *(o->id_) << ")" ); if ( *o->id_ == uid ) return i; } return objList_.end(); } // HepFileManager::findObj() ZM_END_NAMESPACE( zmht ) /* } // namespace zmht */