1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::IOobject, 0);
35 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
37 // Return components following the IOobject requirements
40 // input IOobject(instance, local, name)
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
60 // called with directory
63 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
64 << " called with directory: " << path << "\n";
68 string::size_type first = path.find('/');
72 // called with absolute path
73 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
74 << "called with absolute path: " << path << "\n";
78 if (first == string::npos)
80 // no '/' found - no instance or local
83 name.string::operator=(path);
87 instance = path.substr(0, first);
89 string::size_type last = path.rfind('/');
93 local = path.substr(first+1, last-first-1);
97 name.string::operator=(path.substr(last+1));
101 // check for valid (and stripped) name, regardless of the debug level
102 if (name.empty() || string::stripInvalid<word>(name))
104 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
105 << "has invalid word for name: \"" << name
106 << "\"\nwhile processing path: " << path << "\n";
114 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
116 Foam::IOobject::IOobject
119 const fileName& instance,
120 const objectRegistry& registry,
127 headerClassName_(typeName),
134 registerObject_(registerObject),
137 if (objectRegistry::debug)
139 Info<< "Constructing IOobject called " << name_
140 << " of type " << headerClassName_
146 Foam::IOobject::IOobject
149 const fileName& instance,
150 const fileName& local,
151 const objectRegistry& registry,
158 headerClassName_(typeName),
165 registerObject_(registerObject),
168 if (objectRegistry::debug)
170 Info<< "Constructing IOobject called " << name_
171 << " of type " << headerClassName_
177 Foam::IOobject::IOobject
179 const fileName& path,
180 const objectRegistry& registry,
187 headerClassName_(typeName),
194 registerObject_(registerObject),
197 if (!fileNameComponents(path, instance_, local_, name_))
201 "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
203 << " invalid path specification\n"
207 if (objectRegistry::debug)
209 Info<< "Constructing IOobject called " << name_
210 << " of type " << headerClassName_
216 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
218 Foam::IOobject::~IOobject()
222 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
224 const Foam::objectRegistry& Foam::IOobject::db() const
230 const Foam::Time& Foam::IOobject::time() const
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
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))
277 time().processorCase()
279 instance() == time().system()
280 || instance() == time().constant()
284 fileName parentObjectPath =
285 rootPath()/caseName()
286 /".."/instance()/db_.dbDir()/local()/name();
288 if (isFile(parentObjectPath))
290 return parentObjectPath;
296 word newInstancePath = time().findInstancePath(instant(instance()));
298 if (newInstancePath.size())
302 rootPath()/caseName()
303 /newInstancePath/db_.dbDir()/local()/name()
314 return fileName::null;
318 Foam::Istream* Foam::IOobject::objectStream()
320 fileName fName = filePath();
324 IFstream* isPtr = new IFstream(fName);
343 bool Foam::IOobject::headerOk()
347 Istream* isPtr = objectStream();
349 // If the stream has failed return
352 if (objectRegistry::debug)
355 << "IOobject::headerOk() : "
356 << "file " << objectPath() << " could not be opened"
364 // Try reading header
365 if (!readHeader(*isPtr))
367 if (objectRegistry::debug)
369 IOWarningIn("IOobject::headerOk()", (*isPtr))
370 << "failed to read header of file " << objectPath()
384 void Foam::IOobject::setBad(const string& s)
386 if (objState_ != GOOD)
388 FatalErrorIn("IOobject::setBad(const string&)")
389 << "recurrent failure for object " << s
395 Info<< "IOobject::setBad(const string&) : "
396 << "broken object " << s << info() << endl;
403 void Foam::IOobject::operator=(const IOobject& io)
406 headerClassName_ = io.headerClassName_;
408 instance_ = io.instance_;
412 objState_ = io.objState_;
416 // ************************************************************************* //