Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2023 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 : #include <cstddef> 28 : 29 : namespace PLMD { 30 : 31 : class Value; 32 : 33 : /** 34 : \ingroup TOOLBOX 35 : Class for input files 36 : 37 : This class provides features similar to those in the standard C "FILE*" type, 38 : but only for sequential input. See OFile for sequential output. 39 : 40 : */ 41 : 42 : //TODO: the `fields` component may be made a map or a ordered/unordered set, since Field objects can be ordered (hence findable in Log(N)/log(1)! 43 : class IFile: 44 : /// Class identifying a single field for fielded output 45 : public virtual FileBase { 46 49562 : struct Field: 47 : public FieldBase { 48 : bool read=false; 49 : }; 50 : /// All the defined fields 51 : std::vector<Field> fields; 52 : /// Flag set in the middle of a field reading 53 : bool inMiddleOfField; 54 : /// Set to true if you want to allow fields to be ignored in the read in file 55 : bool ignoreFields; 56 : /// Set to true to allow files without end-of-line at the end 57 : bool noEOL; 58 : /// Advance to next field (= read one line) 59 : IFile& advanceField(); 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&mypath) override; 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 long int field 76 : IFile& scanField(const std::string&,long int&); 77 : /// Read a long long int field 78 : IFile& scanField(const std::string&,long long int&); 79 : /// Read a unsigned field 80 : IFile& scanField(const std::string&,unsigned&); 81 : /// Read a long unsigned field 82 : IFile& scanField(const std::string&,long unsigned&); 83 : /// Read a long long unsigned field 84 : IFile& scanField(const std::string&,long long unsigned&); 85 : /// Read a string field 86 : IFile& scanField(const std::string&,std::string&); 87 : /** 88 : Ends a field-formatted line. 89 : 90 : Typically used as 91 : \verbatim 92 : if.scanField("a",a).scanField("b",b).scanField(); 93 : \endverbatim 94 : */ 95 : IFile& scanField(); 96 : /// Get a full line as a string 97 : IFile& getline(std::string&); 98 : /// Reset end of file 99 : void reset(bool); 100 : /// Check if a field exist 101 : bool FieldExist(const std::string& s); 102 : /// Read in a value 103 : IFile& scanField(Value* val); 104 : /// Allow some of the fields in the input to be ignored 105 : void allowIgnoredFields(); 106 : /// Allow files without EOL at the end. 107 : /// This in practice should be only used when opening 108 : /// plumed input files 109 : void allowNoEOL(); 110 : }; 111 : 112 : } 113 : 114 : #endif