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-2020 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  if (IOobject::debug)
38  {
39  InfoInFunction << "Reading header for file " << is.name() << endl;
40  }
41 
42  // Check Istream not already bad
43  if (!is.good())
44  {
45  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
46  {
48  << " stream not open for reading essential object from file "
49  << is.name()
50  << exit(FatalIOError);
51  }
52 
53  if (IOobject::debug)
54  {
56  << " stream not open for reading from file "
57  << is.name() << endl;
58  }
59 
60  return false;
61  }
62 
63  token firstToken(is);
64 
65  if
66  (
67  is.good()
68  && firstToken.isWord()
69  && firstToken.wordToken() == "FoamFile"
70  )
71  {
72  const dictionary headerDict(is);
73 
74  is.version(headerDict.get<token>("version"));
75  is.format(headerDict.get<word>("format"));
76 
77  headerClassName_ = headerDict.get<word>("class");
78 
79  const word headerObject(headerDict.get<word>("object"));
80 
81  if (IOobject::debug && headerObject != name())
82  {
84  << " object renamed from "
85  << name() << " to " << headerObject
86  << " for file " << is.name() << endl;
87  }
88 
89  // The note entry is optional
90  headerDict.readIfPresent("note", note_);
91 
92  labelByteSize_ = sizeof(label);
93  scalarByteSize_ = sizeof(scalar);
94 
95  // The arch information is optional
96  string arch;
97  if (headerDict.readIfPresent("arch", arch))
98  {
99  unsigned val = foamVersion::labelByteSize(arch);
100  if (val) labelByteSize_ = val;
101 
102  val = foamVersion::scalarByteSize(arch);
103  if (val) scalarByteSize_ = val;
104  }
105 
106  is.setLabelByteSize(labelByteSize_);
107  is.setScalarByteSize(scalarByteSize_);
108  }
109  else
110  {
112  << "First token could not be read or is not the keyword 'FoamFile'"
113  << nl << nl << "Check header is of the form:" << nl << endl;
114 
115  writeHeader(Info);
116 
117  return false;
118  }
119 
120  // Check stream is still OK
121  if (is.good())
122  {
123  objState_ = GOOD;
124  }
125  else
126  {
127  if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
128  {
130  << " stream failure while reading header"
131  << " on line " << is.lineNumber()
132  << " of file " << is.name()
133  << " for essential object" << name()
134  << exit(FatalIOError);
135  }
136 
137  if (IOobject::debug)
138  {
140  << "Stream failure while reading header"
141  << " on line " << is.lineNumber()
142  << " of file " << is.name() << endl;
143  }
144 
145  objState_ = BAD;
146 
147  return false;
148  }
149 
150  if (IOobject::debug)
151  {
152  Info<< " .... read" << endl;
153  }
154 
155  return true;
156 }
157 
158 
159 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
SeriousIOErrorInFunction
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:293
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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:81
Foam::token::isWord
bool isWord() const noexcept
Token is WORD or DIRECTIVE word.
Definition: tokenI.H:583
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Read header.
Definition: IOobjectReadHeader.C:35
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 (uses stdout - output is on the master only)
Foam::IOstream::setLabelByteSize
void setLabelByteSize(unsigned nbytes)
Set the label byte-size associated with the stream.
Definition: IOstream.H:275
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:341
IOobject.H
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::IOstream::setScalarByteSize
void setScalarByteSize(unsigned nbytes)
Set the scalar byte-size associated with the stream.
Definition: IOstream.H:281
Foam::IOobject::BAD
Definition: IOobject.H:114
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::GOOD
Definition: IOobject.H:113
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::foamVersion::labelByteSize
unsigned labelByteSize(const std::string &str)
Extract label size (in bytes) from "label=" tag in string.
dictionary.H
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
Foam::IOstream::lineNumber
label lineNumber() const
Const access to the current stream line number.
Definition: IOstream.H:309
Foam::foamVersion::scalarByteSize
unsigned scalarByteSize(const std::string &str)
Extract scalar size (in bytes) from "scalar=" tag in string.
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header.
Definition: IOobjectWriteHeader.C:156
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:315
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
foamVersion.H