1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 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
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
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 \*---------------------------------------------------------------------------*/
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::IOobject, 0);
34 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
36 // Return components following the IOobject requirements
39 // input IOobject(instance, local, name)
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
59 // called with directory
62 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
63 << " called with directory: " << path << "\n";
67 if (path.isAbsolute())
69 // called with absolute path
70 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
71 << "called with absolute path: " << path << "\n";
75 string::size_type first = path.find('/');
77 if (first == string::npos)
79 // no '/' found - no instance or local
82 name.string::operator=(path);
86 instance = path.substr(0, first);
88 string::size_type last = path.rfind('/');
92 local = path.substr(first+1, last-first-1);
96 name.string::operator=(path.substr(last+1));
100 // check for valid (and stripped) name, regardless of the debug level
101 if (name.empty() || string::stripInvalid<word>(name))
103 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
104 << "has invalid word for name: \"" << name
105 << "\"\nwhile processing path: " << path << "\n";
113 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
115 Foam::IOobject::IOobject
118 const fileName& instance,
119 const objectRegistry& registry,
126 headerClassName_(typeName),
133 registerObject_(registerObject),
136 if (objectRegistry::debug)
138 Info<< "Constructing IOobject called " << name_
139 << " of type " << headerClassName_
145 Foam::IOobject::IOobject
148 const fileName& instance,
149 const fileName& local,
150 const objectRegistry& registry,
157 headerClassName_(typeName),
164 registerObject_(registerObject),
167 if (objectRegistry::debug)
169 Info<< "Constructing IOobject called " << name_
170 << " of type " << headerClassName_
176 Foam::IOobject::IOobject
178 const fileName& path,
179 const objectRegistry& registry,
186 headerClassName_(typeName),
193 registerObject_(registerObject),
196 if (!fileNameComponents(path, instance_, local_, name_))
200 "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
202 << " invalid path specification\n"
206 if (objectRegistry::debug)
208 Info<< "Constructing IOobject called " << name_
209 << " of type " << headerClassName_
215 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
217 Foam::IOobject::~IOobject()
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
223 const Foam::objectRegistry& Foam::IOobject::db() const
229 const Foam::Time& Foam::IOobject::time() const
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
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))
276 time().processorCase()
278 instance() == time().system()
279 || instance() == time().constant()
283 fileName parentObjectPath =
284 rootPath()/caseName()
285 /".."/instance()/db_.dbDir()/local()/name();
287 if (isFile(parentObjectPath))
289 return parentObjectPath;
295 word newInstancePath = time().findInstancePath(instant(instance()));
297 if (newInstancePath.size())
301 rootPath()/caseName()
302 /newInstancePath/db_.dbDir()/local()/name()
313 return fileName::null;
317 Foam::Istream* Foam::IOobject::objectStream()
319 return objectStream(filePath());
323 Foam::Istream* Foam::IOobject::objectStream(const fileName& fName)
327 IFstream* isPtr = new IFstream(fName);
346 bool Foam::IOobject::headerOk()
350 Istream* isPtr = objectStream();
352 // If the stream has failed return
355 if (objectRegistry::debug)
358 << "IOobject::headerOk() : "
359 << "file " << objectPath() << " could not be opened"
367 // Try reading header
368 if (!readHeader(*isPtr))
370 if (objectRegistry::debug)
372 IOWarningIn("IOobject::headerOk()", (*isPtr))
373 << "failed to read header of file " << objectPath()
387 void Foam::IOobject::setBad(const string& s)
389 if (objState_ != GOOD)
391 FatalErrorIn("IOobject::setBad(const string&)")
392 << "recurrent failure for object " << s
398 Info<< "IOobject::setBad(const string&) : "
399 << "broken object " << s << info() << endl;
406 void Foam::IOobject::operator=(const IOobject& io)
409 headerClassName_ = io.headerClassName_;
411 instance_ = io.instance_;
415 objState_ = io.objState_;
419 // ************************************************************************* //