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