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