1 /*---------------------------------------------------------------------------*\
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 -------------------------------------------------------------------------------
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 \*---------------------------------------------------------------------------*/
28 #include "objectRegistry.H"
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 string::size_type first = path.find('/');
71 // called with absolute path
72 WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
73 << "called with absolute path: " << path << "\n";
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
124 headerClassName_(typeName),
131 registerObject_(false),
134 if (objectRegistry::debug)
136 Info<< "Constructing IOobject called " << name_
137 << " of type " << headerClassName_
143 Foam::IOobject::IOobject
146 const fileName& instance,
147 const objectRegistry& registry,
153 name_(registry.mangleFileName(name)),
154 headerClassName_(typeName),
161 registerObject_(registerObject),
164 if (objectRegistry::debug)
166 Info<< "Constructing IOobject called " << name_
167 << " of type " << headerClassName_
173 Foam::IOobject::IOobject
176 const fileName& instance,
177 const fileName& local,
178 const objectRegistry& registry,
184 name_(registry.mangleFileName(name)),
185 headerClassName_(typeName),
192 registerObject_(registerObject),
195 if (objectRegistry::debug)
197 Info<< "Constructing IOobject called " << name_
198 << " of type " << headerClassName_
204 Foam::IOobject::IOobject
206 const fileName& path,
207 const objectRegistry& registry,
214 headerClassName_(typeName),
221 registerObject_(registerObject),
224 if (!fileNameComponents(path, instance_, local_, name_))
228 "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
230 << " invalid path specification\n"
234 if (objectRegistry::debug)
236 Info<< "Constructing IOobject called " << name_
237 << " of type " << headerClassName_
243 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
245 Foam::IOobject::~IOobject()
249 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
251 const Foam::objectRegistry& Foam::IOobject::db() const
257 const Foam::Time& Foam::IOobject::time() const
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
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))
304 time().processorCase()
306 instance() == time().system()
307 || instance() == time().constant()
311 fileName parentObjectPath =
312 rootPath()/caseName()
313 /".."/instance()/db_.dbDir()/local()/name();
315 if (isFile(parentObjectPath))
317 return parentObjectPath;
323 word newInstancePath = time().findInstancePath(instant(instance()));
325 if (newInstancePath.size())
329 rootPath()/caseName()
330 /newInstancePath/db_.dbDir()/local()/name()
341 return fileName::null;
345 Foam::Istream* Foam::IOobject::objectStream()
347 fileName fName = filePath();
351 IFstream* isPtr = new IFstream(fName);
370 bool Foam::IOobject::headerOk()
374 Istream* isPtr = objectStream();
376 // If the stream has failed return
379 if (objectRegistry::debug)
382 << "IOobject::headerOk() : "
383 << "file " << objectPath() << " could not be opened"
391 // Try reading header
392 if (!readHeader(*isPtr))
394 if (objectRegistry::debug)
396 IOWarningIn("IOobject::headerOk()", (*isPtr))
397 << "failed to read header of file " << objectPath()
411 void Foam::IOobject::setBad(const string& s)
413 if (objState_ != GOOD)
415 FatalErrorIn("IOobject::setBad(const string&)")
416 << "recurrent failure for object " << s
422 Info<< "IOobject::setBad(const string&) : "
423 << "broken object " << s << info() << endl;
430 void Foam::IOobject::operator=(const IOobject& io)
433 headerClassName_ = io.headerClassName_;
435 instance_ = io.instance_;
439 objState_ = io.objState_;
443 // ************************************************************************* //