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 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 "ListStream.H"
33 
34 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 inline void writeSpaces(Ostream& os, label nSpaces)
40 {
41  if (nSpaces < 1)
42  {
43  nSpaces = 1;
44  }
45  while (nSpaces--)
46  {
47  os.write(char(token::SPACE));
48  }
49 }
50 
51 // Similar to writeEntry, but with fewer spaces
52 template<class T>
53 inline void writeHeaderEntry(Ostream& os, const word& key, const T& value)
54 {
55  os << indent << key;
56  writeSpaces(os, 12 - label(key.size()));
57  os << value << char(token::END_STATEMENT) << nl;
58 }
59 
60 } // End namespace Foam
61 
62 
63 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
64 
65 void Foam::decomposedBlockData::writeHeaderContent
66 (
67  Ostream& os,
68  IOstreamOption streamOptContainer,
69  const word& objectType,
70  const string& note,
71  const fileName& location,
72  const word& objectName
73 )
74 {
75  // Standard header entries
76  writeHeaderEntry(os, "version", streamOptContainer.version());
77  writeHeaderEntry(os, "format", streamOptContainer.format());
79 
80  if (!note.empty())
81  {
82  writeHeaderEntry(os, "note", note);
83  }
84 
85  if (objectType.empty())
86  {
87  // Empty type not allowed - use 'dictionary' fallback
88  writeHeaderEntry(os, "class", word("dictionary"));
89  }
90  else
91  {
92  writeHeaderEntry(os, "class", objectType);
93  }
94 
95  if (!location.empty())
96  {
97  writeHeaderEntry(os, "location", location);
98  }
99  writeHeaderEntry(os, "object", objectName);
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
104 
106 {
107  dictionary headerDict;
108 
109  // Read the regular "FoamFile" header
110  bool ok = io.readHeader(headerDict, is);
111 
113  {
114  // Quick information - extract from "data.class"
115  if (headerDict.readIfPresent("data.class", io.headerClassName()))
116  {
117  return ok;
118  }
119 
120  {
121  // Master-only reading of header
122  List<char> charData;
124 
125  UIListStream headerStream(charData);
126  headerStream.name() = is.name();
127 
128  ok = io.readHeader(headerStream);
129  }
130  }
131 
132  return ok;
133 }
134 
135 
137 (
138  Ostream& os,
139  IOstreamOption streamOptContainer,
140  const word& objectType,
141  const string& note,
142  const fileName& location,
143  const word& objectName,
144  const dictionary& extraEntries
145 )
146 {
148  {
150  }
151 
152  os.beginBlock("FoamFile");
153 
154  decomposedBlockData::writeHeaderContent
155  (
156  os,
157  streamOptContainer,
158  objectType,
159  note,
160  location,
161  objectName
162  );
163 
164  if (!extraEntries.empty())
165  {
166  extraEntries.writeEntries(os);
167  }
168 
169  os.endBlock();
170 
172  {
174  }
175 }
176 
177 
179 (
180  dictionary& dict,
181  IOstreamOption streamOptData,
182  const IOobject& io
183 )
184 {
185  dict.set("data.format", streamOptData.format());
186  dict.set("data.class", io.type());
187 
188  // Deep-copy of meta-data (if any)
189  const dictionary* metaDataDict = io.findMetaData();
190  if (metaDataDict && !metaDataDict->empty())
191  {
192  dict.add("meta", *metaDataDict);
193  }
194 }
195 
196 
198 (
199  Ostream& os,
200  IOstreamOption streamOptData,
201  const IOobject& io
202 )
203 {
205  {
207  }
208 
209  os.beginBlock("FoamFile");
210 
211  decomposedBlockData::writeHeaderContent
212  (
213  os,
214  static_cast<IOstreamOption>(os), // streamOpt container
215  decomposedBlockData::typeName, // class
216  io.note(),
217  (io.instance()/io.db().dbDir()/io.local()), // location
218  io.name()
219  );
220 
221  {
222  writeHeaderEntry(os, "data.format", streamOptData.format());
223  writeHeaderEntry(os, "data.class", io.type());
224  }
225 
226  // Meta-data (if any)
227  const dictionary* metaDataDict = io.findMetaData();
228  if (metaDataDict && !metaDataDict->empty())
229  {
230  metaDataDict->writeEntry("meta", os);
231  }
232 
233  os.endBlock();
234 
236  {
238  }
239 }
240 
241 
242 // ************************************************************************* //
Foam::UIListStream
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:201
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::decomposedBlockData::writeHeader
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.
Definition: decomposedBlockDataHeader.C:137
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOobject::instance
const fileName & instance() const noexcept
Definition: IOobjectI.H:196
Foam::IOobject::bannerEnabled
static bool bannerEnabled() noexcept
Status of output file banner.
Definition: IOobject.H:315
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
objectRegistry.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::writeHeaderEntry
void writeHeaderEntry(Ostream &os, const word &key, const T &value)
Definition: IOobjectWriteHeader.C:53
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
Foam::IOobject::writeBanner
static Ostream & writeBanner(Ostream &os, const bool noSyntaxHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectWriteHeader.C:76
ListStream.H
Input/output streams with managed List storage.
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Definition: IOobjectReadHeader.C:165
Foam::decomposedBlockData::writeExtraHeaderContent
static void writeExtraHeaderContent(dictionary &dict, IOstreamOption streamOptData, const IOobject &io)
Helper: generate additional entries for FoamFile header.
Definition: decomposedBlockDataHeader.C:179
Foam::objectRegistry::dbDir
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: objectRegistry.H:187
Foam::dictionary::writeEntry
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:164
Foam::writeSpaces
void writeSpaces(Ostream &os, label nSpaces)
Definition: IOobjectWriteHeader.C:39
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOobject::local
const fileName & local() const noexcept
Definition: IOobjectI.H:208
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
Foam::token::END_STATEMENT
End entry [isseparator].
Definition: token.H:154
Foam::ISstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:113
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary::writeEntries
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::decomposedBlockData::readBlockEntry
static bool readBlockEntry(Istream &is, List< char > &charData)
Helper: read block of (binary) character data.
Definition: decomposedBlockData.C:114
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::decomposedBlockData::isCollatedType
static bool isCollatedType(const word &objectType)
True if object type is a known collated type.
Definition: decomposedBlockData.C:54
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::IOobject::findMetaData
virtual const dictionary * findMetaData() const noexcept
Return pointer to meta-data (if any) or nullptr.
Definition: IOobjectMetaData.C:33
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::IOobject::note
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::IOobject::writeDivider
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
Definition: IOobjectWriteHeader.C:132
decomposedBlockData.H
Foam::List< char >
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
Foam::foamVersion::buildArch
const std::string buildArch
dictionary.H
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405
Foam::decomposedBlockData::readHeader
static bool readHeader(IOobject &io, Istream &is)
Definition: decomposedBlockDataHeader.C:105
Foam::IOobject::db
const objectRegistry & db() const noexcept
Return the local objectRegistry.
Definition: IOobject.C:487
foamVersion.H