OFile& OFile::open(const std::string&path) { plumed_assert(!cloned); eof=false; err=false; fp=NULL; gzfp=NULL; this->path=path; this->path=appendSuffix(path,getSuffix()); if(checkRestart()) { fp=std::fopen(const_cast<char*>(this->path.c_str()),"a"); mode="a"; if(Tools::extension(this->path)=="gz") { #ifdef __PLUMED_HAS_ZLIB gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"a9"); #else plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked"); #endif } } else { backupFile( backstring, this->path ); if(comm)comm->Barrier(); fp=std::fopen(const_cast<char*>(this->path.c_str()),"w"); mode="w"; if(Tools::extension(this->path)=="gz") { #ifdef __PLUMED_HAS_ZLIB gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"w9"); #else plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked"); #endif } } if(plumed) plumed->insertFile(*this); return *this; }
bool FileBase::FileExist(const std::string& path){ FILE *ff=NULL; bool do_exist=false; this->path=appendSuffix(path,getSuffix()); mode="r"; 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"); mode="r"; } if(ff) {do_exist=true; fclose(ff);} if(comm) comm->Barrier(); return do_exist; }
void OFile::backupAllFiles( const std::string& str ) { if(str=="/dev/null") return; plumed_assert( backstring!="bck" && !checkRestart()); size_t found=str.find_last_of("/\\"); 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); for(int i=0;; i++) { std::string num; Tools::convert(i,num); std::string filestr = directory + backstring + "." + num + "." + file; if( !FileExist(filestr) ) break; backupFile( "bck", filestr); } }