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-2016 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  {
46  << "bad object " << name()
47  << endl;
48 
49  return false;
50  }
51 
52  if (instance().empty())
53  {
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  InfoInFunction << "Writing file " << objectPath();
78  }
79 
80 
81  bool osGood = false;
82 
83  {
84  // Try opening an OFstream for object
85  OFstream os(objectPath(), fmt, ver, cmp);
86 
87  // If any of these fail, return (leave error handling to Ostream class)
88  if (!os.good())
89  {
90  return false;
91  }
92 
93  if (!writeHeader(os))
94  {
95  return false;
96  }
97 
98  // Write the data to the Ostream
99  if (!writeData(os))
100  {
101  return false;
102  }
103 
104  writeEndDivider(os);
105 
106  osGood = os.good();
107  }
108 
109  if (OFstream::debug)
110  {
111  Info<< " .... written" << endl;
112  }
113 
114  // Only update the lastModified_ time if this object is re-readable,
115  // i.e. lastModified_ is already set
116  if (watchIndex_ != -1)
117  {
118  time().setUnmodified(watchIndex_);
119  }
120 
121  return osGood;
122 }
123 
124 
126 {
127  return writeObject
128  (
129  time().writeFormat(),
131  time().writeCompression()
132  );
133 }
134 
135 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
#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...
const Time & time() const
Return time.
Definition: IOobject.C:227
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
word timeName
Definition: getTimeIndex.H:3
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:295
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Constant dispersed-phase particle diameter model.
Version number type.
Definition: IOstream.H:96
virtual bool write() const
Write using setting from DB.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
messageStream Info
const fileName & instance() const
Definition: IOobject.H:337
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1020
#define InfoInFunction
Report an information message using Foam::Info.