Loading...
Searching...
No Matches
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-2023 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 "SpanStream.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);
102 writeHeaderEntry(os, "object", objectName);
103}
104
105
106// * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
107
108bool Foam::decomposedBlockData::readHeader(IOobject& io, Istream& is)
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 ISpanStream headerStream(charData);
129 headerStream.name() = is.name();
130
131 ok = io.readHeader(headerStream);
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 if (const auto* meta = io.findMetaData(); meta && !meta->empty())
193 {
194 dict.add("meta", *meta);
195 }
196}
197
198
200(
201 Ostream& os,
202 IOstreamOption streamOptData,
203 const IOobject& io
204)
205{
207 {
209 }
210
211 os.beginBlock("FoamFile");
212
213 decomposedBlockData::writeHeaderContent
214 (
215 os,
216 static_cast<IOstreamOption>(os), // streamOpt container
217 decomposedBlockData::typeName, // class
218 io.note(),
219 (io.instance()/io.db().dbDir()/io.local()), // location
220 io.name()
221 );
222
223 // Same as writeExtraHeaderContent
224 {
225 writeHeaderEntry(os, "data.format", streamOptData.format());
226 writeHeaderEntry(os, "data.class", io.type());
227 }
228
229 // Meta-data (if any)
230 if (const auto* meta = io.findMetaData(); meta && !meta->empty())
231 {
232 meta->writeEntry("meta", os);
233 }
234
235 os.endBlock();
236
238 {
240 }
241}
242
243
244// ************************************************************************* //
Input/output streams with (internal or external) character storage.
bool empty() const noexcept
True if the list is empty.
Definition DLListBase.H:189
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
static bool bannerEnabled() noexcept
Status of output file banner.
Definition IOobject.H:376
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
static Ostream & writeBanner(Ostream &os, const bool noSyntaxHint=false)
Write the standard OpenFOAM file/dictionary banner.
const string & note() const noexcept
Return the optional note.
Definition IOobjectI.H:235
A simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
virtual const fileName & name() const
The name of the stream.
Definition IOstream.C:33
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name).
Definition ISstream.H:147
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & endBlock()
Write end block group.
Definition Ostream.C:108
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition Ostream.C:90
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)
Read header as per IOobject with additional handling of decomposedBlockData.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
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...
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
A class for handling file names.
Definition fileName.H:75
@ END_STATEMENT
End entry [isseparator].
Definition token.H:173
@ SPACE
Space [isspace].
Definition token.H:144
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
const auto & io
const std::string buildArch
OpenFOAM build architecture information (machine endian, label/scalar sizes) as a std::string.
Namespace for OpenFOAM.
static void writeHeaderEntry(Ostream &os, const word &key, const T &value)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict