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-2024 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>
34 (
35  const Function1s::unitConversions& defaultUnits,
36  const dictionary& dict
37 ) const
38 {
39  if (dict.found("units"))
40  {
41  autoPtr<Function1s::unitConversions> unitsPtr
42  (
43  new Function1s::unitConversions(defaultUnits)
44  );
45  unitsPtr->readIfPresent("units", dict);
46  return unitsPtr;
47  }
48  else
49  {
50  return autoPtr<Function1s::unitConversions>(nullptr);
51  }
52 }
53 
54 
55 template<class Type>
57 (
58  const Function1s::unitConversions& defaultUnits,
59  const dictionary& dict,
61 ) const
62 {
63  // Expand the file
64  fileName fNameExpanded(fName_);
65  fNameExpanded.expand();
66 
67  // Open a stream and check it
68  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fNameExpanded));
69  ISstream& is = isPtr();
70  if (!is.good())
71  {
73  << "Cannot open file " << fName_ << nl
74  << exit(FatalIOError);
75  }
76 
77  // Read data from the stream
78  read(is, table);
79 
80  // Check something was read
81  if (table.empty())
82  {
84  << "Table read from " << fName_ << " is empty" << nl
85  << exit(FatalIOError);
86  }
87 
88  // Convert units
90  (
91  unitsPtr_.valid() ? unitsPtr_() : defaultUnits,
92  table
93  );
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 template<class Type>
101 (
102  const Function1s::unitConversions& defaultUnits,
103  const dictionary& dict
104 )
105 :
106  TableReader<Type>(),
107  fName_(dict.lookup("file")),
108  unitsPtr_(readUnits(defaultUnits, dict))
109 {}
110 
111 
112 template<class Type>
114 (
115  const TableFileReader<Type>& tfr
116 )
117 :
118  TableReader<Type>(tfr),
119  fName_(tfr.fName_),
120  unitsPtr_(tfr.unitsPtr_, false)
121 {}
122 
123 
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
125 
126 template<class Type>
128 {}
129 
130 
131 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
132 
133 template<class Type>
136 (
138  const dictionary& dict
139 ) const
140 {
142  read(units, dict, data);
143  return data;
144 }
145 
146 
147 template<class Type>
149 (
150  Ostream& os,
152  const List<Tuple2<scalar, Type>>& table
153 ) const
154 {
155  TableReader<Type>::write(os, units, table);
156 
157  writeEntry(os, "format", this->type());
158  writeEntry(os, "file", fName_);
159 
160  if (unitsPtr_.valid())
161  {
162  writeEntry(os, "units", unitsPtr_());
163  }
164 }
165 
166 
167 // ************************************************************************* //
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.
virtual void write(Ostream &os, const Function1s::unitConversions &units, const List< Tuple2< scalar, Type >> &table) const
Write additional information.
TableFileReader(const Function1s::unitConversions &defaultUnits, const dictionary &dict)
Construct from dictionary.
Macros for creating standard TableReader-s.
Definition: TableReader.H:51
virtual void write(Ostream &os, const Function1s::unitConversions &units, const List< Tuple2< scalar, Type >> &table) const
Write settings and values.
Definition: TableReader.C:97
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
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:346
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
bool read(const char *, int32_t &)
Definition: int32IO.C:85
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
HashTable< unitConversion > * unitsPtr_(nullptr)
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266
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