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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IOobject.H"
30 #include "Time.H"
31 #include "IFstream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(IOobject, 0);
38 }
39 
41 (
42  #ifdef _WIN32
43  // Windows: using ':' causes scoping conflicts with d:/path etc
44  Foam::debug::infoSwitch("scopeSeparator", '_')
45  #else
46  Foam::debug::infoSwitch("scopeSeparator", ':')
47  #endif
48 );
49 
50 
51 const Foam::Enum
52 <
54 >
56 ({
57  { fileCheckTypes::timeStamp, "timeStamp" },
58  { fileCheckTypes::timeStampMaster, "timeStampMaster" },
59  { fileCheckTypes::inotify, "inotify" },
60  { fileCheckTypes::inotifyMaster, "inotifyMaster" },
61 });
62 
63 
64 // Default fileCheck type
66 (
67  fileCheckTypesNames.get
68  (
69  "fileModificationChecking",
71  )
72 );
73 
74 
76 namespace Foam
77 {
78  // Register re-reader
79  class addfileModificationCheckingToOpt
80  :
82  {
83  public:
84 
85  addfileModificationCheckingToOpt
86  (const addfileModificationCheckingToOpt&) = delete;
87 
88  void operator=
89  (const addfileModificationCheckingToOpt&) = delete;
90 
91  explicit addfileModificationCheckingToOpt(const char* name)
92  :
93  ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
94  {}
95 
96  virtual ~addfileModificationCheckingToOpt() = default;
97 
98  virtual void readData(Foam::Istream& is)
99  {
102  }
103 
104  virtual void writeData(Foam::Ostream& os) const
105  {
108  }
109  };
110 
111  addfileModificationCheckingToOpt addfileModificationCheckingToOpt_
112  (
113  "fileModificationChecking"
114  );
115 
116 } // End namespace Foam
118 
119 
120 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
121 
123 (
124  const fileName& path,
125  fileName& instance,
126  fileName& local,
127  word& name
128 )
129 {
130  // Convert explicit relative file-system path to absolute file-system path.
131  if (path.starts_with("./") || path.starts_with("../"))
132  {
133  fileName absPath = cwd()/path;
134  absPath.clean();
135 
136  return fileNameComponents(absPath, instance, local, name);
137  }
138 
139  instance.clear();
140  local.clear();
141  name.clear();
142 
143  // Called with directory
144  if (isDir(path))
145  {
147  << " called with directory: " << path << endl;
148 
149  return false;
150  }
151 
152  const auto first = path.find('/');
153  const auto last = path.rfind('/');
154 
155  // The raw length of name (without validating for word chars)
156  auto nameLen = path.size();
157 
158  if (first == std::string::npos)
159  {
160  // No '/' found (or empty entirely)
161  // => no instance or local
162 
164  }
165  else if
166  (
167  first == 0
168  #ifdef _WIN32
169  || (first == 2 && path[1] == ':') // Eg, d:/path
170  #endif
171  )
172  {
173  // Absolute path (starts with '/' or 'd:/')
174  // => no local
175 
176  instance = path.substr(0, last);
177 
178  const std::string ending = path.substr(last+1);
179  nameLen = ending.size(); // The raw length of name
180  name = word::validate(ending);
181  }
182  else
183  {
184  // Normal case.
185  // First part is instance, remainder is local
186  instance = path.substr(0, first);
187 
188  if (last > first)
189  {
190  // With local
191  local = path.substr(first+1, last-first-1);
192  }
193 
194  const std::string ending = path.substr(last+1);
195  nameLen = ending.size(); // The raw length of name
196  name = word::validate(ending);
197  }
198 
199  // Check for valid (and stripped) name, regardless of the debug level
200  if (!nameLen || nameLen != name.size())
201  {
203  << "has invalid word for name: \"" << name
204  << "\"\nwhile processing path: " << path << endl;
205 
206  return false;
207  }
208 
209  return true;
210 }
211 
212 
214 (
215  const IOobject& io,
216  const fileName& altFile,
217  const word& ioName
218 )
219 {
220  if (altFile.empty())
221  {
222  return io;
223  }
224 
225  // Construct from file path instead
226 
227  fileName altPath = altFile;
228 
229  if (isDir(altPath))
230  {
231  // Resolve directories as well
232 
233  if (ioName.empty())
234  {
235  altPath /= io.name();
236  }
237  else
238  {
239  altPath /= ioName;
240  }
241  }
242  altPath.expand();
243 
244 
245  return
246  IOobject
247  (
248  altPath,
249  io.db(),
250  io.readOpt(),
251  io.writeOpt(),
252  io.registerObject(),
253  io.globalObject()
254  );
255 }
256 
257 
259 {
260  const auto i = name.rfind('.');
261 
262  if (i == std::string::npos || i == 0)
263  {
264  return word::null;
265  }
266 
267  return name.substr(i+1);
268 }
269 
270 
272 {
273  const auto i = name.rfind('.');
274 
275  if (i == std::string::npos || i == 0)
276  {
277  return name;
278  }
279 
280  return name.substr(0, i);
281 }
282 
283 
284 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
285 
287 (
288  const word& name,
289  const fileName& instance,
290  const objectRegistry& registry,
291  readOption ro,
292  writeOption wo,
293  bool registerObject
294 )
295 :
296  name_(name),
297  headerClassName_(typeName),
298  note_(),
299  instance_(instance),
300  local_(),
301  db_(registry),
302  rOpt_(ro),
303  wOpt_(wo),
304  registerObject_(registerObject),
305  globalObject_(false),
306  objState_(GOOD),
307  labelByteSize_(sizeof(label)),
308  scalarByteSize_(sizeof(scalar))
309 {
311  {
313  << "Constructing IOobject called " << name_
314  << " of type " << headerClassName_
315  << endl;
316  }
317 }
318 
319 
321 (
322  const word& name,
323  const fileName& instance,
324  const fileName& local,
325  const objectRegistry& registry,
326  readOption ro,
327  writeOption wo,
328  bool registerObject,
329  bool globalObject
330 )
331 :
332  name_(name),
333  headerClassName_(typeName),
334  note_(),
335  instance_(instance),
336  local_(local),
337  db_(registry),
338  rOpt_(ro),
339  wOpt_(wo),
340  registerObject_(registerObject),
341  globalObject_(globalObject),
342  objState_(GOOD),
343  labelByteSize_(sizeof(label)),
344  scalarByteSize_(sizeof(scalar))
345 {
347  {
349  << "Constructing IOobject called " << name_
350  << " of type " << headerClassName_
351  << endl;
352  }
353 }
354 
355 
357 (
358  const fileName& path,
359  const objectRegistry& registry,
360  readOption ro,
361  writeOption wo,
362  bool registerObject,
363  bool globalObject
364 )
365 :
366  name_(),
367  headerClassName_(typeName),
368  note_(),
369  instance_(),
370  local_(),
371  db_(registry),
372  rOpt_(ro),
373  wOpt_(wo),
374  registerObject_(registerObject),
375  globalObject_(globalObject),
376  objState_(GOOD),
377  labelByteSize_(sizeof(label)),
378  scalarByteSize_(sizeof(scalar))
379 {
380  if (!fileNameComponents(path, instance_, local_, name_))
381  {
383  << " invalid path specification"
384  << exit(FatalError);
385  }
386 
388  {
390  << "Constructing IOobject called " << name_
391  << " of type " << headerClassName_
392  << endl;
393  }
394 }
395 
396 
398 (
399  const IOobject& io,
400  const objectRegistry& registry
401 )
402 :
403  name_(io.name_),
404  headerClassName_(io.headerClassName_),
405  note_(io.note_),
406  instance_(io.instance_),
407  local_(io.local_),
408  db_(registry),
409  rOpt_(io.rOpt_),
410  wOpt_(io.wOpt_),
411  registerObject_(io.registerObject_),
412  globalObject_(io.globalObject_),
413  objState_(io.objState_),
414  labelByteSize_(io.labelByteSize_),
415  scalarByteSize_(io.scalarByteSize_)
416 {}
417 
418 
420 (
421  const IOobject& io,
422  const word& name
423 )
424 :
425  name_(name),
426  headerClassName_(io.headerClassName_),
427  note_(io.note_),
428  instance_(io.instance_),
429  local_(io.local_),
430  db_(io.db_),
431  rOpt_(io.rOpt_),
432  wOpt_(io.wOpt_),
433  registerObject_(io.registerObject_),
434  globalObject_(io.globalObject_),
435  objState_(io.objState_),
436  labelByteSize_(io.labelByteSize_),
437  scalarByteSize_(io.scalarByteSize_)
438 {}
439 
440 
442 (
443  const IOobject& io,
444  readOption ro,
445  writeOption wo
446 )
447 :
448  IOobject(io)
449 {
450  rOpt_ = ro;
451  wOpt_ = wo;
452 }
453 
454 
455 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
456 
458 {
459  return db_;
460 }
461 
462 
464 {
465  return db_.time();
466 }
467 
468 
470 {
471  return time().rootPath();
472 }
473 
474 
476 {
477  return time().caseName();
478 }
479 
480 
482 {
483  // A file is 'outside' of the case if it has been specified using an
484  // absolute path
485 
486  const auto first = instance().find('/');
487 
488  if
489  (
490  first == 0
491  #ifdef _WIN32
492  || (first == 2 && instance()[1] == ':') // Eg, d:/path
493  #endif
494  )
495  {
496  // Absolute path (starts with '/' or 'd:/')
497  return instance();
498  }
499 
500  return rootPath()/caseName()/instance()/db_.dbDir()/local();
501 }
502 
503 
505 (
506  const word& instance,
507  const fileName& local
508 ) const
509 {
510  // Note: can only be called with relative instance since is word type
511  return rootPath()/caseName()/instance/db_.dbDir()/local;
512 }
513 
514 
516 (
517  const word& typeName,
518  const bool search
519 ) const
520 {
521  // Do not check for undecomposed files
522  return fileHandler().filePath(false, *this, typeName, search);
523 }
524 
525 
527 (
528  const word& typeName,
529  const bool search
530 ) const
531 {
532  // Check for undecomposed files
533  return fileHandler().filePath(true, *this, typeName, search);
534 }
535 
536 
537 void Foam::IOobject::setBad(const string& s)
538 {
539  if (objState_ != GOOD)
540  {
542  << "Recurrent failure for object " << s
543  << exit(FatalError);
544  }
545 
546  if (error::level)
547  {
549  << "Broken object " << s << info() << endl;
550  }
551 
552  objState_ = BAD;
553 }
554 
555 
556 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
557 
559 {
560  name_ = io.name_;
561  headerClassName_ = io.headerClassName_;
562  note_ = io.note_;
563  instance_ = io.instance_;
564  local_ = io.local_;
565  rOpt_ = io.rOpt_;
566  wOpt_ = io.wOpt_;
567  globalObject_ = io.globalObject_;
568  objState_ = io.objState_;
569  labelByteSize_ = io.labelByteSize_;
570  scalarByteSize_ = io.scalarByteSize_;
571 }
572 
573 
574 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
Foam::IOobject::fileCheckTypes
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:134
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::IOobject::localFilePath
fileName localFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches locally.
Definition: IOobject.C:516
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOobject::fileCheckTypesNames
static const Enum< fileCheckTypes > fileCheckTypesNames
Names for the fileCheckTypes.
Definition: IOobject.H:143
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
Definition: gmvOutputSpray.H:25
Foam::IOobject::rootPath
const fileName & rootPath() const
Definition: IOobject.C:469
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
validate
thermo validate(args.executable(), "h")
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:107
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:211
Foam::fileOperation::filePath
virtual fileName filePath(const bool checkGlobal, const IOobject &, const word &typeName, const bool search=true) const =0
Search for an object. checkGlobal : also check undecomposed case.
Foam::IOobject::scopeSeparator
static char scopeSeparator
Character for scoping object names (':' or '_')
Definition: IOobject.H:208
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1354
Foam::IOobject::IOobject
IOobject(const IOobject &)=default
Copy construct.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:463
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:457
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:127
Foam::IOobject::registerObject
bool registerObject() const
Should object created with this IOobject be registered?
Definition: IOobjectI.H:112
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:177
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOobject::caseName
const fileName & caseName() const
Definition: IOobject.C:475
Foam::IOobject::globalObject
bool globalObject() const
Is object same for all processors?
Definition: IOobjectI.H:124
Foam::IOobject::selectIO
static IOobject selectIO(const IOobject &io, const fileName &altFile, const word &ioName="")
Return the IOobject, but also consider an alternative file name.
Definition: IOobject.C:214
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:123
IFstream.H
Foam::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:39
IOobject.H
Foam::IOobject::globalFilePath
fileName globalFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:527
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::operator=
void operator=(const IOobject &io)
Definition: IOobject.C:558
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobjectI.H:82
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:165
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:186
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::search
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:576
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::IOobject::group
word group() const
Return group (extension part of name)
Definition: IOobjectI.H:76
Foam::IOobject::setBad
void setBad(const string &s)
Set the object state to bad.
Definition: IOobject.C:537
Foam::fileName::clean
static bool clean(std::string &str)
Cleanup filename.
Definition: fileName.C:298
Foam::cwd
fileName cwd()
The physical or logical current working directory path name.
Definition: MSwindows.C:468
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOobject::path
fileName path() const
The complete path.
Definition: IOobject.C:481
Foam::debug::optimisationSwitches
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict(s).
Definition: debug.C:219
Foam::Enum::read
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: Enum.C:109
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:262
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:52
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643