From ada070d013366fdb7ab5375825555e192ab79e5b Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Wed, 8 Jan 2014 13:29:59 +0100
Subject: [PATCH] Eliminate File::open(path,mode)

Mode is redundant since:
* read or write can be decided depending if file is IFile or OFile
* append/non-append is decided based on RESTART option
---
 src/analysis/Analysis.cpp | 11 ++++-------
 src/core/PlumedMain.cpp   |  2 +-
 src/tools/FileBase.cpp    | 20 --------------------
 src/tools/FileBase.h      |  4 ++--
 src/tools/IFile.cpp       | 19 +++++++++++++++++--
 5 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/src/analysis/Analysis.cpp b/src/analysis/Analysis.cpp
index d78a42337..096e6522e 100644
--- a/src/analysis/Analysis.cpp
+++ b/src/analysis/Analysis.cpp
@@ -292,7 +292,10 @@ void Analysis::finalizeWeights( const bool& ignore_weights ){
 void Analysis::runAnalysis(){
 
   // close the restart file so it is flushed
-  if( write_chq ) rfile.close();   
+  if( write_chq ){
+    rfile.flush();
+    rfile.rewind();
+  }
 
   // Note : could add multiple walkers here - simply read in the data from all
   // other walkers here if we are writing the check points.
@@ -309,12 +312,6 @@ void Analysis::runAnalysis(){
   // Update total normalization constant
   old_norm+=norm; firstAnalysisDone=true;
 
-  // Delete the checkpoint file
-  if( write_chq ){
-     std::string filename = getName() + "_" + getLabel() + ".chkpnt";
-     // If we are running more than one calculation only reopen the restart file
-     if( !single_run ) rfile.open( filename.c_str(), "w+" ); 
-  }
 }
 
 double Analysis::getNormalization() const {
diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp
index 4ff03880c..f1dcac9b1 100644
--- a/src/core/PlumedMain.cpp
+++ b/src/core/PlumedMain.cpp
@@ -277,7 +277,7 @@ void PlumedMain::cmd(const std::string & word,void*val){
   } else if(word=="setLogFile"){
        CHECK_NOTINIT(initialized,word);
        CHECK_NULL(val,word);
-       log.open(static_cast<char*>(val),"w");
+       log.open(static_cast<char*>(val));
 // other commands that should be used after initialization:
   } else if(word=="setStopFlag"){
        CHECK_INIT(initialized,word);
diff --git a/src/tools/FileBase.cpp b/src/tools/FileBase.cpp
index 0df6860be..6a394d00f 100644
--- a/src/tools/FileBase.cpp
+++ b/src/tools/FileBase.cpp
@@ -97,26 +97,6 @@ FileBase& FileBase::link(Action&action){
   return *this;
 }
 
-FileBase& FileBase::open(const std::string& path,const std::string& mode){
-  plumed_massert(!cloned,"file "+path+" appears to be cloned");
-  eof=false;
-  err=false;
-  fp=NULL;
-  gzfp=NULL;
-  bool do_exist=FileExist(path);
-  fp=std::fopen(const_cast<char*>(this->path.c_str()),const_cast<char*>(mode.c_str()));
-  if(Tools::extension(this->path)=="gz"){
-#ifdef __PLUMED_HAS_ZLIB
-    gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),const_cast<char*>(mode.c_str()));
-#else
-    plumed_merror("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;
diff --git a/src/tools/FileBase.h b/src/tools/FileBase.h
index 2cbd3b499..f1f019f20 100644
--- a/src/tools/FileBase.h
+++ b/src/tools/FileBase.h
@@ -105,8 +105,8 @@ public:
   operator bool () const;
 /// Set heavyFlush flag
   void setHeavyFlush(){ heavyFlush=true;}
-/// Opens the file (without auto-backup)
-  FileBase& open(const std::string&name,const std::string& mode);
+/// Opens the file
+  virtual FileBase& open(const std::string&name)=0;
 /// Check if the file exists
   bool FileExist(const std::string& path);
 /// Check if a file is open
diff --git a/src/tools/IFile.cpp b/src/tools/IFile.cpp
index fc95ecfe3..0a3ae3bf6 100644
--- a/src/tools/IFile.cpp
+++ b/src/tools/IFile.cpp
@@ -101,8 +101,23 @@ IFile& IFile::advanceField(){
   return *this;
 }
 
-IFile& IFile::open(const std::string&name){
-  FileBase::open(name,"r");
+IFile& IFile::open(const std::string&path){
+  plumed_massert(!cloned,"file "+path+" appears to be cloned");
+  eof=false;
+  err=false;
+  fp=NULL;
+  gzfp=NULL;
+  bool do_exist=FileExist(path);
+  plumed_massert(do_exist,"file " + path + "cannot be found");
+  fp=std::fopen(const_cast<char*>(this->path.c_str()),"r");
+  if(Tools::extension(this->path)=="gz"){
+#ifdef __PLUMED_HAS_ZLIB
+    gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"r");
+#else
+    plumed_merror("trying to use a gz file without zlib being linked");
+#endif
+  }
+  if(plumed) plumed->insertFile(*this);
   return *this;
 }
 
-- 
GitLab