IOobjectWriteHeader.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Writes the header description of the File to the stream
26  associated with the File.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "IOobject.H"
31 #include "objectRegistry.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
36 (
37  Ostream& os,
38  const IOstream::versionNumber version,
40  const word& type,
41  const string& note,
42  const fileName& location,
43  const word& name
44 )
45 {
46  if (!os.good())
47  {
49  << "No stream open for write" << nl
50  << os.info() << endl;
51 
52  return false;
53  }
54 
55  writeBanner(os) << foamFile << "\n{\n";
56 
57  if (version != IOstream::currentVersion)
58  {
59  os << " version " << version << ";\n";
60  }
61 
62  os << " format " << format << ";\n"
63  << " class " << type << ";\n";
64 
65  if (note.size())
66  {
67  os << " note " << note << ";\n";
68  }
69 
70  if (location.size())
71  {
72  os << " location " << location << ";\n";
73  }
74 
75  os << " object " << name << ";\n"
76  << "}" << nl;
77 
78  writeDivider(os) << nl;
79 
80  return true;
81 }
82 
83 
85 (
86  Ostream& os,
87  const dictionary& foamFileDict
88 )
89 {
90  return writeHeader
91  (
92  os,
94  (
95  "version",
97  ),
98  foamFileDict.found("format")
99  ? IOstream::formatEnum(foamFileDict.lookup<word>("format"))
100  : IOstream::ASCII,
101  foamFileDict.lookupOrDefault<word>("class", dictionary::typeName),
102  foamFileDict.lookupOrDefault<string>("note", string::null),
103  foamFileDict.lookupOrDefault<fileName>("location", fileName::null),
104  foamFileDict.lookupOrDefault<word>("name", os.name().name())
105  );
106 }
107 
108 
110 {
111  return writeHeader
112  (
113  os,
114  os.version(),
115  os.format(),
116  type,
117  note(),
118  instance()/db().dbDir()/local(),
119  name()
120  );
121 }
122 
123 
125 {
126  return writeHeader(os, type());
127 }
128 
129 
130 // ************************************************************************* //
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:45
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:325
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
const word & name() const
Return name.
Definition: IOobject.H:307
static bool writeHeader(Ostream &os, const IOstream::versionNumber version, const IOstream::streamFormat format, const word &type, const string &note, const fileName &location, const word &name)
Write header.
Version number type.
Definition: IOstream.H:97
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:552
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
T lookupOrDefault(const word &, const T &) const
Find and return a T, if not found return the given default.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:669
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
static const fileName null
An empty fileName.
Definition: fileName.H:97
static const string null
An empty string.
Definition: string.H:88
A class for handling words, derived from string.
Definition: word.H:63
#define InfoInFunction
Report an information message using Foam::Info.
void writeHeader(std::ostream &, const bool isBinary, const std::string &title)
Write header.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
word format(conversionProperties.lookup("format"))