regIOobjectWrite.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-2019 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 bool write
42 ) const
43 {
44  if (!good())
45  {
47  << "bad object " << name()
48  << endl;
49 
50  return false;
51  }
52 
53  if (instance().empty())
54  {
56  << "instance undefined for object " << name()
57  << endl;
58 
59  return false;
60  }
61 
62 
63 
64  //- uncomment this if you want to write global objects on master only
65  // bool isGlobal = global();
66  bool isGlobal = false;
67 
68  if (instance() == time().timeName())
69  {
70  // Mark as written to local directory
71  isGlobal = false;
72  }
73  else if
74  (
75  instance() != time().system()
76  && instance() != time().caseSystem()
77  && instance() != time().constant()
78  && instance() != time().caseConstant()
79  )
80  {
81  // Update instance
82  const_cast<regIOobject&>(*this).instance() = time().timeName();
83 
84  // Mark as written to local directory
85  isGlobal = false;
86  }
87 
88  if (OFstream::debug)
89  {
90  if (isGlobal)
91  {
92  Pout<< "regIOobject::write() : "
93  << "writing (global) file " << objectPath();
94  }
95  else
96  {
97  Pout<< "regIOobject::write() : "
98  << "writing (local) file " << objectPath();
99  }
100  }
101 
102 
103  bool osGood = false;
104 
105 
106  // Everyone check or just master
107  bool masterOnly =
108  isGlobal
109  && (
110  regIOobject::fileModificationChecking == timeStampMaster
111  || regIOobject::fileModificationChecking == inotifyMaster
112  );
113 
114 
115  if (Pstream::master() || !masterOnly)
116  {
117  // if (mkDir(path()))
118  //{
119  // // Try opening an OFstream for object
120  // OFstream os(objectPath(), fmt, ver, cmp);
121  //
122  // // If any of these fail, return (leave error handling to Ostream
123  // // class)
124  // if (!os.good())
125  // {
126  // return false;
127  // }
128  //
129  // if (!writeHeader(os))
130  // {
131  // return false;
132  // }
133  //
134  // // Write the data to the Ostream
135  // if (!writeData(os))
136  // {
137  // return false;
138  // }
139  //
140  // writeEndDivider(os);
141  //
142  // osGood = os.good();
143  //}
144  osGood = fileHandler().writeObject(*this, fmt, ver, cmp, write);
145  }
146  else
147  {
148  // Or scatter the master osGood?
149  osGood = true;
150  }
151 
152  if (OFstream::debug)
153  {
154  Pout<< " .... written" << endl;
155  }
156 
157  // Only update the lastModified_ time if this object is re-readable,
158  // i.e. lastModified_ is already set
159  if (watchIndices_.size())
160  {
161  fileHandler().setUnmodified(watchIndices_.last());
162  }
163 
164  return osGood;
165 }
166 
167 
168 bool Foam::regIOobject::write(const bool write) const
169 {
170  return writeObject
171  (
172  time().writeFormat(),
174  time().writeCompression(),
175  write
176  );
177 }
178 
179 // ************************************************************************* //
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
word timeName
Definition: getTimeIndex.H:3
const fileOperation & fileHandler()
Get current file handler.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
virtual bool writeObject(const regIOobject &, IOstream::streamFormat format=IOstream::ASCII, IOstream::versionNumber version=IOstream::currentVersion, IOstream::compressionType compression=IOstream::UNCOMPRESSED, const bool write=true) const
Writes a regIOobject (so header, contents and divider).
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const fileName & instance() const
Definition: IOobject.H:390
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
const Time & time() const
Return time.
Definition: IOobject.C:318
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
virtual bool write(const bool write=true) const
Write using setting from DB.
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230