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