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