IOobject.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 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 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(IOobject, 0);
36 
37  template<>
39  {
40  "timeStamp",
41  "timeStampMaster",
42  "inotify",
43  "inotifyMaster"
44  };
45 }
46 
49 
50 // Default fileCheck type
52 (
54  (
55  "fileModificationChecking",
56  fileCheckTypesNames,
57  fileModificationChecking
58  )
59 );
60 
61 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
62 
64 (
65  const fileName& path,
66  fileName& instance,
67  fileName& local,
68  word& name
69 )
70 {
71  instance.clear();
72  local.clear();
73  name.clear();
74 
75  // called with directory
76  if (isDir(path))
77  {
79  << " called with directory: " << path << endl;
80 
81  return false;
82  }
83 
84  if (path.isAbsolute())
85  {
86  string::size_type last = path.rfind('/');
87  instance = path.substr(0, last);
88 
89  // Check afterwards
90  name.string::operator=(path.substr(last+1));
91  }
92  else
93  {
94  string::size_type first = path.find('/');
95 
96  if (first == string::npos)
97  {
98  // no '/' found - no instance or local
99 
100  // check afterwards
101  name.string::operator=(path);
102  }
103  else
104  {
105  instance = path.substr(0, first);
106 
107  string::size_type last = path.rfind('/');
108  if (last > first)
109  {
110  // with local
111  local = path.substr(first+1, last-first-1);
112  }
113 
114  // check afterwards
115  name.string::operator=(path.substr(last+1));
116  }
117  }
118 
119 
120  // Check for valid (and stripped) name, regardless of the debug level
121  if (name.empty() || string::stripInvalid<word>(name))
122  {
124  << "has invalid word for name: \"" << name
125  << "\"\nwhile processing path: " << path << endl;
126 
127  return false;
128  }
129 
130  return true;
131 }
132 
133 
135 {
136  word::size_type i = name.find_last_of('.');
137 
138  if (i == word::npos || i == 0)
139  {
140  return word::null;
141  }
142  else
143  {
144  return name.substr(i+1, word::npos);
145  }
146 }
147 
148 
150 {
151  word::size_type i = name.find_last_of('.');
152 
153  if (i == word::npos || i == 0)
154  {
155  return name;
156  }
157  else
158  {
159  return name.substr(0, i);
160  }
161 }
162 
163 
164 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
165 
167 (
168  const word& name,
169  const fileName& instance,
170  const objectRegistry& registry,
171  readOption ro,
172  writeOption wo,
173  bool registerObject
174 )
175 :
176  name_(name),
177  headerClassName_(typeName),
178  note_(),
179  instance_(instance),
180  local_(),
181  db_(registry),
182  rOpt_(ro),
183  wOpt_(wo),
184  registerObject_(registerObject),
185  objState_(GOOD)
186 {
187  if (objectRegistry::debug)
188  {
190  << "Constructing IOobject called " << name_
191  << " of type " << headerClassName_
192  << endl;
193  }
194 }
195 
196 
198 (
199  const word& name,
200  const fileName& instance,
201  const fileName& local,
202  const objectRegistry& registry,
203  readOption ro,
204  writeOption wo,
205  bool registerObject
206 )
207 :
208  name_(name),
209  headerClassName_(typeName),
210  note_(),
211  instance_(instance),
212  local_(local),
213  db_(registry),
214  rOpt_(ro),
215  wOpt_(wo),
216  registerObject_(registerObject),
217  objState_(GOOD)
218 {
219  if (objectRegistry::debug)
220  {
222  << "Constructing IOobject called " << name_
223  << " of type " << headerClassName_
224  << endl;
225  }
226 }
227 
228 
230 (
231  const fileName& path,
232  const objectRegistry& registry,
233  readOption ro,
234  writeOption wo,
235  bool registerObject
236 )
237 :
238  name_(),
239  headerClassName_(typeName),
240  note_(),
241  instance_(),
242  local_(),
243  db_(registry),
244  rOpt_(ro),
245  wOpt_(wo),
246  registerObject_(registerObject),
247  objState_(GOOD)
248 {
249  if (!fileNameComponents(path, instance_, local_, name_))
250  {
252  << " invalid path specification"
253  << exit(FatalError);
254  }
255 
256  if (objectRegistry::debug)
257  {
259  << "Constructing IOobject called " << name_
260  << " of type " << headerClassName_
261  << endl;
262  }
263 }
264 
265 
267 (
268  const IOobject& io,
269  const objectRegistry& registry
270 )
271 :
272  name_(io.name_),
273  headerClassName_(io.headerClassName_),
274  note_(io.note_),
275  instance_(io.instance_),
276  local_(io.local_),
277  db_(registry),
278  rOpt_(io.rOpt_),
279  wOpt_(io.wOpt_),
280  registerObject_(io.registerObject_),
281  objState_(io.objState_)
282 {}
283 
284 
286 (
287  const IOobject& io,
288  const word& name
289 )
290 :
291  name_(name),
292  headerClassName_(io.headerClassName_),
293  note_(io.note_),
294  instance_(io.instance_),
295  local_(io.local_),
296  db_(io.db_),
297  rOpt_(io.rOpt_),
298  wOpt_(io.wOpt_),
299  registerObject_(io.registerObject_),
300  objState_(io.objState_)
301 {}
302 
303 
304 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
305 
307 {}
308 
309 
310 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
311 
313 {
314  return db_;
315 }
316 
317 
319 {
320  return db_.time();
321 }
322 
323 
325 {
326  return group(name_);
327 }
328 
329 
331 {
332  return member(name_);
333 }
334 
335 
337 {
338  return time().rootPath();
339 }
340 
341 
342 const Foam::fileName& Foam::IOobject::caseName(const bool global) const
343 {
344  if (global)
345  {
346  return time().globalCaseName();
347  }
348  else
349  {
350  return time().caseName();
351  }
352 }
353 
354 
356 {
357  return instance_;
358 }
359 
360 
362 {
363  if
364  (
365  !instance_.isAbsolute()
366  && instance_ != time().system()
367  && instance_ != time().constant()
368  && instance_ != time().timeName()
369  )
370  {
371  scalar timeValue;
372  if (readScalar(instance_.c_str(), timeValue))
373  {
374  instance_ = time().timeName();
375  }
376  }
377 }
378 
379 
380 Foam::fileName Foam::IOobject::path(const bool global) const
381 {
382  if (instance_.isAbsolute())
383  {
384  return instance_;
385  }
386  else
387  {
388  return rootPath()/caseName(global)/instance()/db_.dbDir()/local();
389  }
390 }
391 
392 
394 {
395  if (instance().isAbsolute())
396  {
397  return instance();
398  }
399  else
400  {
401  return instance()/db_.dbDir()/local();
402  }
403 }
404 
405 
407 (
408  const word& typeName,
409  const bool global
410 ) const
411 {
412  return fileHandler().filePath(global, *this, typeName);
413 }
414 
415 
416 void Foam::IOobject::setBad(const string& s)
417 {
418  if (objState_ != GOOD)
419  {
421  << "Recurrent failure for object " << s
422  << exit(FatalError);
423  }
424 
425  if (error::level)
426  {
428  << "Broken object " << s << info() << endl;
429  }
430 
431  objState_ = BAD;
432 }
433 
434 
436 {
437  name_ = io.name_;
438  headerClassName_ = io.headerClassName_;
439  note_ = io.note_;
440  instance_ = io.instance_;
441  local_ = io.local_;
442  rOpt_ = io.rOpt_;
443  wOpt_ = io.wOpt_;
444  objState_ = io.objState_;
445 }
446 
447 
448 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:132
const char *const group
Group name for atomic constants.
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:38
writeOption
Enumeration defining the write options.
Definition: IOobject.H:125
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:64
A class for handling file names.
Definition: fileName.H:79
void updateInstance() const
If the instance is a time directory update to the current time.
Definition: IOobject.C:361
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void operator=(const IOobject &)
Definition: IOobject.C:435
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:210
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:167
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const fileName & caseName(const bool global) const
Definition: IOobject.C:342
word member() const
Return member (name without the extension)
Definition: IOobject.C:330
virtual ~IOobject()
Destructor.
Definition: IOobject.C:306
word group() const
Return group (extension part of name)
Definition: IOobject.C:324
fileName filePath(const word &typeName, const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:407
readOption
Enumeration defining the read options.
Definition: IOobject.H:116
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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))
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:73
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
A class for handling words, derived from string.
Definition: word.H:59
virtual fileName filePath(const bool globalFile, const IOobject &, const word &typeName) const =0
Search for an object. globalFile : also check undecomposed case.
fileName relativePath() const
Return the path relative to the case directory.
Definition: IOobject.C:393
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:416
static const word null
An empty word.
Definition: word.H:77
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:380
const fileOperation & fileHandler()
Get current file handler.
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
const Time & time() const
Return time.
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:231
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const fileName & rootPath() const
Definition: IOobject.C:336
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:140
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:318
Enum namedEnumOptimisationSwitch(const char *name, const NamedEnum< Enum, nEnum > &enumNames, const Enum defaultValue)
Lookup optimisation switch or add default value.
Definition: debug.H:94
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.