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 : class IFile: 42 : /// Class identifying a single field for fielded output 43 : public virtual FileBase { 44 26057 : class Field: 45 : public FieldBase { 46 : public: 47 : bool read; 48 7253 : Field(): read(false) {} 49 : }; 50 : /// Low-level read. 51 : /// Note: in parallel, all processes read 52 : std::size_t llread(char*,std::size_t); 53 : /// All the defined fields 54 : std::vector<Field> fields; 55 : /// Flag set in the middle of a field reading 56 : bool inMiddleOfField; 57 : /// Set to true if you want to allow fields to be ignored in the read in file 58 : bool ignoreFields; 59 : /// Set to true to allow files without end-of-line at the end 60 : bool noEOL; 61 : /// Advance to next field (= read one line) 62 : IFile& advanceField(); 63 : /// Find field index by name 64 : unsigned findField(const std::string&name)const; 65 : public: 66 : /// Constructor 67 : IFile(); 68 : /// Destructor 69 : ~IFile(); 70 : /// Opens the file 71 : IFile& open(const std::string&name) override; 72 : /// Gets the list of all fields 73 : IFile& scanFieldList(std::vector<std::string>&); 74 : /// Read a double field 75 : IFile& scanField(const std::string&,double&); 76 : /// Read a int field 77 : IFile& scanField(const std::string&,int&); 78 : /// Read a long int field 79 : IFile& scanField(const std::string&,long int&); 80 : /// Read a unsigned field 81 : IFile& scanField(const std::string&,unsigned&); 82 : /// Read a long unsigned field 83 : IFile& scanField(const std::string&,long unsigned&); 84 : /// Read a string field 85 : IFile& scanField(const std::string&,std::string&); 86 : /** 87 : Ends a field-formatted line. 88 : 89 : Typically used as 90 : \verbatim 91 : if.scanField("a",a).scanField("b",b).scanField(); 92 : \endverbatim 93 : */ 94 : IFile& scanField(); 95 : /// Get a full line as a string 96 : IFile& getline(std::string&); 97 : /// Reset end of file 98 : void reset(bool); 99 : /// Check if a field exist 100 : bool FieldExist(const std::string& s); 101 : /// Read in a value 102 : IFile& scanField(Value* val); 103 : /// Allow some of the fields in the input to be ignored 104 : void allowIgnoredFields(); 105 : /// Allow files without EOL at the end. 106 : /// This in practice should be only used when opening 107 : /// plumed input files 108 : void allowNoEOL(); 109 : }; 110 : 111 : } 112 : 113 : #endif