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 -------------------------------------------------------------------------------
11 License
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 
65  val = foamVersion::scalarByteSize(arch);
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 (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
84  {
86  << " stream not open for reading essential object from file "
87  << is.relativeName()
88  << exit(FatalIOError);
89  }
90 
91  if (IOobject::debug)
92  {
94  << " stream not open for reading from file "
95  << is.relativeName() << endl;
96  }
97 
98  return false;
99  }
100 
101  token firstToken(is);
102 
103  if (is.good() && firstToken.isWord("FoamFile"))
104  {
105  headerDict.read(is, false); // Read sub-dictionary content
106 
107  IOstreamOption streamOpt = parseHeader(headerDict);
108 
109  is.format(streamOpt.format());
110  is.version(streamOpt.version());
111  is.setLabelByteSize(sizeofLabel_);
112  is.setScalarByteSize(sizeofScalar_);
113  }
114  else
115  {
117  << "First token could not be read or is not 'FoamFile'"
118  << nl << nl
119  << "Check header is of the form:" << nl << endl;
120 
121  writeHeader(Info);
122 
123  return false;
124  }
125 
126  // Check stream is still OK
127  if (is.good())
128  {
129  objState_ = GOOD;
130  }
131  else
132  {
133  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
134  {
136  << " stream failure while reading header"
137  << " on line " << is.lineNumber()
138  << " of file " << is.relativeName()
139  << " for essential object:" << name()
140  << exit(FatalIOError);
141  }
142 
143  if (IOobject::debug)
144  {
146  << "Stream failure while reading header"
147  << " on line " << is.lineNumber()
148  << " of file " << is.relativeName() << endl;
149  }
150 
151  objState_ = BAD;
152 
153  return false;
154  }
155 
156  if (IOobject::debug)
157  {
158  Info<< " .... read" << endl;
159  }
160 
161  return true;
162 }
163 
164 
166 {
167  dictionary headerDict;
168  return IOobject::readHeader(headerDict, is);
169 }
170 
171 
172 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
SeriousIOErrorInFunction
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:318
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::IOstream::setLabelByteSize
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:284
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::IOstream::setScalarByteSize
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:290
Foam::IOstream::lineNumber
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:318
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Definition: IOobjectReadHeader.C:165
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
IOobject.H
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::IOstream::relativeName
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
Foam::dictionary::read
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::foamVersion::labelByteSize
unsigned labelByteSize(const std::string &str)
Extract label size (in bytes) from "label=" tag in string.
dictionary.H
Foam::IOobject::parseHeader
IOstreamOption parseHeader(const dictionary &headerDict)
Definition: IOobjectReadHeader.C:35
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::foamVersion::scalarByteSize
unsigned scalarByteSize(const std::string &str)
Extract scalar size (in bytes) from "scalar=" tag in string.
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:340
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405
foamVersion.H