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 -------------------------------------------------------------------------------
10 License
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 
36 namespace Foam
37 {
38 
39 // Like Ostream::writeEntry, but with fewer spaces
40 template<class T>
41 static 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 
68 void 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 
108 bool 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);
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 (
183  dictionary& dict,
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
218  decomposedBlockData::typeName, // class
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 // ************************************************************************* //
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:157
dictionary dict
Input/output streams with (internal or external) character storage.
virtual Ostream & write(const char c) override
Write character.
Definition: OBJstream.C:69
virtual const dictionary * findMetaData() const noexcept
Return pointer to meta-data (if any) or nullptr.
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:225
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:625
End entry [isseparator].
Definition: token.H:160
static void writeExtraHeaderContent(dictionary &dict, IOstreamOption streamOptData, const IOobject &io)
Helper: generate additional entries for FoamFile header.
A class for handling words, derived from Foam::string.
Definition: word.H:63
static bool readBlockEntry(Istream &is, List< char > &charData)
Helper: read block of (binary) character data.
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
Space [isspace].
Definition: token.H:131
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:450
static void writeHeaderEntry(Ostream &os, const word &key, const T &value)
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.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static bool bannerEnabled() noexcept
Status of output file banner.
Definition: IOobject.H:361
const fileName & instance() const noexcept
Read access to instance path component.
Definition: IOobjectI.H:266
const std::string buildArch
OpenFOAM build architecture information (machine endian, label/scalar sizes) as a std::string...
virtual void indent() override
Add indentation characters.
Definition: OSstream.C:285
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:213
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
static Ostream & writeBanner(Ostream &os, const bool noSyntaxHint=false)
Write the standard OpenFOAM file/dictionary banner.
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:765
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
const fileName & local() const noexcept
Read access to local path component.
Definition: IOobjectI.H:278
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.
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.
Namespace for OpenFOAM.