From a09f101f5c05dedbe13dd80537b1eb1eed48ecba Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Fri, 6 Feb 2015 13:14:27 +0100 Subject: [PATCH] Improved file tools It is now possible to enforce the suffix of a file. Additionally, there is a check so that files named "/dev/null" are not added any suffix nor backed up. These changes are useful to implement single-file multiple walkers --- src/tools/FileBase.cpp | 21 +++++++++++++++------ src/tools/FileBase.h | 10 ++++++++++ src/tools/OFile.cpp | 8 ++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/tools/FileBase.cpp b/src/tools/FileBase.cpp index b9eb0277d..7375b64bf 100644 --- a/src/tools/FileBase.cpp +++ b/src/tools/FileBase.cpp @@ -100,10 +100,8 @@ FileBase& FileBase::link(Action&action){ bool FileBase::FileExist(const std::string& path){ FILE *ff=NULL; bool do_exist=false; - if(plumed){ - this->path=appendSuffix(path,plumed->getSuffix()); - ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); - } + this->path=appendSuffix(path,getSuffix()); + ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); if(!ff){ this->path=path; ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); @@ -140,7 +138,8 @@ FileBase::FileBase(): cloned(false), eof(false), err(false), - heavyFlush(false) + heavyFlush(false), + enforcedSuffix_(false) { } @@ -158,6 +157,7 @@ FileBase::operator bool()const{ } std::string FileBase::appendSuffix(const std::string&path,const std::string&suffix){ + if(path=="/dev/null") return path; // do not append a suffix to /dev/null std::string ret=path; std::string ext=Tools::extension(path); if(ext=="gz"){ @@ -170,7 +170,16 @@ std::string FileBase::appendSuffix(const std::string&path,const std::string&suff return ret; } +FileBase& FileBase::enforceSuffix(const std::string&suffix){ + enforcedSuffix_=true; + enforcedSuffix=suffix; + return *this; +} - +std::string FileBase::getSuffix()const{ + if(enforcedSuffix_) return enforcedSuffix; + if(plumed) return plumed->getSuffix(); + return ""; +} } diff --git a/src/tools/FileBase.h b/src/tools/FileBase.h index 4c2b71d72..ef464e268 100644 --- a/src/tools/FileBase.h +++ b/src/tools/FileBase.h @@ -81,6 +81,11 @@ protected: /// It appends the desired suffix to the string. Notice that /// it conserves a possible ".gz" suffix. static std::string appendSuffix(const std::string&path,const std::string&suffix); +private: +/// Enforced suffix: + std::string enforcedSuffix; +/// If true, use enforcedSuffix, else get it from PlumedMain + bool enforcedSuffix_; public: /// Link to an already open filed FileBase& link(FILE*); @@ -92,6 +97,9 @@ public: /// Link to an Action object. /// Automatically links also the corresponding PlumedMain and Communicator. FileBase& link(Action&); +/// Enforce suffix. +/// Overrides the one set in PlumedMain& + FileBase& enforceSuffix(const std::string&suffix); /// Flushes the file to disk virtual FileBase& flush(); /// Closes the file @@ -111,6 +119,8 @@ public: bool FileExist(const std::string& path); /// Check if a file is open bool isOpen(); +/// Get the file suffix + std::string getSuffix()const; }; diff --git a/src/tools/OFile.cpp b/src/tools/OFile.cpp index 25312b5d1..811bf7808 100644 --- a/src/tools/OFile.cpp +++ b/src/tools/OFile.cpp @@ -234,9 +234,10 @@ void OFile::setBackupString( const std::string& str ){ } void OFile::backupAllFiles( const std::string& str ){ + if(str=="/dev/null") return; plumed_assert( backstring!="bck" && plumed && !plumed->getRestart() ); size_t found=str.find_last_of("/\\"); - std::string filename = appendSuffix(str,plumed->getSuffix()); + std::string filename = appendSuffix(str,getSuffix()); std::string directory=filename.substr(0,found+1); std::string file=filename.substr(found+1); if( FileExist(filename) ) backupFile("bck", filename); @@ -249,6 +250,7 @@ void OFile::backupAllFiles( const std::string& str ){ } void OFile::backupFile( const std::string& bstring, const std::string& fname ){ + if(fname=="/dev/null") return; int maxbackup=100; if(std::getenv("PLUMED_MAXBACKUP")) Tools::convert(std::getenv("PLUMED_MAXBACKUP"),maxbackup); if(maxbackup>0 && (!comm || comm->Get_rank()==0)){ @@ -282,9 +284,7 @@ OFile& OFile::open(const std::string&path){ fp=NULL; gzfp=NULL; this->path=path; - if(plumed){ - this->path=appendSuffix(path,plumed->getSuffix()); - } + this->path=appendSuffix(path,getSuffix()); if(checkRestart()){ fp=std::fopen(const_cast<char*>(this->path.c_str()),"a"); if(Tools::extension(this->path)=="gz"){ -- GitLab