1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 Copyright (c) 2012-2021 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 class Field: 44 public FieldBase { 45 public: 46 bool read; Field()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 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 IFile& open(const std::string&name) override; 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 long int field 78 IFile& scanField(const std::string&,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 string field 84 IFile& scanField(const std::string&,std::string&); 85 /** 86 Ends a field-formatted line. 87 88 Typically used as 89 \verbatim 90 if.scanField("a",a).scanField("b",b).scanField(); 91 \endverbatim 92 */ 93 IFile& scanField(); 94 /// Get a full line as a string 95 IFile& getline(std::string&); 96 /// Reset end of file 97 void reset(bool); 98 /// Check if a field exist 99 bool FieldExist(const std::string& s); 100 /// Read in a value 101 IFile& scanField(Value* val); 102 /// Allow some of the fields in the input to be ignored 103 void allowIgnoredFields(); 104 /// Allow files without EOL at the end. 105 /// This in practice should be only used when opening 106 /// plumed input files 107 void allowNoEOL(); 108 }; 109 110 } 111 112 #endif 113