Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / IOobject / IOobject.C
blobdccf354347b1520c664ce86ac639a910373790c6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "IOobject.H"
27 #include "IFstream.H"
28 #include "objectRegistry.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     string::size_type first = path.find('/');
69     if (first == 0)
70     {
71         // called with absolute path
72         WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
73             << "called with absolute path: " << path << "\n";
74         return false;
75     }
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 Time& time,
119     readOption ro,
120     writeOption wo
123     name_(name),
124     headerClassName_(typeName),
125     note_(),
126     instance_(""),
127     local_(),
128     db_(time),
129     rOpt_(ro),
130     wOpt_(wo),
131     registerObject_(false),
132     objState_(GOOD)
134     if (objectRegistry::debug)
135     {
136         Info<< "Constructing IOobject called " << name_
137             << " of type " << headerClassName_
138             << endl;
139     }
143 Foam::IOobject::IOobject
145     const word& name,
146     const fileName& instance,
147     const objectRegistry& registry,
148     readOption ro,
149     writeOption wo,
150     bool registerObject
153     name_(registry.mangleFileName(name)),
154     headerClassName_(typeName),
155     note_(),
156     instance_(instance),
157     local_(),
158     db_(registry),
159     rOpt_(ro),
160     wOpt_(wo),
161     registerObject_(registerObject),
162     objState_(GOOD)
164     if (objectRegistry::debug)
165     {
166         Info<< "Constructing IOobject called " << name_
167             << " of type " << headerClassName_
168             << endl;
169     }
173 Foam::IOobject::IOobject
175     const word& name,
176     const fileName& instance,
177     const fileName& local,
178     const objectRegistry& registry,
179     readOption ro,
180     writeOption wo,
181     bool registerObject
184     name_(registry.mangleFileName(name)),
185     headerClassName_(typeName),
186     note_(),
187     instance_(instance),
188     local_(local),
189     db_(registry),
190     rOpt_(ro),
191     wOpt_(wo),
192     registerObject_(registerObject),
193     objState_(GOOD)
195     if (objectRegistry::debug)
196     {
197         Info<< "Constructing IOobject called " << name_
198             << " of type " << headerClassName_
199             << endl;
200     }
204 Foam::IOobject::IOobject
206     const fileName& path,
207     const objectRegistry& registry,
208     readOption ro,
209     writeOption wo,
210     bool registerObject
213     name_(),
214     headerClassName_(typeName),
215     note_(),
216     instance_(),
217     local_(),
218     db_(registry),
219     rOpt_(ro),
220     wOpt_(wo),
221     registerObject_(registerObject),
222     objState_(GOOD)
224     if (!fileNameComponents(path, instance_, local_, name_))
225     {
226         FatalErrorIn
227         (
228             "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
229         )
230             << " invalid path specification\n"
231             << exit(FatalError);
232     }
234     if (objectRegistry::debug)
235     {
236         Info<< "Constructing IOobject called " << name_
237             << " of type " << headerClassName_
238             << endl;
239     }
243 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
245 Foam::IOobject::~IOobject()
249 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
251 const Foam::objectRegistry& Foam::IOobject::db() const
253     return db_;
257 const Foam::Time& Foam::IOobject::time() const
259     return db_.time();
263 const Foam::fileName& Foam::IOobject::caseName() const
265     return time().caseName();
269 const Foam::fileName& Foam::IOobject::rootPath() const
271     return time().rootPath();
275 Foam::fileName Foam::IOobject::path() const
277     return rootPath()/caseName()/instance()/db_.dbDir()/local();
281 Foam::fileName Foam::IOobject::path
283     const word& instance,
284     const fileName& local
285 ) const
287     return rootPath()/caseName()/instance/db_.dbDir()/local;
291 Foam::fileName Foam::IOobject::filePath() const
293     fileName path = this->path();
294     fileName objectPath = path/name();
296     if (isFile(objectPath))
297     {
298         return objectPath;
299     }
300     else
301     {
302         if
303         (
304             time().processorCase()
305          && (
306                 instance() == time().system()
307              || instance() == time().constant()
308             )
309         )
310         {
311             fileName parentObjectPath =
312                 rootPath()/caseName()
313                /".."/instance()/db_.dbDir()/local()/name();
315             if (isFile(parentObjectPath))
316             {
317                 return parentObjectPath;
318             }
319         }
321         if (!isDir(path))
322         {
323             word newInstancePath = time().findInstancePath(instant(instance()));
325             if (newInstancePath.size())
326             {
327                 fileName fName
328                 (
329                     rootPath()/caseName()
330                    /newInstancePath/db_.dbDir()/local()/name()
331                 );
333                 if (isFile(fName))
334                 {
335                     return fName;
336                 }
337             }
338         }
339     }
341     return fileName::null;
345 Foam::Istream* Foam::IOobject::objectStream()
347     fileName fName = filePath();
349     if (fName.size())
350     {
351         IFstream* isPtr = new IFstream(fName);
353         if (isPtr->good())
354         {
355             return isPtr;
356         }
357         else
358         {
359             delete isPtr;
360             return NULL;
361         }
362     }
363     else
364     {
365         return NULL;
366     }
370 bool Foam::IOobject::headerOk()
372     bool ok = true;
374     Istream* isPtr = objectStream();
376     // If the stream has failed return
377     if (!isPtr)
378     {
379         if (objectRegistry::debug)
380         {
381             Info
382                 << "IOobject::headerOk() : "
383                 << "file " << objectPath() << " could not be opened"
384                 << endl;
385         }
387         ok = false;
388     }
389     else
390     {
391         // Try reading header
392         if (!readHeader(*isPtr))
393         {
394             if (objectRegistry::debug)
395             {
396                 IOWarningIn("IOobject::headerOk()", (*isPtr))
397                     << "failed to read header of file " << objectPath()
398                     << endl;
399             }
401             ok = false;
402         }
403     }
405     delete isPtr;
407     return ok;
411 void Foam::IOobject::setBad(const string& s)
413     if (objState_ != GOOD)
414     {
415         FatalErrorIn("IOobject::setBad(const string&)")
416             << "recurrent failure for object " << s
417             << exit(FatalError);
418     }
420     if (error::level)
421     {
422         Info<< "IOobject::setBad(const string&) : "
423             << "broken object " << s << info() << endl;
424     }
426     objState_ = BAD;
430 void Foam::IOobject::operator=(const IOobject& io)
432     name_ = io.name_;
433     headerClassName_ = io.headerClassName_;
434     note_ = io.note_;
435     instance_ = io.instance_;
436     local_ = io.local_;
437     rOpt_ = io.rOpt_;
438     wOpt_ = io.wOpt_;
439     objState_ = io.objState_;
443 // ************************************************************************* //