Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / IOobject / IOobject.C
blob5f492f74f452de876014d41684468aefb5b83878
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "IOobject.H"
27 #include "Time.H"
28 #include "IFstream.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::IOobject, 0);
34 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
36 // Return components following the IOobject requirements
38 // behaviour
39 //    input               IOobject(instance, local, name)
40 //    -----               ------
41 //    "foo"               ("", "", "foo")
42 //    "foo/bar"           ("foo", "", "bar")
43 //    "/XXX"              ERROR - no absolute path
44 //    "foo/bar/"          ERROR - no name
45 //    "foo/xxx/bar"       ("foo", "xxx", "bar")
46 //    "foo/xxx/yyy/bar"   ("foo", "xxx/yyy", "bar")
47 bool Foam::IOobject::IOobject::fileNameComponents
49     const fileName& path,
50     fileName& instance,
51     fileName& local,
52     word& name
55     instance.clear();
56     local.clear();
57     name.clear();
59     // called with directory
60     if (isDir(path))
61     {
62         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
63             << " called with directory: " << path << "\n";
64         return false;
65     }
67     if (path.isAbsolute())
68     {
69         // called with absolute path
70         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
71             << "called with absolute path: " << path << "\n";
72         return false;
73     }
75     string::size_type first = path.find('/');
77     if (first == string::npos)
78     {
79         // no '/' found - no instance or local
81         // check afterwards
82         name.string::operator=(path);
83     }
84     else
85     {
86         instance = path.substr(0, first);
88         string::size_type last = path.rfind('/');
89         if (last > first)
90         {
91             // with local
92             local = path.substr(first+1, last-first-1);
93         }
95         // check afterwards
96         name.string::operator=(path.substr(last+1));
97     }
100     // check for valid (and stripped) name, regardless of the debug level
101     if (name.empty() || string::stripInvalid<word>(name))
102     {
103         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
104             << "has invalid word for name: \"" << name
105             << "\"\nwhile processing path: " << path << "\n";
106         return false;
107     }
109     return true;
113 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
115 Foam::IOobject::IOobject
117     const word& name,
118     const fileName& instance,
119     const objectRegistry& registry,
120     readOption ro,
121     writeOption wo,
122     bool registerObject
125     name_(name),
126     headerClassName_(typeName),
127     note_(),
128     instance_(instance),
129     local_(),
130     db_(registry),
131     rOpt_(ro),
132     wOpt_(wo),
133     registerObject_(registerObject),
134     objState_(GOOD)
136     if (objectRegistry::debug)
137     {
138         Info<< "Constructing IOobject called " << name_
139             << " of type " << headerClassName_
140             << endl;
141     }
145 Foam::IOobject::IOobject
147     const word& name,
148     const fileName& instance,
149     const fileName& local,
150     const objectRegistry& registry,
151     readOption ro,
152     writeOption wo,
153     bool registerObject
156     name_(name),
157     headerClassName_(typeName),
158     note_(),
159     instance_(instance),
160     local_(local),
161     db_(registry),
162     rOpt_(ro),
163     wOpt_(wo),
164     registerObject_(registerObject),
165     objState_(GOOD)
167     if (objectRegistry::debug)
168     {
169         Info<< "Constructing IOobject called " << name_
170             << " of type " << headerClassName_
171             << endl;
172     }
176 Foam::IOobject::IOobject
178     const fileName& path,
179     const objectRegistry& registry,
180     readOption ro,
181     writeOption wo,
182     bool registerObject
185     name_(),
186     headerClassName_(typeName),
187     note_(),
188     instance_(),
189     local_(),
190     db_(registry),
191     rOpt_(ro),
192     wOpt_(wo),
193     registerObject_(registerObject),
194     objState_(GOOD)
196     if (!fileNameComponents(path, instance_, local_, name_))
197     {
198         FatalErrorIn
199         (
200             "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
201         )
202             << " invalid path specification\n"
203             << exit(FatalError);
204     }
206     if (objectRegistry::debug)
207     {
208         Info<< "Constructing IOobject called " << name_
209             << " of type " << headerClassName_
210             << endl;
211     }
215 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
217 Foam::IOobject::~IOobject()
221 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
223 const Foam::objectRegistry& Foam::IOobject::db() const
225     return db_;
229 const Foam::Time& Foam::IOobject::time() const
231     return db_.time();
235 const Foam::fileName& Foam::IOobject::caseName() const
237     return time().caseName();
241 const Foam::fileName& Foam::IOobject::rootPath() const
243     return time().rootPath();
247 Foam::fileName Foam::IOobject::path() const
249     return rootPath()/caseName()/instance()/db_.dbDir()/local();
253 Foam::fileName Foam::IOobject::path
255     const word& instance,
256     const fileName& local
257 ) const
259     return rootPath()/caseName()/instance/db_.dbDir()/local;
263 Foam::fileName Foam::IOobject::filePath() const
265     fileName path = this->path();
266     fileName objectPath = path/name();
268     if (isFile(objectPath))
269     {
270         return objectPath;
271     }
272     else
273     {
274         if
275         (
276             time().processorCase()
277          && (
278                 instance() == time().system()
279              || instance() == time().constant()
280             )
281         )
282         {
283             fileName parentObjectPath =
284                 rootPath()/caseName()
285                /".."/instance()/db_.dbDir()/local()/name();
287             if (isFile(parentObjectPath))
288             {
289                 return parentObjectPath;
290             }
291         }
293         if (!isDir(path))
294         {
295             word newInstancePath = time().findInstancePath(instant(instance()));
297             if (newInstancePath.size())
298             {
299                 fileName fName
300                 (
301                     rootPath()/caseName()
302                    /newInstancePath/db_.dbDir()/local()/name()
303                 );
305                 if (isFile(fName))
306                 {
307                     return fName;
308                 }
309             }
310         }
311     }
313     return fileName::null;
317 Foam::Istream* Foam::IOobject::objectStream()
319     return objectStream(filePath());
323 Foam::Istream* Foam::IOobject::objectStream(const fileName& fName)
325     if (fName.size())
326     {
327         IFstream* isPtr = new IFstream(fName);
329         if (isPtr->good())
330         {
331             return isPtr;
332         }
333         else
334         {
335             delete isPtr;
336             return NULL;
337         }
338     }
339     else
340     {
341         return NULL;
342     }
346 bool Foam::IOobject::headerOk()
348     bool ok = true;
350     Istream* isPtr = objectStream();
352     // If the stream has failed return
353     if (!isPtr)
354     {
355         if (objectRegistry::debug)
356         {
357             Info
358                 << "IOobject::headerOk() : "
359                 << "file " << objectPath() << " could not be opened"
360                 << endl;
361         }
363         ok = false;
364     }
365     else
366     {
367         // Try reading header
368         if (!readHeader(*isPtr))
369         {
370             if (objectRegistry::debug)
371             {
372                 IOWarningIn("IOobject::headerOk()", (*isPtr))
373                     << "failed to read header of file " << objectPath()
374                     << endl;
375             }
377             ok = false;
378         }
379     }
381     delete isPtr;
383     return ok;
387 void Foam::IOobject::setBad(const string& s)
389     if (objState_ != GOOD)
390     {
391         FatalErrorIn("IOobject::setBad(const string&)")
392             << "recurrent failure for object " << s
393             << exit(FatalError);
394     }
396     if (error::level)
397     {
398         Info<< "IOobject::setBad(const string&) : "
399             << "broken object " << s << info() << endl;
400     }
402     objState_ = BAD;
406 void Foam::IOobject::operator=(const IOobject& io)
408     name_ = io.name_;
409     headerClassName_ = io.headerClassName_;
410     note_ = io.note_;
411     instance_ = io.instance_;
412     local_ = io.local_;
413     rOpt_ = io.rOpt_;
414     wOpt_ = io.wOpt_;
415     objState_ = io.objState_;
419 // ************************************************************************* //