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-2021 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
33// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34
36{
37 IOstreamOption streamOpt; // == (ASCII, currentVersion)
38
39 // Treat "version" as optional
40 {
41 token tok;
42 if (headerDict.readIfPresent("version", tok))
43 {
44 streamOpt.version(tok);
45 }
46 }
47
48 // Treat "format" as mandatory, could also as optional
49 streamOpt.format(headerDict.get<word>("format"));
50
51 headerClassName_ = headerDict.get<word>("class");
52
53 const word headerObject(headerDict.get<word>("object"));
54
55 // The "note" entry is optional
56 headerDict.readIfPresent("note", note_);
57
58 // The "arch" information may be missing
59 string arch;
60 if (headerDict.readIfPresent("arch", arch))
61 {
62 unsigned val = foamVersion::labelByteSize(arch);
63 if (val) sizeofLabel_ = static_cast<unsigned char>(val);
64
66 if (val) sizeofScalar_ = static_cast<unsigned char>(val);
67 }
68
69 return streamOpt;
70}
71
72
74{
75 if (IOobject::debug)
76 {
77 InfoInFunction << "Reading header for file " << is.name() << endl;
78 }
79
80 // Check Istream not already bad
81 if (!is.good())
82 {
83 if
84 (
85 rOpt_ == IOobject::MUST_READ
87 )
88 {
90 << " stream not open for reading essential object from file "
91 << is.relativeName()
93 }
94
95 if (IOobject::debug)
96 {
98 << " stream not open for reading from file "
99 << is.relativeName() << endl;
100 }
101
102 return false;
103 }
104
105 token firstToken(is);
106
107 if (is.good() && firstToken.isWord("FoamFile"))
108 {
109 headerDict.read(is, false); // Read sub-dictionary content
110
111 IOstreamOption streamOpt = parseHeader(headerDict);
112
113 is.format(streamOpt.format());
114 is.version(streamOpt.version());
115 is.setLabelByteSize(sizeofLabel_);
116 is.setScalarByteSize(sizeofScalar_);
117 }
118 else
119 {
121 << "First token could not be read or is not 'FoamFile'"
122 << nl << nl
123 << "Check header is of the form:" << nl << endl;
124
126
127 return false;
128 }
129
130 // Check stream is still OK
131 objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
132
133 if (IOobject::debug)
134 {
135 Info<< " .... read - state: "
136 << (objState_ == objectState::GOOD ? "good" : "bad")
137 << endl;
138
139 }
140
141 if (objState_ == objectState::BAD)
142 {
143 if
144 (
145 rOpt_ == IOobject::MUST_READ
147 )
148 {
150 << " stream failure while reading header"
151 << " on line " << is.lineNumber()
152 << " of file " << is.relativeName()
153 << " for essential object:" << name()
154 << exit(FatalIOError);
155 }
156
157 if (IOobject::debug)
158 {
160 << "Stream failure while reading header"
161 << " on line " << is.lineNumber()
162 << " of file " << is.relativeName() << endl;
163 }
164
165 return false;
166 }
167
168 return true;
169}
170
171
173{
174 dictionary headerDict;
175 return IOobject::readHeader(headerDict, is);
176}
177
178
179// ************************************************************************* //
bool readHeader(Istream &is)
IOstreamOption parseHeader(const dictionary &headerDict)
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:180
The IOstreamOption is 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:318
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:284
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:290
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
A token holds an item read from Istream.
Definition: token.H:69
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
#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.
static void writeHeader(Ostream &os, const word &fieldName)
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53