TableFileReader.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "TableFileReader.H"
27 #include "fileOperation.H"
28 
29 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
30 
31 template<class Type>
33 (
34  const dictionary& dict,
36 ) const
37 {
38  // Expand the file
39  fileName fNameExpanded(fName_);
40  fNameExpanded.expand();
41 
42  // Open a stream and check it
43  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fNameExpanded));
44  ISstream& is = isPtr();
45  if (!is.good())
46  {
48  << "Cannot open file" << fName_ << nl
49  << exit(FatalIOError);
50  }
51 
52  // Read data from the stream
53  read(is, table);
54 
55  // Check something was read
56  if (table.empty())
57  {
59  << "Table read from " << fName_ << " is empty" << nl
60  << exit(FatalIOError);
61  }
62 }
63 
64 
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
66 
67 template<class Type>
69 (
70  const dictionary& dict
71 )
72 :
73  TableReader<Type>(dict),
74  fName_(dict.lookup("file"))
75 {}
76 
77 
78 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
79 
80 template<class Type>
82 {}
83 
84 
85 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
86 
87 template<class Type>
89 (
90  Ostream& os,
91  const List<Tuple2<scalar, Type>>& table
92 ) const
93 {
94  writeEntry(os, "format", this->type());
95  writeEntry(os, "file", fName_);
96 }
97 
98 
99 // ************************************************************************* //
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Generic input stream.
Definition: ISstream.H:55
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Base class to read table data for tables.
virtual ~TableFileReader()
Destructor.
TableFileReader(const dictionary &dict)
Construct from dictionary.
virtual void write(Ostream &os, const List< Tuple2< scalar, Type >> &table) const
Write additional information.
Base class to read table data for tables.
Definition: TableReader.H:51
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:63
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A class for handling file names.
Definition: fileName.H:82
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:125
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
IOerror FatalIOError
static const char nl
Definition: Ostream.H:260
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict