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-2022 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 
73 bool Foam::IOobject::readHeader(dictionary& headerDict, Istream& is)
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 (isReadRequired())
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  // Mark as not read
124  headerClassName_.clear();
125 
126  return false;
127  }
128 
129  // Check stream is still OK
130  objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
131 
132  if (IOobject::debug)
133  {
134  Info<< " .... read - state: "
135  << (objState_ == objectState::GOOD ? "good" : "bad")
136  << endl;
137 
138  }
139 
140  if (objState_ == objectState::BAD)
141  {
142  if (isReadRequired())
143  {
145  << " stream failure while reading header"
146  << " on line " << is.lineNumber()
147  << " of file " << is.relativeName()
148  << " for essential object:" << name()
149  << exit(FatalIOError);
150  }
151 
152  if (IOobject::debug)
153  {
155  << "Stream failure while reading header"
156  << " on line " << is.lineNumber()
157  << " of file " << is.relativeName() << endl;
158  }
159 
160  return false;
161  }
162 
163  return true;
164 }
165 
166 
167 bool Foam::IOobject::readHeader(Istream& is)
168 {
169  dictionary headerDict;
170  return IOobject::readHeader(headerDict, is);
171 }
172 
173 
174 // ************************************************************************* //
#define SeriousIOErrorInFunction(ios)
Report an IO error message using Foam::SeriousError.
static void writeHeader(Ostream &os, const word &fieldName)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
A token holds an item read from Istream.
Definition: token.H:64
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
A simple container for options an IOstream can normally have.
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.
IOstreamOption parseHeader(const dictionary &headerDict)
Parse &#39;FoamFile&#39; header contents and set the IOobject characteristics and return the stream character...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
int debug
Static debugging option.
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...
unsigned scalarByteSize(const std::string &str)
Extract scalar size (in bytes) from "scalar=" tag in string.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
versionNumber version() const noexcept
Get the stream version.
unsigned labelByteSize(const std::string &str)
Extract label size (in bytes) from "label=" tag in string.
messageStream Info
Information stream (stdout output on master, null elsewhere)
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
bool readHeader(Istream &is)
Read header (&#39;FoamFile&#39; dictionary) and set the IOobject and stream characteristics.
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.