Loading...
Searching...
No Matches
IOobjectReadHeader.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2019-2023 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "IOobject.H"
30#include "dictionary.H"
31#include "foamVersion.H"
32#include "fileOperation.H"
33#include "Pstream.H"
34
35// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36
38{
39 IOstreamOption streamOpt; // == (ASCII, currentVersion)
40
41 // Treat "version" as optional
42 {
43 token tok;
44 if (headerDict.readIfPresent("version", tok))
45 {
46 streamOpt.version(tok);
47 }
48 }
49
50 // Treat "format" as mandatory, could also as optional
51 streamOpt.format(headerDict.get<word>("format"));
52
53 headerClassName_ = headerDict.get<word>("class");
54
55 const word headerObject(headerDict.get<word>("object"));
56
57 // The "note" entry is optional
58 headerDict.readIfPresent("note", note_);
59
60 // The "arch" information may be missing
61 string arch;
62 if (headerDict.readIfPresent("arch", arch))
63 {
64 unsigned val = foamVersion::labelByteSize(arch);
65 if (val) sizeofLabel_ = static_cast<unsigned char>(val);
66
68 if (val) sizeofScalar_ = static_cast<unsigned char>(val);
69 }
70
71 return streamOpt;
72}
73
74
75bool Foam::IOobject::readHeader(dictionary& headerDict, Istream& is)
76{
77 if (IOobject::debug)
78 {
79 InfoInFunction << "Reading header for file " << is.name() << endl;
80 }
81
82 // Check Istream not already bad
83 if (!is.good())
84 {
85 if (isReadRequired())
86 {
88 << " stream not open for reading essential object from file "
89 << is.relativeName()
91 }
92
93 if (IOobject::debug)
94 {
96 << " stream not open for reading from file "
97 << is.relativeName() << endl;
98 }
99
100 return false;
101 }
102
103 token firstToken(is);
104
105 if (is.good() && firstToken.isWord("FoamFile"))
106 {
107 headerDict.read(is, false); // Read sub-dictionary content
108
109 IOstreamOption streamOpt = parseHeader(headerDict);
110
111 is.format(streamOpt.format());
112 is.version(streamOpt.version());
113 is.setLabelByteSize(sizeofLabel_);
114 is.setScalarByteSize(sizeofScalar_);
115 }
116 else
117 {
119 << "First token could not be read or is not 'FoamFile'"
120 << nl << nl
121 << "Check header is of the form:" << nl << endl;
122
124
125 // Mark as not read
126 headerClassName_.clear();
127
128 return false;
129 }
130
131 // Check stream is still OK
132 objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
133
134 if (IOobject::debug)
135 {
136 Info<< " .... read - state: "
137 << (objState_ == objectState::GOOD ? "good" : "bad")
138 << endl;
139
140 }
141
142 if (objState_ == objectState::BAD)
143 {
144 if (isReadRequired())
145 {
147 << " stream failure while reading header"
148 << " on line " << is.lineNumber()
149 << " of file " << is.relativeName()
150 << " for essential object:" << name()
151 << exit(FatalIOError);
152 }
153
154 if (IOobject::debug)
155 {
157 << "Stream failure while reading header"
158 << " on line " << is.lineNumber()
159 << " of file " << is.relativeName() << endl;
160 }
161
162 return false;
163 }
164
165 return true;
166}
167
168
169bool Foam::IOobject::readHeader(Istream& is)
170{
171 dictionary headerDict;
172 return IOobject::readHeader(headerDict, is);
173}
174
175
176// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
177
178bool Foam::IOobject::readAndCheckHeader
179(
180 const bool isGlobal,
181 const word& typeName,
182 const bool checkType,
183 const bool search,
184 const bool verbose
185)
186{
187 // Mark as not yet read. cf, IOobject::readHeader()
188 headerClassName_.clear();
189
190 // Everyone check or just master
191 const bool masterOnly
192 (
193 isGlobal
195 );
196
197 const auto& handler = Foam::fileHandler();
198
199 // Determine local status
200 bool ok = false;
201
202 if (masterOnly)
203 {
204 if (UPstream::master())
205 {
206 // Force master-only header reading
207 const bool oldParRun = UPstream::parRun(false);
208 const fileName fName
209 (
210 handler.filePath(isGlobal, *this, typeName, search)
211 );
212 ok = handler.readHeader(*this, fName, typeName);
213 UPstream::parRun(oldParRun);
214
215 if (ok && checkType && !isHeaderClass(typeName))
216 {
217 ok = false;
218 if (verbose)
219 {
221 << "Unexpected class name \"" << headerClassName()
222 << "\" expected \"" << typeName
223 << "\" when reading " << fName << endl;
224 }
225 }
226 }
227
228 // If masterOnly make sure all processors know about the read
229 // information. Note: should ideally be inside fileHandler...
231 (
233 ok,
234 headerClassName_,
235 note_
236 );
237 }
238 else
239 {
240 // All read header
241 const fileName fName
242 (
243 handler.filePath(isGlobal, *this, typeName, search)
244 );
245 ok = handler.readHeader(*this, fName, typeName);
246
247 if (ok && checkType && !isHeaderClass(typeName))
248 {
249 ok = false;
250 if (verbose)
251 {
253 << "Unexpected class name \"" << headerClassName()
254 << "\" expected \"" << typeName
255 << "\" when reading " << fName << endl;
256 }
257 }
258 }
259
260 return ok;
261}
262
263
264// ************************************************************************* //
static bool isReadRequired(readOption opt) noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
bool readHeader(Istream &is)
Read header ('FoamFile' dictionary) and set the IOobject and stream characteristics.
static bool fileModificationChecking_masterOnly() noexcept
Test fileModificationChecking for master-only.
Definition IOobjectI.H:23
IOstreamOption parseHeader(const dictionary &headerDict)
Parse 'FoamFile' header contents and set the IOobject characteristics and return the stream character...
A simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
streamFormat format() const noexcept
Get the current stream format.
label lineNumber() const noexcept
Const access to the current stream line number.
Definition IOstream.H:409
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
virtual const fileName & name() const
The name of the stream.
Definition IOstream.C:33
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition IOstream.H:348
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition IOstream.H:356
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition IOstream.C:39
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
static void broadcasts(const int communicator, Type &value, Args &&... values)
Broadcast multiple items to all communicator ranks. Does nothing in non-parallel.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition UPstream.H:1069
static bool & parRun() noexcept
Test if this a parallel run.
Definition UPstream.H:1681
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
A class for handling file names.
Definition fileName.H:75
A token holds an item read from Istream.
Definition token.H:70
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE).
Definition tokenI.H:1004
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
auto & name
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
unsigned scalarByteSize(const std::string &str)
Extract scalar size (in bytes) from "scalar=" tag in string.
unsigned labelByteSize(const std::string &str)
Extract label size (in bytes) from "label=" tag in string.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
static void writeHeader(Ostream &os, const word &fieldName)
messageStream Info
Information stream (stdout output on master, null elsewhere).
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition fileName.C:642
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50