Newer
Older
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "IOobject.H"
#include "Time.H"
#include "IFstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(Foam::IOobject, 0);
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
// Return components following the IOobject requirements
//
// input IOobject(instance, local, name)
// ----- ------
// "foo" ("", "", "foo")
// "foo/bar" ("foo", "", "bar")
// "/XXX" ERROR - no absolute path
// "foo/bar/" ERROR - no name
// "foo/xxx/bar" ("foo", "xxx", "bar")
// "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
bool Foam::IOobject::IOobject::fileNameComponents
(
const fileName& path,
fileName& instance,
fileName& local,
word& name
)
{
instance.clear();
local.clear();
name.clear();
// called with directory
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
<< " called with directory: " << path << "\n";
return false;
}
string::size_type first = path.find('/');
if (first == 0)
{
// called with absolute path
WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
<< "called with absolute path: " << path << "\n";
return false;
}
if (first == string::npos)
{
// no '/' found - no instance or local
// check afterwards
name.string::operator=(path);
}
else
{
instance = path.substr(0, first);
string::size_type last = path.rfind('/');
if (last > first)
{
// with local
local = path.substr(first+1, last-first-1);
}
// check afterwards
name.string::operator=(path.substr(last+1));
}
// check for valid (and stripped) name, regardless of the debug level
if (name.empty() || string::stripInvalid<word>(name))
{
WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
<< "has invalid word for name: \"" << name
<< "\"\nwhile processing path: " << path << "\n";
return false;
}
return true;
}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IOobject::IOobject
(
const word& name,
const fileName& instance,
const objectRegistry& registry,
readOption ro,
writeOption wo,
bool registerObject
)
:
name_(name),
headerClassName_(typeName),
note_(),
instance_(instance),
local_(),
db_(registry),
rOpt_(ro),
wOpt_(wo),
registerObject_(registerObject),
objState_(GOOD)
{
if (objectRegistry::debug)
{
Info<< "Constructing IOobject called " << name_
<< " of type " << headerClassName_
<< endl;
}
}
Foam::IOobject::IOobject
(
const word& name,
const fileName& instance,
const fileName& local,
const objectRegistry& registry,
readOption ro,
writeOption wo,
bool registerObject
)
:
name_(name),
headerClassName_(typeName),
note_(),
instance_(instance),
local_(local),
db_(registry),
rOpt_(ro),
wOpt_(wo),
registerObject_(registerObject),
objState_(GOOD)
{
if (objectRegistry::debug)
{
Info<< "Constructing IOobject called " << name_
<< " of type " << headerClassName_
<< endl;
}
}
Foam::IOobject::IOobject
(
const fileName& path,
const objectRegistry& registry,
readOption ro,
writeOption wo,
bool registerObject
)
:
name_(),
headerClassName_(typeName),
note_(),
instance_(),
local_(),
db_(registry),
rOpt_(ro),
wOpt_(wo),
registerObject_(registerObject),
objState_(GOOD)
{
if (!fileNameComponents(path, instance_, local_, name_))
{
FatalErrorIn
(
"IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
)
<< " invalid path specification\n"
<< exit(FatalError);
}
if (objectRegistry::debug)
{
Info<< "Constructing IOobject called " << name_
<< " of type " << headerClassName_
<< endl;
}
}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
Foam::IOobject::~IOobject()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::objectRegistry& Foam::IOobject::db() const
{
return db_;
}
const Foam::Time& Foam::IOobject::time() const
{
return db_.time();
}
const Foam::fileName& Foam::IOobject::caseName() const
{
return time().caseName();
}
const Foam::fileName& Foam::IOobject::rootPath() const
{
return time().rootPath();
}
Foam::fileName Foam::IOobject::path() const
{
return rootPath()/caseName()/instance()/db_.dbDir()/local();
}
Foam::fileName Foam::IOobject::path
(
const word& instance,
const fileName& local
) const
{
return rootPath()/caseName()/instance/db_.dbDir()/local;
}
Foam::fileName Foam::IOobject::filePath() const
{
fileName path = this->path();
fileName objectPath = path/name();
if (isFile(objectPath))
{
return objectPath;
}
else
{
if
(
time().processorCase()
&& (
instance() == time().system()
|| instance() == time().constant()
)
)
{
fileName parentObjectPath =
rootPath()/caseName()
/".."/instance()/db_.dbDir()/local()/name();
if (isFile(parentObjectPath))
{
return parentObjectPath;
}
}
{
word newInstancePath = time().findInstancePath(instant(instance()));
if (newInstancePath.size())
{
fileName fName
(
rootPath()/caseName()
/newInstancePath/db_.dbDir()/local()/name()
);
{
return fName;
}
}
}
}
return fileName::null;
}
Foam::Istream* Foam::IOobject::objectStream()
{
return objectStream(filePath());
}
Foam::Istream* Foam::IOobject::objectStream(const fileName& fName)
{
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{
IFstream* isPtr = new IFstream(fName);
if (isPtr->good())
{
return isPtr;
}
else
{
delete isPtr;
return NULL;
}
}
else
{
return NULL;
}
}
bool Foam::IOobject::headerOk()
{
bool ok = true;
Istream* isPtr = objectStream();
// If the stream has failed return
if (!isPtr)
{
if (objectRegistry::debug)
{
Info
<< "IOobject::headerOk() : "
<< "file " << objectPath() << " could not be opened"
<< endl;
}
ok = false;
}
else
{
// Try reading header
if (!readHeader(*isPtr))
{
if (objectRegistry::debug)
{
IOWarningIn("IOobject::headerOk()", (*isPtr))
<< "failed to read header of file " << objectPath()
<< endl;
}
ok = false;
}
}
delete isPtr;
return ok;
}
void Foam::IOobject::setBad(const string& s)
{
if (objState_ != GOOD)
{
FatalErrorIn("IOobject::setBad(const string&)")
<< "recurrent failure for object " << s
<< exit(FatalError);
}
if (error::level)
{
Info<< "IOobject::setBad(const string&) : "
<< "broken object " << s << info() << endl;
}
objState_ = BAD;
}
void Foam::IOobject::operator=(const IOobject& io)
{
name_ = io.name_;
headerClassName_ = io.headerClassName_;
note_ = io.note_;
instance_ = io.instance_;
local_ = io.local_;
rOpt_ = io.rOpt_;
wOpt_ = io.wOpt_;
objState_ = io.objState_;
}
// ************************************************************************* //