initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / IOobject / IOobject.C
blob1de08115736c84aa9736423472c88900586c0460
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 \*---------------------------------------------------------------------------*/
27 #include "IOobject.H"
28 #include "Time.H"
29 #include "IFstream.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::IOobject, 0);
35 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
37 // Return components following the IOobject requirements
39 // behaviour
40 //    input               IOobject(instance, local, name)
41 //    -----               ------
42 //    "foo"               ("", "", "foo")
43 //    "foo/bar"           ("foo", "", "bar")
44 //    "/XXX"              ERROR - no absolute path
45 //    "foo/bar/"          ERROR - no name
46 //    "foo/xxx/bar"       ("foo", "xxx", "bar")
47 //    "foo/xxx/yyy/bar"   ("foo", "xxx/yyy", "bar")
48 bool Foam::IOobject::IOobject::fileNameComponents
50     const fileName& path,
51     fileName& instance,
52     fileName& local,
53     word& name
56     instance.clear();
57     local.clear();
58     name.clear();
60     // called with directory
61     if (isDir(path))
62     {
63         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
64             << " called with directory: " << path << "\n";
65         return false;
66     }
68     string::size_type first = path.find('/');
70     if (first == 0)
71     {
72         // called with absolute path
73         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
74             << "called with absolute path: " << path << "\n";
75         return false;
76     }
78     if (first == string::npos)
79     {
80         // no '/' found - no instance or local
82         // check afterwards
83         name.string::operator=(path);
84     }
85     else
86     {
87         instance = path.substr(0, first);
89         string::size_type last = path.rfind('/');
90         if (last > first)
91         {
92             // with local
93             local = path.substr(first+1, last-first-1);
94         }
96         // check afterwards
97         name.string::operator=(path.substr(last+1));
98     }
101     // check for valid (and stripped) name, regardless of the debug level
102     if (name.empty() || string::stripInvalid<word>(name))
103     {
104         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
105             << "has invalid word for name: \"" << name
106             << "\"\nwhile processing path: " << path << "\n";
107         return false;
108     }
110     return true;
114 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
116 Foam::IOobject::IOobject
118     const word& name,
119     const fileName& instance,
120     const objectRegistry& registry,
121     readOption ro,
122     writeOption wo,
123     bool registerObject
126     name_(name),
127     headerClassName_(typeName),
128     note_(),
129     instance_(instance),
130     local_(),
131     db_(registry),
132     rOpt_(ro),
133     wOpt_(wo),
134     registerObject_(registerObject),
135     objState_(GOOD)
137     if (objectRegistry::debug)
138     {
139         Info<< "Constructing IOobject called " << name_
140             << " of type " << headerClassName_
141             << endl;
142     }
146 Foam::IOobject::IOobject
148     const word& name,
149     const fileName& instance,
150     const fileName& local,
151     const objectRegistry& registry,
152     readOption ro,
153     writeOption wo,
154     bool registerObject
157     name_(name),
158     headerClassName_(typeName),
159     note_(),
160     instance_(instance),
161     local_(local),
162     db_(registry),
163     rOpt_(ro),
164     wOpt_(wo),
165     registerObject_(registerObject),
166     objState_(GOOD)
168     if (objectRegistry::debug)
169     {
170         Info<< "Constructing IOobject called " << name_
171             << " of type " << headerClassName_
172             << endl;
173     }
177 Foam::IOobject::IOobject
179     const fileName& path,
180     const objectRegistry& registry,
181     readOption ro,
182     writeOption wo,
183     bool registerObject
186     name_(),
187     headerClassName_(typeName),
188     note_(),
189     instance_(),
190     local_(),
191     db_(registry),
192     rOpt_(ro),
193     wOpt_(wo),
194     registerObject_(registerObject),
195     objState_(GOOD)
197     if (!fileNameComponents(path, instance_, local_, name_))
198     {
199         FatalErrorIn
200         (
201             "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
202         )
203             << " invalid path specification\n"
204             << exit(FatalError);
205     }
207     if (objectRegistry::debug)
208     {
209         Info<< "Constructing IOobject called " << name_
210             << " of type " << headerClassName_
211             << endl;
212     }
216 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
218 Foam::IOobject::~IOobject()
222 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
224 const Foam::objectRegistry& Foam::IOobject::db() const
226     return db_;
230 const Foam::Time& Foam::IOobject::time() const
232     return db_.time();
236 const Foam::fileName& Foam::IOobject::caseName() const
238     return time().caseName();
242 const Foam::fileName& Foam::IOobject::rootPath() const
244     return time().rootPath();
248 Foam::fileName Foam::IOobject::path() const
250     return rootPath()/caseName()/instance()/db_.dbDir()/local();
254 Foam::fileName Foam::IOobject::path
256     const word& instance,
257     const fileName& local
258 ) const
260     return rootPath()/caseName()/instance/db_.dbDir()/local;
264 Foam::fileName Foam::IOobject::filePath() const
266     fileName path = this->path();
267     fileName objectPath = path/name();
269     if (isFile(objectPath))
270     {
271         return objectPath;
272     }
273     else
274     {
275         if
276         (
277             time().processorCase()
278          && (
279                 instance() == time().system()
280              || instance() == time().constant()
281             )
282         )
283         {
284             fileName parentObjectPath =
285                 rootPath()/caseName()
286                /".."/instance()/db_.dbDir()/local()/name();
288             if (isFile(parentObjectPath))
289             {
290                 return parentObjectPath;
291             }
292         }
294         if (!isDir(path))
295         {
296             word newInstancePath = time().findInstancePath(instant(instance()));
298             if (newInstancePath.size())
299             {
300                 fileName fName
301                 (
302                     rootPath()/caseName()
303                    /newInstancePath/db_.dbDir()/local()/name()
304                 );
306                 if (isFile(fName))
307                 {
308                     return fName;
309                 }
310             }
311         }
312     }
314     return fileName::null;
318 Foam::Istream* Foam::IOobject::objectStream()
320     fileName fName = filePath();
322     if (fName.size())
323     {
324         IFstream* isPtr = new IFstream(fName);
326         if (isPtr->good())
327         {
328             return isPtr;
329         }
330         else
331         {
332             delete isPtr;
333             return NULL;
334         }
335     }
336     else
337     {
338         return NULL;
339     }
343 bool Foam::IOobject::headerOk()
345     bool ok = true;
347     Istream* isPtr = objectStream();
349     // If the stream has failed return
350     if (!isPtr)
351     {
352         if (objectRegistry::debug)
353         {
354             Info
355                 << "IOobject::headerOk() : "
356                 << "file " << objectPath() << " could not be opened"
357                 << endl;
358         }
360         ok = false;
361     }
362     else
363     {
364         // Try reading header
365         if (!readHeader(*isPtr))
366         {
367             if (objectRegistry::debug)
368             {
369                 IOWarningIn("IOobject::headerOk()", (*isPtr))
370                     << "failed to read header of file " << objectPath()
371                     << endl;
372             }
374             ok = false;
375         }
376     }
378     delete isPtr;
380     return ok;
384 void Foam::IOobject::setBad(const string& s)
386     if (objState_ != GOOD)
387     {
388         FatalErrorIn("IOobject::setBad(const string&)")
389             << "recurrent failure for object " << s
390             << exit(FatalError);
391     }
393     if (error::level)
394     {
395         Info<< "IOobject::setBad(const string&) : "
396             << "broken object " << s << info() << endl;
397     }
399     objState_ = BAD;
403 void Foam::IOobject::operator=(const IOobject& io)
405     name_ = io.name_;
406     headerClassName_ = io.headerClassName_;
407     note_ = io.note_;
408     instance_ = io.instance_;
409     local_ = io.local_;
410     rOpt_ = io.rOpt_;
411     wOpt_ = io.wOpt_;
412     objState_ = io.objState_;
416 // ************************************************************************* //