initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOobject / IOobject.C
blobbb8be2e3ab7dce1adf1855c04527a25ddce5a550
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 \*---------------------------------------------------------------------------*/
27 #include "IOobject.H"
28 #include "Time.H"
29 #include "IFstream.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 namespace Foam
35     defineTypeNameAndDebug(IOobject, 0);
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 Foam::IOobject::IOobject
42     const word& name,
43     const fileName& instance,
44     const objectRegistry& registry,
45     readOption ro,
46     writeOption wo,
47     bool registerObject
50     name_(name),
51     headerClassName_(typeName),
52     note_(),
53     instance_(instance),
54     local_(),
55     db_(registry),
56     rOpt_(ro),
57     wOpt_(wo),
58     registerObject_(registerObject),
59     objState_(GOOD)
61     if (objectRegistry::debug)
62     {
63         Info<< "Constructing IOobject called " << name_
64             << " of type " << headerClassName_
65             << endl;
66     }
70 Foam::IOobject::IOobject
72     const word& name,
73     const fileName& instance,
74     const fileName& local,
75     const objectRegistry& registry,
76     readOption ro,
77     writeOption wo,
78     bool registerObject
81     name_(name),
82     headerClassName_(typeName),
83     note_(),
84     instance_(instance),
85     local_(local),
86     db_(registry),
87     rOpt_(ro),
88     wOpt_(wo),
89     registerObject_(registerObject),
90     objState_(GOOD)
92     if (objectRegistry::debug)
93     {
94         Info<< "Constructing IOobject called " << name_
95             << " of type " << headerClassName_
96             << endl;
97     }
101 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
103 Foam::IOobject::~IOobject()
107 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
109 const Foam::objectRegistry& Foam::IOobject::db() const
111     return db_;
115 const Foam::Time& Foam::IOobject::time() const
117     return db_.time();
121 const Foam::fileName& Foam::IOobject::caseName() const
123     return time().caseName();
127 const Foam::fileName& Foam::IOobject::rootPath() const
129     return time().rootPath();
133 Foam::fileName Foam::IOobject::path() const
135     return rootPath()/caseName()/instance()/db_.dbDir()/local();
139 Foam::fileName Foam::IOobject::path
141     const word& instance,
142     const fileName& local
143 ) const
145     return rootPath()/caseName()/instance/db_.dbDir()/local;
149 Foam::fileName Foam::IOobject::filePath() const
151     fileName path = this->path();
152     fileName objectPath = path/name();
154     if (file(objectPath))
155     {
156         return objectPath;
157     }
158     else
159     {
160         if
161         (
162             time().processorCase()
163          && (
164                 instance() == time().system()
165              || instance() == time().constant()
166             )
167         )
168         {
169             fileName parentObjectPath =
170                 rootPath()/caseName()
171                /".."/instance()/db_.dbDir()/local()/name();
173             if (file(parentObjectPath))
174             {
175                 return parentObjectPath;
176             }
177         }
179         if (!dir(path))
180         {
181             word newInstancePath = time().findInstancePath(instant(instance()));
183             if (newInstancePath.size())
184             {
185                 fileName fName
186                 (
187                     rootPath()/caseName()
188                    /newInstancePath/db_.dbDir()/local()/name()
189                 );
191                 if (file(fName))
192                 {
193                     return fName;
194                 }
195             }
196         }
197     }
199     return fileName::null;
203 Foam::Istream* Foam::IOobject::objectStream()
205     fileName fName = filePath();
207     if (fName != fileName::null)
208     {
209         IFstream* isPtr = new IFstream(fName);
211         if (isPtr->good())
212         {
213             return isPtr;
214         }
215         else
216         {
217             delete isPtr;
218             return NULL;
219         }
220     }
221     else
222     {
223         return NULL;
224     }
228 bool Foam::IOobject::headerOk()
230     bool ok = true;
232     Istream* isPtr = objectStream();
234     // If the stream has failed return
235     if (!isPtr)
236     {
237         if (objectRegistry::debug)
238         {
239             Info
240                 << "IOobject::headerOk() : "
241                 << "file " << objectPath() << " could not be opened"
242                 << endl;
243         }
245         ok = false;
246     }
247     else
248     {
249         // Try reading header
250         if (!readHeader(*isPtr))
251         {
252             if (objectRegistry::debug)
253             {
254                 IOWarningIn("IOobject::headerOk()", (*isPtr))
255                     << "failed to read header of file " << objectPath()
256                     << endl;
257             }
259             ok = false;
260         }
261     }
263     delete isPtr;
265     return ok;
269 void Foam::IOobject::setBad(const string& s)
271     if (objState_ != GOOD)
272     {
273         FatalErrorIn("IOobject::setBad(const string&)")
274             << "recurrent failure for object " << s
275             << exit(FatalError);
276     }
278     if (error::level)
279     {
280         Info<< "IOobject::setBad(const string&) : "
281             << "broken object " << s << info() << endl;
282     }
284     objState_ = BAD;
288 void Foam::IOobject::operator=(const IOobject& io)
290     name_ = io.name_;
291     headerClassName_ = io.headerClassName_;
292     note_ = io.note_;
293     instance_ = io.instance_;
294     local_ = io.local_;
295     rOpt_ = io.rOpt_;
296     wOpt_ = io.wOpt_;
297     objState_ = io.objState_;
301 // ************************************************************************* //