IOobjectWriteHeader.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-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "objectRegistry.H"
32#include "foamVersion.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// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
67
68// A banner corresponding to this:
69//
70/*--------------------------------*- C++ -*----------------------------------*\
71| ========= | |
72| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
73| \\ / O peration | Version: VERSION |
74| \\ / A nd | Website: www.openfoam.com |
75| \\/ M anipulation | |
76\*---------------------------------------------------------------------------*/
77
79Foam::IOobject::writeBanner(Ostream& os, const bool noSyntaxHint)
80{
81 // The version padded with spaces to fit after "Version: "
82 // - initialized with zero-length string to detect if it has been populated
83 static char paddedVersion[39] = "";
84
85 if (!*paddedVersion)
86 {
87 // Populate: like strncpy but without trailing '\0'
88
89 const std::string apiValue(std::to_string(Foam::foamVersion::api));
90
91 std::size_t len = apiValue.length();
92 if (len > 38)
93 {
94 len = 38;
95 }
96
97 std::memset(paddedVersion, ' ', 38);
98 std::memcpy(paddedVersion, apiValue.c_str(), len);
99 paddedVersion[38] = '\0';
100 }
101
102 os <<
103 "/*--------------------------------";
104
105 if (noSyntaxHint)
106 {
107 // Without syntax hint
108 os << "---------";
109 }
110 else
111 {
112 // With syntax hint
113 os << "*- C++ -*";
114 }
115
116 os <<
117 "----------------------------------*\\\n"
118 "| ========= |"
119 " |\n"
120 "| \\\\ / F ield |"
121 " OpenFOAM: The Open Source CFD Toolbox |\n"
122 "| \\\\ / O peration |"
123 " Version: " << paddedVersion << "|\n"
124 "| \\\\ / A nd |"
125 " Website: www.openfoam.com |\n"
126 "| \\\\/ M anipulation |"
127 " |\n"
128 "\\*-----------------------------------------"
129 "----------------------------------*/\n";
130
131 return os;
132}
133
134
136{
137 os <<
138 "// * * * * * * * * * * * * * * * * * "
139 "* * * * * * * * * * * * * * * * * * * * //\n";
140
141 return os;
142}
143
144
146{
147 os << "\n\n"
148 "// *****************************************"
149 "******************************** //\n";
150
151 return os;
152}
153
154
155// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
156
158(
159 Ostream& os,
160 const IOobject& io,
161 const word& objectType,
162 const dictionary* metaDataDict
163)
164{
165 // Standard header entries
166 writeHeaderEntry(os, "version", os.version());
167 writeHeaderEntry(os, "format", os.format());
169
170 if (!io.note().empty())
171 {
172 writeHeaderEntry(os, "note", io.note());
173 }
174
175 if (objectType.empty())
176 {
177 // Empty type not allowed - use 'dictionary' fallback
178 writeHeaderEntry(os, "class", word("dictionary"));
179 }
180 else
181 {
182 writeHeaderEntry(os, "class", objectType);
183 }
184
185 writeHeaderEntry(os, "location", io.instance()/io.db().dbDir()/io.local());
186 writeHeaderEntry(os, "object", io.name());
187
188 // Meta-data (if any)
189 if (metaDataDict && !metaDataDict->empty())
190 {
191 metaDataDict->writeEntry("meta", os);
192 }
193}
194
195
197(
199 const IOobject& io,
200 const word& objectType,
201 IOstreamOption streamOpt,
202 const dictionary* metaDataDict
203)
204{
205 // Standard header entries
206 dict.set("version", streamOpt.version());
207 dict.set("format", streamOpt.format());
209
210 if (!io.note().empty())
211 {
212 dict.set("note", io.note());
213 }
214
215 if (objectType.empty())
216 {
217 // Empty type not allowed - use 'dictionary' fallback
218 dict.set("class", word("dictionary"));
219 }
220 else
221 {
222 dict.set("class", objectType);
223 }
224
225 dict.set("location", io.instance()/io.db().dbDir()/io.local());
226 dict.set("object", io.name());
227
228 // Deep-copy of meta-data (if any)
229 if (metaDataDict && !metaDataDict->empty())
230 {
231 dict.add("meta", *metaDataDict);
232 }
233}
234
235
236// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
237
239(
240 Ostream& os,
241 const word& objectType
242) const
243{
244 if (!os.good())
245 {
247 << "No stream open for write" << nl
248 << os.info() << endl;
249
250 return false;
251 }
252
254 {
256 }
257
258 os.beginBlock("FoamFile");
259
260 // Standard header entries
262 (
263 os,
264 *this,
265 objectType,
266 this->findMetaData()
267 );
268
269 os.endBlock();
270
272 {
274 }
275
276 return true;
277}
278
279
281{
282 return IOobject::writeHeader(os, this->type());
283}
284
285
287(
289 const word& objectType,
290 IOstreamOption streamOpt
291) const
292{
294 (
295 dict,
296 *this,
297 objectType,
298 streamOpt,
299 this->findMetaData()
300 );
301}
302
303
305(
307 IOstreamOption streamOpt
308) const
309{
310 IOobject::writeHeader(dict, this->type(), streamOpt);
311}
312
313
314// ************************************************************************* //
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
static bool bannerEnabled() noexcept
Status of output file banner.
Definition: IOobject.H:315
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
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
static void writeHeaderContent(Ostream &os, const IOobject &io, const word &objectType, const dictionary *metaDataDict=nullptr)
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
bool writeHeader(Ostream &os) const
Write header with current type()
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.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:413
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
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
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
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)
#define InfoInFunction
Report an information message using Foam::Info.
const std::string buildArch
Namespace for OpenFOAM.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
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