All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
IFile.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_IFile_h
23 #define __PLUMED_tools_IFile_h
24 
25 #include "FileBase.h"
26 #include <vector>
27 
28 namespace PLMD{
29 
30 class Value;
31 
32 /**
33 \ingroup TOOLBOX
34 Class for input files
35 
36 This class provides features similar to those in the standard C "FILE*" type,
37 but only for sequential input. See OFile for sequential output.
38 
39 */
40 class IFile:
41 /// Class identifying a single field for fielded output
42 public virtual FileBase{
43  class Field:
44  public FieldBase{
45  public:
46  bool read;
47  Field(): read(false) {}
48  };
49 /// Low-level read.
50 /// Note: in parallel, all processes read
51  size_t llread(char*,size_t);
52 /// All the defined fields
53  std::vector<Field> fields;
54 /// Flag set in the middle of a field reading
56 /// Set to true if you want to allow fields to be ignored in the read in file
58 /// Advance to next field (= read one line)
60 /// Find field index by name
61  unsigned findField(const std::string&name)const;
62 public:
63 /// Constructor
64  IFile();
65 /// Destructor
66  ~IFile();
67 /// Opens the file
68  IFile& open(const std::string&name);
69 /// Gets the list of all fields
70  IFile& scanFieldList(std::vector<std::string>&);
71 /// Read a double field
72  IFile& scanField(const std::string&,double&);
73 /// Read a int field
74  IFile& scanField(const std::string&,int&);
75 /// Read a string field
76  IFile& scanField(const std::string&,std::string&);
77 /**
78  Ends a field-formatted line.
79 
80 Typically used as
81 \verbatim
82  if.scanField("a",a).scanField("b",b).scanField();
83 \endverbatim
84 */
85  IFile& scanField();
86 /// Get a full line as a string
87  IFile& getline(std::string&);
88 /// Reset end of file
89  void reset(bool);
90 /// Check if a field exist
91  bool FieldExist(const std::string& s);
92 /// Read in a value
93  IFile& scanField(Value* val);
94 /// Allow some of the fields in the input to be ignored
95  void allowIgnoredFields();
96 };
97 
98 }
99 
100 #endif
bool FieldExist(const std::string &s)
Check if a field exist.
Definition: IFile.cpp:104
IFile()
Constructor.
Definition: IFile.cpp:159
IFile & getline(std::string &)
Get a full line as a string.
Definition: IFile.cpp:169
unsigned findField(const std::string &name) const
Find field index by name.
Definition: IFile.cpp:185
std::vector< Field > fields
All the defined fields.
Definition: IFile.h:53
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
bool inMiddleOfField
Flag set in the middle of a field reading.
Definition: IFile.h:55
void reset(bool)
Reset end of file.
Definition: IFile.cpp:192
IFile & open(const std::string &name)
Opens the file.
Definition: IFile.cpp:90
IFile & advanceField()
Advance to next field (= read one line)
Definition: IFile.cpp:46
IFile & scanField()
Ends a field-formatted line.
Definition: IFile.cpp:149
bool ignoreFields
Set to true if you want to allow fields to be ignored in the read in file.
Definition: IFile.h:57
Class for input files.
Definition: IFile.h:40
~IFile()
Destructor.
Definition: IFile.cpp:165
size_t llread(char *, size_t)
Low-level read.
Definition: IFile.cpp:37
Base class for dealing with files.
Definition: FileBase.h:39
IFile & scanFieldList(std::vector< std::string > &)
Gets the list of all fields.
Definition: IFile.cpp:95
void allowIgnoredFields()
Allow some of the fields in the input to be ignored.
Definition: IFile.cpp:199