Skip to content
Snippets Groups Projects
Commit a09f101f authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

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
parent c3b95dd1
No related branches found
No related tags found
No related merge requests found
...@@ -100,10 +100,8 @@ FileBase& FileBase::link(Action&action){ ...@@ -100,10 +100,8 @@ FileBase& FileBase::link(Action&action){
bool FileBase::FileExist(const std::string& path){ bool FileBase::FileExist(const std::string& path){
FILE *ff=NULL; FILE *ff=NULL;
bool do_exist=false; bool do_exist=false;
if(plumed){ this->path=appendSuffix(path,getSuffix());
this->path=appendSuffix(path,plumed->getSuffix()); ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
}
if(!ff){ if(!ff){
this->path=path; this->path=path;
ff=std::fopen(const_cast<char*>(this->path.c_str()),"r"); ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
...@@ -140,7 +138,8 @@ FileBase::FileBase(): ...@@ -140,7 +138,8 @@ FileBase::FileBase():
cloned(false), cloned(false),
eof(false), eof(false),
err(false), err(false),
heavyFlush(false) heavyFlush(false),
enforcedSuffix_(false)
{ {
} }
...@@ -158,6 +157,7 @@ FileBase::operator bool()const{ ...@@ -158,6 +157,7 @@ FileBase::operator bool()const{
} }
std::string FileBase::appendSuffix(const std::string&path,const std::string&suffix){ 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 ret=path;
std::string ext=Tools::extension(path); std::string ext=Tools::extension(path);
if(ext=="gz"){ if(ext=="gz"){
...@@ -170,7 +170,16 @@ std::string FileBase::appendSuffix(const std::string&path,const std::string&suff ...@@ -170,7 +170,16 @@ std::string FileBase::appendSuffix(const std::string&path,const std::string&suff
return ret; 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 "";
}
} }
...@@ -81,6 +81,11 @@ protected: ...@@ -81,6 +81,11 @@ protected:
/// It appends the desired suffix to the string. Notice that /// It appends the desired suffix to the string. Notice that
/// it conserves a possible ".gz" suffix. /// it conserves a possible ".gz" suffix.
static std::string appendSuffix(const std::string&path,const std::string&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: public:
/// Link to an already open filed /// Link to an already open filed
FileBase& link(FILE*); FileBase& link(FILE*);
...@@ -92,6 +97,9 @@ public: ...@@ -92,6 +97,9 @@ public:
/// Link to an Action object. /// Link to an Action object.
/// Automatically links also the corresponding PlumedMain and Communicator. /// Automatically links also the corresponding PlumedMain and Communicator.
FileBase& link(Action&); FileBase& link(Action&);
/// Enforce suffix.
/// Overrides the one set in PlumedMain&
FileBase& enforceSuffix(const std::string&suffix);
/// Flushes the file to disk /// Flushes the file to disk
virtual FileBase& flush(); virtual FileBase& flush();
/// Closes the file /// Closes the file
...@@ -111,6 +119,8 @@ public: ...@@ -111,6 +119,8 @@ public:
bool FileExist(const std::string& path); bool FileExist(const std::string& path);
/// Check if a file is open /// Check if a file is open
bool isOpen(); bool isOpen();
/// Get the file suffix
std::string getSuffix()const;
}; };
......
...@@ -234,9 +234,10 @@ void OFile::setBackupString( const std::string& str ){ ...@@ -234,9 +234,10 @@ void OFile::setBackupString( const std::string& str ){
} }
void OFile::backupAllFiles( const std::string& str ){ void OFile::backupAllFiles( const std::string& str ){
if(str=="/dev/null") return;
plumed_assert( backstring!="bck" && plumed && !plumed->getRestart() ); plumed_assert( backstring!="bck" && plumed && !plumed->getRestart() );
size_t found=str.find_last_of("/\\"); 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 directory=filename.substr(0,found+1);
std::string file=filename.substr(found+1); std::string file=filename.substr(found+1);
if( FileExist(filename) ) backupFile("bck", filename); if( FileExist(filename) ) backupFile("bck", filename);
...@@ -249,6 +250,7 @@ void OFile::backupAllFiles( const std::string& str ){ ...@@ -249,6 +250,7 @@ void OFile::backupAllFiles( const std::string& str ){
} }
void OFile::backupFile( const std::string& bstring, const std::string& fname ){ void OFile::backupFile( const std::string& bstring, const std::string& fname ){
if(fname=="/dev/null") return;
int maxbackup=100; int maxbackup=100;
if(std::getenv("PLUMED_MAXBACKUP")) Tools::convert(std::getenv("PLUMED_MAXBACKUP"),maxbackup); if(std::getenv("PLUMED_MAXBACKUP")) Tools::convert(std::getenv("PLUMED_MAXBACKUP"),maxbackup);
if(maxbackup>0 && (!comm || comm->Get_rank()==0)){ if(maxbackup>0 && (!comm || comm->Get_rank()==0)){
...@@ -282,9 +284,7 @@ OFile& OFile::open(const std::string&path){ ...@@ -282,9 +284,7 @@ OFile& OFile::open(const std::string&path){
fp=NULL; fp=NULL;
gzfp=NULL; gzfp=NULL;
this->path=path; this->path=path;
if(plumed){ this->path=appendSuffix(path,getSuffix());
this->path=appendSuffix(path,plumed->getSuffix());
}
if(checkRestart()){ if(checkRestart()){
fp=std::fopen(const_cast<char*>(this->path.c_str()),"a"); fp=std::fopen(const_cast<char*>(this->path.c_str()),"a");
if(Tools::extension(this->path)=="gz"){ if(Tools::extension(this->path)=="gz"){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment