All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FileBase.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "File.h"
23 #include "Exception.h"
24 #include "core/Action.h"
25 #include "core/PlumedMain.h"
26 #include "core/Value.h"
27 #include "Communicator.h"
28 #include "Tools.h"
29 #include <cstdarg>
30 #include <cstring>
31 
32 #include <iostream>
33 #include <string>
34 
35 namespace PLMD{
36 
38  PLMD::OFile pof;
39  pof.open("ciao");
40  pof.printf("%s\n","test1");
41  pof.setLinePrefix("plumed: ");
42  pof.printf("%s\n","test2");
43  pof.setLinePrefix("");
44  pof.addConstantField("x2").printField("x2",67.0);
45  pof.printField("x1",10.0).printField("x3",20.12345678901234567890).printField();
46  pof.printField("x1",10.0).printField("x3",-1e70*20.12345678901234567890).printField();
47  pof.printField("x3",10.0).printField("x2",777.0).printField("x1",-1e70*20.12345678901234567890).printField();
48  pof.printField("x3",67.0).printField("x1",18.0).printField();
49  pof.close();
50 
51  PLMD::IFile pif;
52  std::string s;
53  pif.open("ciao");
54  pif.getline(s); std::printf("%s\n",s.c_str());
55  pif.getline(s); std::printf("%s\n",s.c_str());
56 
57  int x1,x2,x3;
58  while(pif.scanField("x1",x1).scanField("x3",x2).scanField("x2",x3).scanField()){
59  std::cout<<"CHECK "<<x1<<" "<<x2<<" "<<x3<<"\n";
60  }
61  pif.close();
62 }
63 
65  this->fp=fp;
66  cloned=true;
67  return *this;
68 }
69 
71  fflush(fp);
72  if(heavyFlush){
73  fclose(fp);
74  fp=std::fopen(const_cast<char*>(path.c_str()),"a");
75  }
76  return *this;
77 }
78 
80  plumed_massert(!fp,"cannot link an already open file");
81  this->comm=&comm;
82  return *this;
83 }
84 
86  plumed_massert(!fp,"cannot link an already open file");
87  this->plumed=&plumed;
88  link(plumed.comm);
89  return *this;
90 }
91 
93  plumed_massert(!fp,"cannot link an already open file");
94  this->action=&action;
95  link(action.plumed);
96  return *this;
97 }
98 
99 FileBase& FileBase::open(const std::string& path,const std::string& mode){
100  plumed_massert(!cloned,"file "+path+" appears to be cloned");
101  eof=false;
102  err=false;
103  fp=NULL;
104  if(plumed){
105  this->path=path+plumed->getSuffix();
106  fp=std::fopen(const_cast<char*>(this->path.c_str()),const_cast<char*>(mode.c_str()));
107  }
108  if(!fp){
109  this->path=path;
110  fp=std::fopen(const_cast<char*>(this->path.c_str()),const_cast<char*>(mode.c_str()));
111  }
112  if(plumed) plumed->insertFile(*this);
113  plumed_massert(fp,"file " + path + "cannot be found");
114  return *this;
115 }
116 
117 
118 bool FileBase::FileExist(const std::string& path){
119  FILE *ff=NULL;
120  bool do_exist=false;
121  if(plumed){
122  this->path=path+plumed->getSuffix();
123  ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
124  }
125  if(!ff){
126  this->path=path;
127  ff=std::fopen(const_cast<char*>(this->path.c_str()),"r");
128  }
129  if(ff) {do_exist=true; fclose(ff);}
130  if(comm) comm->Barrier();
131  return do_exist;
132 }
133 
135  bool isopen=false;
136  if(fp) isopen=true;
137  return isopen;
138 }
139 
141  plumed_assert(!cloned);
142  eof=false;
143  err=false;
144  std::fclose(fp);
145  fp=NULL;
146 }
147 
149  fp(NULL),
150  comm(NULL),
151  plumed(NULL),
152  action(NULL),
153  cloned(false),
154  eof(false),
155  err(false),
156  heavyFlush(false)
157 {
158 }
159 
161 {
162  if(plumed) plumed->eraseFile(*this);
163  if(!cloned && fp) fclose(fp);
164 }
165 
166 FileBase::operator bool()const{
167  return !eof;
168 }
169 
170 }
std::string path
path of the opened file
Definition: FileBase.h:75
IFile & getline(std::string &)
Get a full line as a string.
Definition: IFile.cpp:169
Action * action
pointer to corresponding action. NULL if not linked
Definition: FileBase.h:63
FileBase & link(FILE *)
Link to an already open filed.
Definition: FileBase.cpp:64
virtual ~FileBase()
Virtual destructor (allows inheritance)
Definition: FileBase.cpp:160
Class containing wrappers to MPI.
Definition: Communicator.h:44
static void test()
Runs a small testcase.
Definition: FileBase.cpp:37
OFile & addConstantField(const std::string &)
Definition: OFile.cpp:120
FileBase & flush()
Flushes the file to disk.
Definition: FileBase.cpp:70
Communicator * comm
communicator. NULL if not set
Definition: FileBase.h:59
IFile & open(const std::string &name)
Opens the file.
Definition: IFile.cpp:90
bool heavyFlush
Set to true if you want flush to be heavy (close/reopen)
Definition: FileBase.h:77
FileBase()
Private constructor.
Definition: FileBase.cpp:148
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
bool eof
Set to true when end of file is encountered.
Definition: FileBase.h:71
void close()
Closes the file Should be used only for explicitely opened files.
Definition: FileBase.cpp:140
Base class for all the input Actions.
Definition: Action.h:60
bool isOpen()
Check if a file is open.
Definition: FileBase.cpp:134
FileBase & open(const std::string &name, const std::string &mode)
Opens the file (without auto-backup)
Definition: FileBase.cpp:99
bool cloned
Control closing on destructor.
Definition: FileBase.h:66
Communicator & comm
Communicator for plumed.
Definition: PlumedMain.h:77
PlumedMain & plumed
Reference to main plumed object.
Definition: Action.h:90
Class for input files.
Definition: IFile.h:40
FILE * fp
file pointer
Definition: FileBase.h:57
IFile & scanField(const std::string &, double &)
Read a double field.
Definition: IFile.cpp:121
Class for output files.
Definition: OFile.h:84
bool FileExist(const std::string &path)
Check if the file exists.
Definition: FileBase.cpp:118
bool err
Set to true when error is encountered.
Definition: FileBase.h:73
OFile & printField(const std::string &, double)
Set the value of a double precision field.
Definition: OFile.cpp:145
Main plumed object.
Definition: PlumedMain.h:71
Main plumed object.
Definition: Plumed.h:201
void Barrier() const
Wrapper to MPI_Barrier.
Base class for dealing with files.
Definition: FileBase.h:39
PlumedMain * plumed
pointer to main plumed object. NULL if not linked
Definition: FileBase.h:61
OFile & setLinePrefix(const std::string &)
Set the prefix for output.
Definition: OFile.cpp:77
OFile & open(const std::string &name)
Opens the file using automatic append/backup.
Definition: OFile.cpp:264