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-2019 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 
40 const Foam::Enum
41 <
43 >
45 ({
46  { fileCheckTypes::timeStamp, "timeStamp" },
47  { fileCheckTypes::timeStampMaster, "timeStampMaster" },
48  { fileCheckTypes::inotify, "inotify" },
49  { fileCheckTypes::inotifyMaster, "inotifyMaster" },
50 });
51 
52 
53 // Default fileCheck type
55 (
56  fileCheckTypesNames.get
57  (
58  "fileModificationChecking",
60  )
61 );
62 
63 
65 namespace Foam
66 {
67  // Register re-reader
68  class addfileModificationCheckingToOpt
69  :
71  {
72  public:
73 
74  addfileModificationCheckingToOpt
75  (const addfileModificationCheckingToOpt&) = delete;
76 
77  void operator=
78  (const addfileModificationCheckingToOpt&) = delete;
79 
80  explicit addfileModificationCheckingToOpt(const char* name)
81  :
82  ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
83  {}
84 
85  virtual ~addfileModificationCheckingToOpt() = default;
86 
87  virtual void readData(Foam::Istream& is)
88  {
91  }
92 
93  virtual void writeData(Foam::Ostream& os) const
94  {
97  }
98  };
99 
100  addfileModificationCheckingToOpt addfileModificationCheckingToOpt_
101  (
102  "fileModificationChecking"
103  );
104 
105 } // End namespace Foam
107 
108 
109 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
110 
112 (
113  const fileName& path,
114  fileName& instance,
115  fileName& local,
116  word& name
117 )
118 {
119  // Convert explicit relative file-system path to absolute file-system path.
120  if (path.starts_with("./") || path.starts_with("../"))
121  {
122  fileName absPath = cwd()/path;
123  absPath.clean();
124 
125  return fileNameComponents(absPath, instance, local, name);
126  }
127 
128  instance.clear();
129  local.clear();
130  name.clear();
131 
132  // Called with directory
133  if (isDir(path))
134  {
136  << " called with directory: " << path << endl;
137 
138  return false;
139  }
140 
141  const auto first = path.find('/');
142  const auto last = path.rfind('/');
143 
144  // The raw length of name (without validating for word chars)
145  auto nameLen = path.size();
146 
147  if (first == std::string::npos)
148  {
149  // No '/' found (or empty entirely)
150  // => no instance or local
151 
153  }
154  else if
155  (
156  first == 0
157  #ifdef _WIN32
158  || (first == 2 && path[1] == ':') // Eg, d:/path
159  #endif
160  )
161  {
162  // Absolute path (starts with '/' or 'd:/')
163  // => no local
164 
165  instance = path.substr(0, last);
166 
167  const std::string ending = path.substr(last+1);
168  nameLen = ending.size(); // The raw length of name
169  name = word::validate(ending);
170  }
171  else
172  {
173  // Normal case.
174  // First part is instance, remainder is local
175  instance = path.substr(0, first);
176 
177  if (last > first)
178  {
179  // With local
180  local = path.substr(first+1, last-first-1);
181  }
182 
183  const std::string ending = path.substr(last+1);
184  nameLen = ending.size(); // The raw length of name
185  name = word::validate(ending);
186  }
187 
188  // Check for valid (and stripped) name, regardless of the debug level
189  if (!nameLen || nameLen != name.size())
190  {
192  << "has invalid word for name: \"" << name
193  << "\"\nwhile processing path: " << path << endl;
194 
195  return false;
196  }
197 
198  return true;
199 }
200 
201 
203 (
204  const IOobject& io,
205  const fileName& altFile,
206  const word& ioName
207 )
208 {
209  if (altFile.empty())
210  {
211  return io;
212  }
213 
214  // Construct from file path instead
215 
216  fileName altPath = altFile;
217 
218  if (isDir(altPath))
219  {
220  // Resolve directories as well
221 
222  if (ioName.empty())
223  {
224  altPath /= io.name();
225  }
226  else
227  {
228  altPath /= ioName;
229  }
230  }
231  altPath.expand();
232 
233 
234  return
235  IOobject
236  (
237  altPath,
238  io.db(),
239  io.readOpt(),
240  io.writeOpt(),
241  io.registerObject(),
242  io.globalObject()
243  );
244 }
245 
246 
248 {
249  const auto i = name.rfind('.');
250 
251  if (i == std::string::npos || i == 0)
252  {
253  return word::null;
254  }
255 
256  return name.substr(i+1);
257 }
258 
259 
261 {
262  const auto i = name.rfind('.');
263 
264  if (i == std::string::npos || i == 0)
265  {
266  return name;
267  }
268 
269  return name.substr(0, i);
270 }
271 
272 
273 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
274 
276 (
277  const word& name,
278  const fileName& instance,
279  const objectRegistry& registry,
280  readOption ro,
281  writeOption wo,
282  bool registerObject
283 )
284 :
285  name_(name),
286  headerClassName_(typeName),
287  note_(),
288  instance_(instance),
289  local_(),
290  db_(registry),
291  rOpt_(ro),
292  wOpt_(wo),
293  registerObject_(registerObject),
294  globalObject_(false),
295  objState_(GOOD),
296  labelByteSize_(sizeof(label)),
297  scalarByteSize_(sizeof(scalar))
298 {
300  {
302  << "Constructing IOobject called " << name_
303  << " of type " << headerClassName_
304  << endl;
305  }
306 }
307 
308 
310 (
311  const word& name,
312  const fileName& instance,
313  const fileName& local,
314  const objectRegistry& registry,
315  readOption ro,
316  writeOption wo,
317  bool registerObject,
318  bool globalObject
319 )
320 :
321  name_(name),
322  headerClassName_(typeName),
323  note_(),
324  instance_(instance),
325  local_(local),
326  db_(registry),
327  rOpt_(ro),
328  wOpt_(wo),
329  registerObject_(registerObject),
330  globalObject_(globalObject),
331  objState_(GOOD),
332  labelByteSize_(sizeof(label)),
333  scalarByteSize_(sizeof(scalar))
334 {
336  {
338  << "Constructing IOobject called " << name_
339  << " of type " << headerClassName_
340  << endl;
341  }
342 }
343 
344 
346 (
347  const fileName& path,
348  const objectRegistry& registry,
349  readOption ro,
350  writeOption wo,
351  bool registerObject,
352  bool globalObject
353 )
354 :
355  name_(),
356  headerClassName_(typeName),
357  note_(),
358  instance_(),
359  local_(),
360  db_(registry),
361  rOpt_(ro),
362  wOpt_(wo),
363  registerObject_(registerObject),
364  globalObject_(globalObject),
365  objState_(GOOD),
366  labelByteSize_(sizeof(label)),
367  scalarByteSize_(sizeof(scalar))
368 {
369  if (!fileNameComponents(path, instance_, local_, name_))
370  {
372  << " invalid path specification"
373  << exit(FatalError);
374  }
375 
377  {
379  << "Constructing IOobject called " << name_
380  << " of type " << headerClassName_
381  << endl;
382  }
383 }
384 
385 
387 (
388  const IOobject& io,
389  const objectRegistry& registry
390 )
391 :
392  name_(io.name_),
393  headerClassName_(io.headerClassName_),
394  note_(io.note_),
395  instance_(io.instance_),
396  local_(io.local_),
397  db_(registry),
398  rOpt_(io.rOpt_),
399  wOpt_(io.wOpt_),
400  registerObject_(io.registerObject_),
401  globalObject_(io.globalObject_),
402  objState_(io.objState_),
403  labelByteSize_(io.labelByteSize_),
404  scalarByteSize_(io.scalarByteSize_)
405 {}
406 
407 
409 (
410  const IOobject& io,
411  const word& name
412 )
413 :
414  name_(name),
415  headerClassName_(io.headerClassName_),
416  note_(io.note_),
417  instance_(io.instance_),
418  local_(io.local_),
419  db_(io.db_),
420  rOpt_(io.rOpt_),
421  wOpt_(io.wOpt_),
422  registerObject_(io.registerObject_),
423  globalObject_(io.globalObject_),
424  objState_(io.objState_),
425  labelByteSize_(io.labelByteSize_),
426  scalarByteSize_(io.scalarByteSize_)
427 {}
428 
429 
430 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
431 
433 {
434  return db_;
435 }
436 
437 
439 {
440  return db_.time();
441 }
442 
443 
445 {
446  return time().rootPath();
447 }
448 
449 
451 {
452  return time().caseName();
453 }
454 
455 
457 {
458  // A file is 'outside' of the case if it has been specified using an
459  // absolute path (starts with '/')
460 
461  if (instance().starts_with('/'))
462  {
463  return instance();
464  }
465 
466  return rootPath()/caseName()/instance()/db_.dbDir()/local();
467 }
468 
469 
471 (
472  const word& instance,
473  const fileName& local
474 ) const
475 {
476  // Note: can only be called with relative instance since is word type
477  return rootPath()/caseName()/instance/db_.dbDir()/local;
478 }
479 
480 
482 (
483  const word& typeName,
484  const bool search
485 ) const
486 {
487  // Do not check for undecomposed files
488  return fileHandler().filePath(false, *this, typeName, search);
489 }
490 
491 
493 (
494  const word& typeName,
495  const bool search
496 ) const
497 {
498  // Check for undecomposed files
499  return fileHandler().filePath(true, *this, typeName, search);
500 }
501 
502 
503 void Foam::IOobject::setBad(const string& s)
504 {
505  if (objState_ != GOOD)
506  {
508  << "Recurrent failure for object " << s
509  << exit(FatalError);
510  }
511 
512  if (error::level)
513  {
515  << "Broken object " << s << info() << endl;
516  }
517 
518  objState_ = BAD;
519 }
520 
521 
522 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
523 
525 {
526  name_ = io.name_;
527  headerClassName_ = io.headerClassName_;
528  note_ = io.note_;
529  instance_ = io.instance_;
530  local_ = io.local_;
531  rOpt_ = io.rOpt_;
532  wOpt_ = io.wOpt_;
533  globalObject_ = io.globalObject_;
534  objState_ = io.objState_;
535  labelByteSize_ = io.labelByteSize_;
536  scalarByteSize_ = io.scalarByteSize_;
537 }
538 
539 
540 // ************************************************************************* //
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:316
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:51
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:46
Foam::IOobject::localFilePath
fileName localFilePath(const word &typeName, const bool search=true) const
Helper for filePath that searches locally.
Definition: IOobject.C:482
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:444
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
Controls the output verbosity of messageStream.
Definition: messageStream.H:108
Foam::IOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:207
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::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1181
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:438
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:432
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
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:88
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IOobject::writeOpt
writeOption writeOpt() const
The write option.
Definition: IOobjectI.H:153
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:450
Foam::IOobject::globalObject
bool globalObject() const
Is object same for all processors?
Definition: IOobjectI.H:100
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:203
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:112
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:493
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:524
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::IOobject::IOobject
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:276
Foam::IOobject::member
word member() const
Return member (name without the extension)
Definition: IOobjectI.H:58
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:141
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:159
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:52
Foam::IOobject::setBad
void setBad(const string &s)
Set the object state to bad.
Definition: IOobject.C:503
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:456
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:120
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
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