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-2015 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  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 // ************************************************************************* //
regIOobject.H
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:37
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::regIOobject::write
virtual bool write() const
Write using setting from DB.
Definition: regIOobjectWrite.C:126
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobject.H:350
Foam::IOstream::currentVersion
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:245
constant
Constant dispersed-phase particle diameter model.
OFstream.H
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::Info
messageStream Info
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:237
writeData
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::system
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1155
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86