regIOobjectWrite.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 | Copyright (C) 2011 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  write function for regIOobjects
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "regIOobject.H"
30 #include "Time.H"
31 #include "OSspecific.H"
32 #include "OFstream.H"
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
37 (
41 ) const
42 {
43  if (!good())
44  {
45  SeriousErrorIn("regIOobject::write()")
46  << "bad object " << name()
47  << endl;
48 
49  return false;
50  }
51 
52  if (instance().empty())
53  {
54  SeriousErrorIn("regIOobject::write()")
55  << "instance undefined for object " << name()
56  << endl;
57 
58  return false;
59  }
60 
61  if
62  (
63  instance() != time().timeName()
64  && instance() != time().system()
65  && instance() != time().caseSystem()
66  && instance() != time().constant()
67  && instance() != time().caseConstant()
68  )
69  {
70  const_cast<regIOobject&>(*this).instance() = time().timeName();
71  }
72 
73  mkDir(path());
74 
75  if (OFstream::debug)
76  {
77  Info<< "regIOobject::write() : "
78  << "writing file " << objectPath();
79  }
80 
81 
82  bool osGood = false;
83 
84  {
85  // Try opening an OFstream for object
86  OFstream os(objectPath(), fmt, ver, cmp);
87 
88  // If any of these fail, return (leave error handling to Ostream class)
89  if (!os.good())
90  {
91  return false;
92  }
93 
94  if (!writeHeader(os))
95  {
96  return false;
97  }
98 
99  // Write the data to the Ostream
100  if (!writeData(os))
101  {
102  return false;
103  }
104 
105  writeEndDivider(os);
106 
107  osGood = os.good();
108  }
109 
110  if (OFstream::debug)
111  {
112  Info<< " .... written" << endl;
113  }
114 
115  // Only update the lastModified_ time if this object is re-readable,
116  // i.e. lastModified_ is already set
117  if (watchIndex_ != -1)
118  {
119  time().setUnmodified(watchIndex_);
120  }
121 
122  return osGood;
123 }
124 
125 
127 {
128  return writeObject
129  (
130  time().writeFormat(),
132  time().writeCompression()
133  );
134 }
135 
136 // ************************************************************************* //
#define SeriousErrorIn(functionName)
Report an error message using Foam::SeriousError.
Output to file stream.
Definition: OFstream.H:81
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
const fileName & instance() const
Definition: IOobject.H:337
messageStream Info
virtual bool write() const
Write using setting from DB.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1162
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
Constant dispersed-phase particle diameter model.
const Time & time() const
Return time.
Definition: IOobject.C:251
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:420
Version number type.
Definition: IOstream.H:96
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
word timeName
Definition: getTimeIndex.H:3