Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2019 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.org for more information.
6 :
7 : This file is part of plumed, version 2.
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 23802 : class Field:
44 : public FieldBase {
45 : public:
46 : bool read;
47 11102 : 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
55 : bool inMiddleOfField;
56 : /// Set to true if you want to allow fields to be ignored in the read in file
57 : bool ignoreFields;
58 : /// Set to true to allow files without end-of-line at the end
59 : bool noEOL;
60 : /// Advance to next field (= read one line)
61 : IFile& advanceField();
62 : /// Find field index by name
63 : unsigned findField(const std::string&name)const;
64 : public:
65 : /// Constructor
66 : IFile();
67 : /// Destructor
68 : ~IFile();
69 : /// Opens the file
70 0 : IFile& open(const std::string&name);
71 : /// Gets the list of all fields
72 : IFile& scanFieldList(std::vector<std::string>&);
73 : /// Read a double field
74 : IFile& scanField(const std::string&,double&);
75 : /// Read a int field
76 : IFile& scanField(const std::string&,int&);
77 : /// Read a string field
78 : IFile& scanField(const std::string&,std::string&);
79 : /**
80 : Ends a field-formatted line.
81 :
82 : Typically used as
83 : \verbatim
84 : if.scanField("a",a).scanField("b",b).scanField();
85 : \endverbatim
86 : */
87 : IFile& scanField();
88 : /// Get a full line as a string
89 : IFile& getline(std::string&);
90 : /// Reset end of file
91 : void reset(bool);
92 : /// Check if a field exist
93 : bool FieldExist(const std::string& s);
94 : /// Read in a value
95 : IFile& scanField(Value* val);
96 : /// Allow some of the fields in the input to be ignored
97 : void allowIgnoredFields();
98 : /// Allow files without EOL at the end.
99 : /// This in practice should be only used when opening
100 : /// plumed input files
101 : void allowNoEOL();
102 : };
103 :
104 : }
105 :
106 : #endif
|