All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OFile.h
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 #ifndef __PLUMED_tools_OFile_h
23 #define __PLUMED_tools_OFile_h
24 
25 #include "FileBase.h"
26 #include <vector>
27 #include <sstream>
28 
29 namespace PLMD{
30 
31 class Value;
32 
33 /**
34 \ingroup TOOLBOX
35 Class for output files
36 
37 This class provides features similar to those in the standard C "FILE*" type,
38 but only for sequential output. See IFile for sequential input.
39 
40 See the example here for a possible use:
41 \verbatim
42 #include "File.h"
43 
44 int main(){
45  PLMD::OFile pof;
46  pof.open("ciao","w");
47  pof.printf("%s\n","test1");
48  pof.setLinePrefix("plumed: ");
49  pof.printf("%s\n","test2");
50  pof.setLinePrefix("");
51  pof.addConstantField("x2").printField("x2",67.0);
52  pof.printField("x1",10.0).printField("x3",20.12345678901234567890).printField();
53  pof.printField("x1",10.0).printField("x3",-1e70*20.12345678901234567890).printField();
54  pof.printField("x3",10.0).printField("x2",777.0).printField("x1",-1e70*20.12345678901234567890).printField();
55  pof.printField("x3",67.0).printField("x1",18.0).printField();
56  pof.close();
57  return 0;
58 }
59 \endverbatim
60 
61 This program is expected to produce a file "ciao" which reads
62 \verbatim
63 test1
64 plumed: test2
65 #! FIELDS x1 x3
66 #! SET x2 67
67  10 20.12345678901234
68  10 -2.012345678901235e+71
69 #! FIELDS x1 x3
70 #! SET x2 777
71  -2.012345678901235e+71 10
72  18 67
73 \endverbatim
74 
75 Notes
76 - "x2" is declared as "constant", which means that it is written using the "SET"
77 keyword. Thus, everytime it is modified, all the headers are repeated in the output file.
78 - printField() without arguments is used as a "newline".
79 - most methods return a reference to the OFile itself, to allow chaining many calls on the same line
80 (this is similar to << operator in std::ostream)
81 
82 */
83 
84 class OFile:
85 public virtual FileBase{
86 /// Pointer to a linked OFile.
87 /// see link(OFile&)
89 /// Internal buffer for printf
91 /// Internal buffer (generic use)
92  char* buffer;
93 /// Internal buffer length
94  int buflen;
95 /// This variables stores the actual buffer length
97 /// Class identifying a single field for fielded output
98  class Field:
99  public FieldBase{
100  };
101 /// Low-level write
102  size_t llwrite(const char*,size_t);
103 /// True if fields has changed.
104 /// This could be due to a change in the list of fields or a reset
105 /// of a nominally constant field
107 /// Format for fields writing
108  std::string fieldFmt;
109 /// All the previously defined variable fields
110  std::vector<Field> previous_fields;
111 /// All the defined variable fields
112  std::vector<Field> fields;
113 /// All the defined constant fields
114  std::vector<Field> const_fields;
115 /// Prefix for line (e.g. "PLUMED: ")
116  std::string linePrefix;
117 /// Temporary ostringstream for << output
118  std::ostringstream oss;
119 /// The string used for backing up files
120  std::string backstring;
121 /// Find field index given name
122  unsigned findField(const std::string&name)const;
123 public:
124 /// Constructor
125  OFile();
126 /// Destructor
127  ~OFile();
128 /// Allows overloading of link
129  using FileBase::link;
130 /// Allows overloading of open
131  using FileBase::open;
132 /// Allows linking this OFile to another one.
133 /// In this way, everything written to this OFile will be immediately
134 /// written on the linked OFile. Notice that a OFile should
135 /// be either opened explicitly, linked to a FILE or linked to a OFile
136  OFile& link(OFile&);
137 /// Set the string name to be used for automatic backup
138  void setBackupString( const std::string& );
139 /// Backup a file by giving it a different name
140  void backupFile( const std::string& bstring, const std::string& fname );
141 /// This backs up all the files that would have been created with the
142 /// name str. It is used in analysis when you are not restarting. Analysis
143 /// output files at different times, which are names analysis.0.<filename>,
144 /// analysis.1.<filename> and <filename>, are backed up to bck.0.analysis.0.<filename>,
145 /// bck.0.analysis.1.<filename> and bck.0.<filename>
146  void backupAllFiles( const std::string& str );
147 /// Opens the file using automatic append/backup
148  OFile& open(const std::string&name);
149 /// Set the prefix for output.
150 /// Typically "PLUMED: ". Notice that lines with a prefix cannot
151 /// be parsed using fields in a IFile.
152  OFile& setLinePrefix(const std::string&);
153 /// Set the format for writing double precision fields
154  OFile& fmtField(const std::string&);
155 /// Reset the format for writing double precision fields to its default
156  OFile& fmtField();
157 /// Set the value of a double precision field
158  OFile& printField(const std::string&,double);
159 /// Set the value of a int field
160  OFile& printField(const std::string&,int);
161 /// Set the value of a string field
162  OFile& printField(const std::string&,const std::string&);
163 ///
164  OFile& addConstantField(const std::string&);
165 /// Used to setup printing of values
166  OFile& setupPrintValue( Value *val );
167 /// Print a value
168  OFile& printField( Value* val, const double& v );
169 /** Close a line.
170 Typically used as
171 \verbatim
172  of.printField("a",a).printField("b",b).printField();
173 \endverbatim
174 */
175  OFile& printField();
176 /**
177 Resets the list of fields.
178 As it is only possible to add new constant fields (addConstantField()),
179 this method can be used to clean the field list.
180 */
181  OFile& clearFields();
182 /// Formatted output with explicit format - a la printf
183  int printf(const char*fmt,...);
184 /// Formatted output with << operator
185  template <class T>
186  friend OFile& operator<<(OFile&,const T &);
187 /// Rewind a file
188  OFile&rewind();
189 };
190 
191 /// Write using << syntax
192 template <class T>
193 OFile& operator<<(OFile&of,const T &t){
194  of.oss<<t;
195  of.printf("%s",of.oss.str().c_str());
196  of.oss.str("");
197  return of;
198 }
199 
200 
201 }
202 
203 #endif
std::string linePrefix
Prefix for line (e.g. "PLUMED: ")
Definition: OFile.h:116
unsigned findField(const std::string &name) const
Find field index given name.
~OFile()
Destructor.
Definition: OFile.cpp:66
OFile & rewind()
Rewind a file.
Definition: OFile.cpp:284
std::string backstring
The string used for backing up files.
Definition: OFile.h:120
void backupAllFiles(const std::string &str)
This backs up all the files that would have been created with the name str.
Definition: OFile.cpp:222
Class identifying a single field for fielded output.
Definition: OFile.h:98
OFile & fmtField()
Reset the format for writing double precision fields to its default.
Definition: OFile.cpp:140
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
FileBase & link(FILE *)
Link to an already open filed.
Definition: FileBase.cpp:64
char * buffer_string
Internal buffer for printf.
Definition: OFile.h:90
OFile & printField()
Close a line.
Definition: OFile.cpp:190
OFile & addConstantField(const std::string &)
Definition: OFile.cpp:120
std::vector< Field > previous_fields
All the previously defined variable fields.
Definition: OFile.h:110
std::ostringstream oss
Temporary ostringstream for << output.
Definition: OFile.h:118
int buflen
Internal buffer length.
Definition: OFile.h:94
size_t llwrite(const char *, size_t)
Low-level write.
Definition: OFile.cpp:39
OFile & link(OFile &)
Allows linking this OFile to another one.
Definition: OFile.cpp:71
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
std::string fieldFmt
Format for fields writing.
Definition: OFile.h:108
OFile()
Constructor.
Definition: OFile.cpp:50
FileBase & open(const std::string &name, const std::string &mode)
Opens the file (without auto-backup)
Definition: FileBase.cpp:99
void backupFile(const std::string &bstring, const std::string &fname)
Backup a file by giving it a different name.
Definition: OFile.cpp:237
std::vector< Field > fields
All the defined variable fields.
Definition: OFile.h:112
Class for output files.
Definition: OFile.h:84
friend OFile & operator<<(OFile &, const T &)
Formatted output with << operator.
Definition: OFile.h:193
OFile & clearFields()
Resets the list of fields.
Definition: OFile.cpp:128
unsigned actual_buffer_length
This variables stores the actual buffer length.
Definition: OFile.h:96
Base class for dealing with files.
Definition: FileBase.h:39
bool fieldChanged
True if fields has changed.
Definition: OFile.h:106
char * buffer
Internal buffer (generic use)
Definition: OFile.h:92
std::vector< Field > const_fields
All the defined constant fields.
Definition: OFile.h:114
void setBackupString(const std::string &)
Set the string name to be used for automatic backup.
Definition: OFile.cpp:218
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
OFile * linked
Pointer to a linked OFile.
Definition: OFile.h:88
std::ostream & operator<<(std::ostream &log, const ActionRegister &ar)
OFile & setupPrintValue(Value *val)
Used to setup printing of values.
Definition: OFile.cpp:172