decomposedBlockDataHeader.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) 2021-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "decomposedBlockData.H"
29#include "dictionary.H"
30#include "foamVersion.H"
31#include "objectRegistry.H"
32#include "ListStream.H"
33
34// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35
36namespace Foam
37{
38
39// Like Ostream::writeEntry, but with fewer spaces
40template<class T>
41static inline void writeHeaderEntry
42(
43 Ostream& os,
44 const word& key,
45 const T& value
46)
47{
48 os.indent();
49 os.write(key);
50
51 label padding = (12 - label(key.size()));
52
53 // Write padding spaces (always at least one)
54 do
55 {
56 os.write(char(token::SPACE));
57 }
58 while (--padding > 0);
59
60 os << value << char(token::END_STATEMENT) << nl;
61}
62
63} // End namespace Foam
64
65
66// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
67
68void Foam::decomposedBlockData::writeHeaderContent
69(
70 Ostream& os,
71 IOstreamOption streamOptContainer,
72 const word& objectType,
73 const string& note,
74 const fileName& location,
75 const word& objectName
76)
77{
78 // Standard header entries
79 writeHeaderEntry(os, "version", streamOptContainer.version());
80 writeHeaderEntry(os, "format", streamOptContainer.format());
82
83 if (!note.empty())
84 {
85 writeHeaderEntry(os, "note", note);
86 }
87
88 if (objectType.empty())
89 {
90 // Empty type not allowed - use 'dictionary' fallback
91 writeHeaderEntry(os, "class", word("dictionary"));
92 }
93 else
94 {
95 writeHeaderEntry(os, "class", objectType);
96 }
97
98 if (!location.empty())
99 {
100 writeHeaderEntry(os, "location", location);
101 }
102 writeHeaderEntry(os, "object", objectName);
103}
104
105
106// * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
107
109{
110 dictionary headerDict;
111
112 // Read the regular "FoamFile" header
113 bool ok = io.readHeader(headerDict, is);
114
116 {
117 // Quick information - extract from "data.class"
118 if (headerDict.readIfPresent("data.class", io.headerClassName()))
119 {
120 return ok;
121 }
122
123 {
124 // Master-only reading of header
125 List<char> charData;
127
128 UIListStream headerStream(charData);
129 headerStream.name() = is.name();
130
131 ok = io.readHeader(headerStream);
132 }
133 }
134
135 return ok;
136}
137
138
140(
141 Ostream& os,
142 IOstreamOption streamOptContainer,
143 const word& objectType,
144 const string& note,
145 const fileName& location,
146 const word& objectName,
147 const dictionary& extraEntries
148)
149{
151 {
153 }
154
155 os.beginBlock("FoamFile");
156
157 decomposedBlockData::writeHeaderContent
158 (
159 os,
160 streamOptContainer,
161 objectType,
162 note,
163 location,
164 objectName
165 );
166
167 if (!extraEntries.empty())
168 {
169 extraEntries.writeEntries(os);
170 }
171
172 os.endBlock();
173
175 {
177 }
178}
179
180
182(
184 IOstreamOption streamOptData,
185 const IOobject& io
186)
187{
188 dict.set("data.format", streamOptData.format());
189 dict.set("data.class", io.type());
190
191 // Deep-copy of meta-data (if any)
192 const dictionary* metaDataDict = io.findMetaData();
193 if (metaDataDict && !metaDataDict->empty())
194 {
195 dict.add("meta", *metaDataDict);
196 }
197}
198
199
201(
202 Ostream& os,
203 IOstreamOption streamOptData,
204 const IOobject& io
205)
206{
208 {
210 }
211
212 os.beginBlock("FoamFile");
213
214 decomposedBlockData::writeHeaderContent
215 (
216 os,
217 static_cast<IOstreamOption>(os), // streamOpt container
219 io.note(),
220 (io.instance()/io.db().dbDir()/io.local()), // location
221 io.name()
222 );
223
224 {
225 writeHeaderEntry(os, "data.format", streamOptData.format());
226 writeHeaderEntry(os, "data.class", io.type());
227 }
228
229 // Meta-data (if any)
230 const dictionary* metaDataDict = io.findMetaData();
231 if (metaDataDict && !metaDataDict->empty())
232 {
233 metaDataDict->writeEntry("meta", os);
234 }
235
236 os.endBlock();
237
239 {
241 }
242}
243
244
245// ************************************************************************* //
Input/output streams with managed List storage.
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
bool readHeader(Istream &is)
static bool bannerEnabled() noexcept
Status of output file banner.
Definition: IOobject.H:315
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
virtual const dictionary * findMetaData() const noexcept
Return pointer to meta-data (if any) or nullptr.
static Ostream & writeBanner(Ostream &os, const bool noSyntaxHint=false)
Write the standard OpenFOAM file/dictionary banner.
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:500
const fileName & local() const noexcept
Read access to local path component.
Definition: IOobjectI.H:208
const fileName & instance() const noexcept
Read access to instance path component.
Definition: IOobjectI.H:196
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
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.
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:113
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
virtual void indent()
Add indentation characters.
Definition: OSstream.C:266
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:105
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:87
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:205
static bool readBlockEntry(Istream &is, List< char > &charData)
Helper: read block of (binary) character data.
static void writeHeader(Ostream &os, IOstreamOption streamOptContainer, const word &objectType, const string &note, const fileName &location, const word &objectName, const dictionary &extraEntries)
Helper: write FoamFile IOobject header.
static void writeExtraHeaderContent(dictionary &dict, IOstreamOption streamOptData, const IOobject &io)
Helper: generate additional entries for FoamFile header.
static bool isCollatedType(const word &objectType)
True if object type is a known collated type.
static bool readHeader(IOobject &io, Istream &is)
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:164
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
A class for handling file names.
Definition: fileName.H:76
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
@ END_STATEMENT
End entry [isseparator].
Definition: token.H:154
@ SPACE
Space [isspace].
Definition: token.H:125
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
const std::string buildArch
Namespace for OpenFOAM.
static void writeHeaderEntry(Ostream &os, const word &key, const T &value)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
static const char *const typeName
The type name used in ensight case files.