initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / regIOobject / regIOobjectWrite.C
blob62df5dafe1512ee7fd2d9895fc86f20c052b3530
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     write function for regIOobjects
28 \*---------------------------------------------------------------------------*/
30 #include "regIOobject.H"
31 #include "Time.H"
32 #include "OSspecific.H"
33 #include "OFstream.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 bool Foam::regIOobject::writeObject
39     IOstream::streamFormat fmt,
40     IOstream::versionNumber ver,
41     IOstream::compressionType cmp
42 ) const
44     if (!good())
45     {
46         SeriousErrorIn("regIOobject::write()")
47             << "bad object " << name()
48             << endl;
50         return false;
51     }
53     if (instance().empty())
54     {
55         SeriousErrorIn("regIOobject::write()")
56             << "instance undefined for object " << name()
57             << endl;
59         return false;
60     }
62     if
63     (
64         instance() != time().timeName()
65      && instance() != time().system()
66      && instance() != time().caseSystem()
67      && instance() != time().constant()
68      && instance() != time().caseConstant()
69     )
70     {
71         const_cast<regIOobject&>(*this).instance() = time().timeName();
72     }
74     mkDir(path());
76     if (OFstream::debug)
77     {
78         Info<< "regIOobject::write() : "
79             << "writing file " << objectPath();
80     }
83     bool osGood = false;
85     {
86         // Try opening an OFstream for object
87         OFstream os(objectPath(), fmt, ver, cmp);
89         // If any of these fail, return (leave error handling to Ostream class)
90         if (!os.good())
91         {
92             return false;
93         }
95         if (!writeHeader(os))
96         {
97             return false;
98         }
100         // Write the data to the Ostream
101         if (!writeData(os))
102         {
103             return false;
104         }
106         writeEndDivider(os);
108         osGood = os.good();
109     }
111     if (OFstream::debug)
112     {
113         Info<< " .... written" << endl;
114     }
116     // Only update the lastModified_ time if this object is re-readable,
117     // i.e. lastModified_ is already set
118     if (lastModified_)
119     {
120         lastModified_ = lastModified(objectPath());
121     }
123     return osGood;
127 bool Foam::regIOobject::write() const
129     return writeObject
130     (
131         time().writeFormat(),
132         IOstream::currentVersion,
133         time().writeCompression()
134     );
137 // ************************************************************************* //