IOobject.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "IOobject.H"
27 #include "Time.H"
28 #include "IFstream.H"
29 #include "StaticHashTable.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(IOobject, 0);
36 }
37 
39 
40 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
41 
43 (
44  const fileName& path,
45  fileName& instance,
46  fileName& local,
47  word& name
48 )
49 {
50  instance.clear();
51  local.clear();
52  name.clear();
53 
54  // called with directory
55  if (isDir(path))
56  {
58  << " called with directory: " << path << endl;
59 
60  return false;
61  }
62 
63  if (path.isAbsolute())
64  {
65  string::size_type last = path.rfind('/');
66  instance = path.substr(0, last);
67 
68  // Check afterwards
69  name.string::operator=(path.substr(last+1));
70  }
71  else
72  {
73  string::size_type first = path.find('/');
74 
75  if (first == string::npos)
76  {
77  // no '/' found - no instance or local
78 
79  // check afterwards
80  name.string::operator=(path);
81  }
82  else
83  {
84  instance = path.substr(0, first);
85 
86  string::size_type last = path.rfind('/');
87  if (last > first)
88  {
89  // with local
90  local = path.substr(first+1, last-first-1);
91  }
92 
93  // check afterwards
94  name.string::operator=(path.substr(last+1));
95  }
96  }
97 
98 
99  // Check for valid (and stripped) name, regardless of the debug level
100  if (name.empty() || string::stripInvalid<word>(name))
101  {
103  << "has invalid word for name: \"" << name
104  << "\"\nwhile processing path: " << path << endl;
105 
106  return false;
107  }
108 
109  return true;
110 }
111 
112 
113 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114 
116 (
117  const word& name,
118  const fileName& instance,
119  const objectRegistry& registry,
120  readOption ro,
121  writeOption wo,
122  bool registerObject
123 )
124 :
125  name_(name),
126  headerClassName_(typeName),
127  note_(),
128  instance_(instance),
129  local_(),
130  db_(registry),
131  rOpt_(ro),
132  wOpt_(wo),
133  registerObject_(registerObject),
134  objState_(GOOD)
135 {
136  if (objectRegistry::debug)
137  {
138  Info<< "Constructing IOobject called " << name_
139  << " of type " << headerClassName_
140  << endl;
141  }
142 }
143 
144 
146 (
147  const word& name,
148  const fileName& instance,
149  const fileName& local,
150  const objectRegistry& registry,
151  readOption ro,
152  writeOption wo,
153  bool registerObject
154 )
155 :
156  name_(name),
157  headerClassName_(typeName),
158  note_(),
159  instance_(instance),
160  local_(local),
161  db_(registry),
162  rOpt_(ro),
163  wOpt_(wo),
164  registerObject_(registerObject),
165  objState_(GOOD)
166 {
167  if (objectRegistry::debug)
168  {
169  Info<< "Constructing IOobject called " << name_
170  << " of type " << headerClassName_
171  << endl;
172  }
173 }
174 
175 
177 (
178  const fileName& path,
179  const objectRegistry& registry,
180  readOption ro,
181  writeOption wo,
182  bool registerObject
183 )
184 :
185  name_(),
186  headerClassName_(typeName),
187  note_(),
188  instance_(),
189  local_(),
190  db_(registry),
191  rOpt_(ro),
192  wOpt_(wo),
193  registerObject_(registerObject),
194  objState_(GOOD)
195 {
196  if (!fileNameComponents(path, instance_, local_, name_))
197  {
199  << " invalid path specification"
200  << exit(FatalError);
201  }
202 
203  if (objectRegistry::debug)
204  {
205  Info<< "Constructing IOobject called " << name_
206  << " of type " << headerClassName_
207  << endl;
208  }
209 }
210 
211 
213 (
214  const IOobject& io,
215  const word& name
216 )
217 :
218  name_(name),
219  headerClassName_(io.headerClassName_),
220  note_(io.note_),
221  instance_(io.instance_),
222  local_(io.local_),
223  db_(io.db_),
224  rOpt_(io.rOpt_),
225  wOpt_(io.wOpt_),
226  registerObject_(io.registerObject_),
227  objState_(io.objState_)
228 {}
229 
230 
231 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
232 
234 {}
235 
236 
237 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
238 
240 {
241  return db_;
242 }
243 
244 
246 {
247  return db_.time();
248 }
249 
250 
252 {
253  return time().caseName();
254 }
255 
256 
258 {
259  word::size_type i = name_.find_last_of('.');
260 
261  if (i == word::npos || i == 0)
262  {
263  return word::null;
264  }
265  else
266  {
267  return name_.substr(i+1, word::npos);
268  }
269 }
270 
271 
273 {
274  word::size_type i = name_.find_last_of('.');
275 
276  if (i == word::npos || i == 0)
277  {
278  return name_;
279  }
280  else
281  {
282  return name_.substr(0, i);
283  }
284 }
285 
286 
288 {
289  return time().rootPath();
290 }
291 
292 
294 {
295  if (instance().isAbsolute())
296  {
297  return instance();
298  }
299  else
300  {
301  return rootPath()/caseName()/instance()/db_.dbDir()/local();
302  }
303 }
304 
305 
307 (
308  const word& instance,
309  const fileName& local
310 ) const
311 {
312  // Note: can only be called with relative instance since is word type
313  return rootPath()/caseName()/instance/db_.dbDir()/local;
314 }
315 
316 
318  const Foam::word & to)
319 {
320  replacedFileNames_.insert(from, to);
321 }
322 
323 
325 {
327  replacedFileNames_.find(name());
328 
329  const word & diskFileName = (findIt == replacedFileNames_.end()) ?
330  name() : *findIt;
331 
332  return diskFileName;
333 }
334 
335 
337 {
338  const word & diskFileName = uniqueFileName();
339 
340  if (instance().isAbsolute())
341  {
342  fileName objectPath = instance()/diskFileName;
343  if (isFile(objectPath))
344  {
345  return objectPath;
346  }
347  else
348  {
349  return fileName::null;
350  }
351  }
352  else
353  {
354  fileName path = this->path();
355  fileName objectPath = path/diskFileName;
356 
357  if (isFile(objectPath))
358  {
359  return objectPath;
360  }
361  else
362  {
363  if
364  (
365  time().processorCase()
366  && (
367  instance() == time().system()
368  || instance() == time().constant()
369  )
370  )
371  {
372  fileName parentObjectPath =
373  rootPath()/time().globalCaseName()
374  /instance()/db_.dbDir()/local()/diskFileName;
375 
376  if (isFile(parentObjectPath))
377  {
378  return parentObjectPath;
379  }
380  }
381 
382  if (!isDir(path))
383  {
384  word newInstancePath = time().findInstancePath
385  (
386  instant(instance())
387  );
388 
389  if (newInstancePath.size())
390  {
391  fileName fName
392  (
393  rootPath()/caseName()
394  /newInstancePath/db_.dbDir()/local()/diskFileName
395  );
396 
397  if (isFile(fName))
398  {
399  return fName;
400  }
401  }
402  }
403  }
404 
405  return fileName::null;
406  }
407 }
408 
409 
411 {
412  return objectStream(filePath());
413 }
414 
415 
417 {
418  if (fName.size())
419  {
420  IFstream* isPtr = new IFstream(fName);
421 
422  if (isPtr->good())
423  {
424  return isPtr;
425  }
426  else
427  {
428  delete isPtr;
429  return NULL;
430  }
431  }
432  else
433  {
434  return NULL;
435  }
436 }
437 
438 
440 {
441  bool ok = true;
442 
443  Istream* isPtr = objectStream();
444 
445  // If the stream has failed return
446  if (!isPtr)
447  {
448  if (objectRegistry::debug)
449  {
450  Info
451  << "IOobject::headerOk() : "
452  << "file " << objectPath() << " could not be opened"
453  << endl;
454  }
455 
456  ok = false;
457  }
458  else
459  {
460  // Try reading header
461  if (!readHeader(*isPtr))
462  {
463  if (objectRegistry::debug)
464  {
465  IOWarningInFunction((*isPtr))
466  << "failed to read header of file " << objectPath()
467  << endl;
468  }
469 
470  ok = false;
471  }
472  }
473 
474  delete isPtr;
475 
476  return ok;
477 }
478 
479 
480 void Foam::IOobject::setBad(const string& s)
481 {
482  if (objState_ != GOOD)
483  {
485  << "recurrent failure for object " << s
486  << exit(FatalError);
487  }
488 
489  if (error::level)
490  {
491  Info<< "IOobject::setBad(const string&) : "
492  << "broken object " << s << info() << endl;
493  }
494 
495  objState_ = BAD;
496 }
497 
498 
500 {
501  name_ = io.name_;
502  headerClassName_ = io.headerClassName_;
503  note_ = io.note_;
504  instance_ = io.instance_;
505  local_ = io.local_;
506  rOpt_ = io.rOpt_;
507  wOpt_ = io.wOpt_;
508  objState_ = io.objState_;
509 }
510 
511 
512 // ************************************************************************* //
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::IOobject::filePath
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:336
Foam::IOobject::setBad
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:480
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::IOobject::objectStream
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:410
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::IOobject::rOpt_
readOption rOpt_
Read option.
Definition: IOobject.H:145
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOobject::registerObject_
bool registerObject_
Register object created from this IOobject with registry if true.
Definition: IOobject.H:151
Foam::StaticHashTable::Iterator< const T &, const StaticHashTable< T, Key, Hash > & >
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::IOobject::instance_
fileName instance_
Instance path component.
Definition: IOobject.H:136
Foam::IOobject::rootPath
const fileName & rootPath() const
Definition: IOobject.C:287
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:117
Foam::messageStream::level
static int level
Definition: messageStream.H:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::note_
string note_
Optional note.
Definition: IOobject.H:133
Foam::IOobject::replaceFileName
static void replaceFileName(const word &from, const word &to)
Definition: IOobject.C:317
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:245
constant
Constant dispersed-phase particle diameter model.
StaticHashTable.H
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:239
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::IOobject::headerOk
bool headerOk()
Read and check header info.
Definition: IOobject.C:439
Foam::IOobject::db_
const objectRegistry & db_
objectRegistry reference
Definition: IOobject.H:142
Foam::StaticHashTable
STL conforming hash table.
Definition: StaticHashTable.H:58
Foam::IOobject::headerClassName_
word headerClassName_
Class name read from header.
Definition: IOobject.H:130
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Info
messageStream Info
Foam::IOobject::caseName
const fileName & caseName() const
Definition: IOobject.C:251
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:115
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
Foam::IOobject::wOpt_
writeOption wOpt_
Write option.
Definition: IOobject.H:148
Foam::IOobject::fileNameComponents
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:43
IFstream.H
IOobject.H
Foam::IOobject::operator=
void operator=(const IOobject &)
Definition: IOobject.C:499
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:97
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::IOobject::~IOobject
virtual ~IOobject()
Destructor.
Definition: IOobject.C:233
Foam::IOobject::IOobject
IOobject(const word &name, const fileName &instance, const objectRegistry &registry, readOption r=NO_READ, writeOption w=NO_WRITE, bool registerObject=true)
Construct from name, instance, registry, io options.
Definition: IOobject.C:116
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobject.C:272
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
replacedFileNames_
static Foam::StaticHashTable< Foam::word > replacedFileNames_
Definition: IOobject.C:38
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::IOobject::name_
word name_
Name.
Definition: IOobject.H:127
Foam::IOobject::group
word group() const
Return group (extension part of name)
Definition: IOobject.C:257
Foam::IOobject::uniqueFileName
const word & uniqueFileName() const
Unique on disk file name.
Definition: IOobject.C:324
Foam::fileName::caseName
string caseName() const
Return file name (part beyond last /), subsitute for FOAM_CASE.
Definition: fileName.C:227
Foam::IOobject::local_
fileName local_
Local path component.
Definition: IOobject.H:139
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:64
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:271
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::IOobject::objState_
objectState objState_
IOobject state.
Definition: IOobject.H:154
Foam::IOobject::path
fileName path() const
Return complete path.
Definition: IOobject.C:293
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::system
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1155
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47