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-2022 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 {
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().name()
369  )
370  {
371  scalar timeValue;
372  if (readScalar(instance_.c_str(), timeValue))
373  {
374  instance_ = time().name();
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 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const fileName & local() const
Definition: IOobject.H:400
const fileName & caseName(const bool global) const
Definition: IOobject.C:342
fileName filePath(const word &typeName, const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:407
const Time & time() const
Return time.
Definition: IOobject.C:318
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:416
fileName relativePath() const
Return the path relative to the case directory.
Definition: IOobject.C:393
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:355
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
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
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:64
void updateInstance() const
If the instance is a time directory update to the current time.
Definition: IOobject.C:361
word group() const
Return group (extension part of name)
Definition: IOobject.C:324
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:226
void operator=(const IOobject &)
Definition: IOobject.C:435
word member() const
Return member (name without the extension)
Definition: IOobject.C:330
const word & name() const
Return name.
Definition: IOobject.H:310
const fileName & rootPath() const
Definition: IOobject.C:336
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:140
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:133
writeOption
Enumeration defining the write options.
Definition: IOobject.H:126
virtual ~IOobject()
Destructor.
Definition: IOobject.C:306
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:380
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:54
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A class for handling file names.
Definition: fileName.H:82
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:73
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:210
virtual fileName filePath(const bool globalFile, const IOobject &, const word &typeName) const =0
Search for an object. globalFile : also check undecomposed case.
Registry of regIOobjects.
const Time & time() const
Return time.
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
const char *const group
Group name for atomic constants.
Enum namedEnumOptimisationSwitch(const char *name, const NamedEnum< Enum, nEnum > &enumNames, const Enum defaultValue)
Lookup optimisation switch or add default value.
Definition: debug.H:94
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
defineTypeNameAndDebug(combustionModel, 0)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
error FatalError