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 :
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 // ************************************************************************* //
Base class to read table data for tables.
Definition: TableReader.H:49
dictionary dict
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:65
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Base class to read table data for tables.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
virtual void write(Ostream &os, const List< Tuple2< scalar, Type >> &table) const
Write additional information.
virtual ~TableFileReader()
Destructor.
const fileOperation & fileHandler()
Get current file handler.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Generic input stream.
Definition: ISstream.H:52
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:103
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
TableFileReader(const dictionary &dict)
Construct from dictionary.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844
IOerror FatalIOError