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-2018 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  template<>
37  const char* NamedEnum
38  <
40  4
41  >::names[] =
42  {
43  "timeStamp",
44  "timeStampMaster",
45  "inotify",
46  "inotifyMaster"
47  };
48 }
49 
50 
53 
54 // Default fileCheck type
56 (
57  fileCheckTypesNames.read
58  (
60  (
61  "fileModificationChecking"
62  )
63  )
64 );
65 
66 namespace Foam
67 {
68  // Register re-reader
70  :
72  {
73  public:
74 
76  :
78  {}
79 
81  {}
82 
83  virtual void readData(Foam::Istream& is)
84  {
87  }
88 
89  virtual void writeData(Foam::Ostream& os) const
90  {
93  }
94  };
95 
97  (
98  "fileModificationChecking"
99  );
100 }
101 
102 
103 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
104 
106 (
107  const fileName& path,
108  fileName& instance,
109  fileName& local,
110  word& name
111 )
112 {
113  instance.clear();
114  local.clear();
115  name.clear();
116 
117  // called with directory
118  if (isDir(path))
119  {
121  << " called with directory: " << path << endl;
122 
123  return false;
124  }
125 
126  if (path.isAbsolute())
127  {
128  string::size_type last = path.rfind('/');
129  instance = path.substr(0, last);
130 
131  // Check afterwards
132  name.string::operator=(path.substr(last+1));
133  }
134  else
135  {
136  string::size_type first = path.find('/');
137 
138  if (first == string::npos)
139  {
140  // no '/' found - no instance or local
141 
142  // check afterwards
143  name.string::operator=(path);
144  }
145  else
146  {
147  instance = path.substr(0, first);
148 
149  string::size_type last = path.rfind('/');
150  if (last > first)
151  {
152  // with local
153  local = path.substr(first+1, last-first-1);
154  }
155 
156  // check afterwards
157  name.string::operator=(path.substr(last+1));
158  }
159  }
160 
161 
162  // Check for valid (and stripped) name, regardless of the debug level
163  if (name.empty() || string::stripInvalid<word>(name))
164  {
166  << "has invalid word for name: \"" << name
167  << "\"\nwhile processing path: " << path << endl;
168 
169  return false;
170  }
171 
172  return true;
173 }
174 
175 
177 {
178  word::size_type i = name.find_last_of('.');
179 
180  if (i == word::npos || i == 0)
181  {
182  return word::null;
183  }
184  else
185  {
186  return name.substr(i+1, word::npos);
187  }
188 }
189 
190 
192 {
193  word::size_type i = name.find_last_of('.');
194 
195  if (i == word::npos || i == 0)
196  {
197  return name;
198  }
199  else
200  {
201  return name.substr(0, i);
202  }
203 }
204 
205 
206 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
207 
209 (
210  const word& name,
211  const fileName& instance,
212  const objectRegistry& registry,
213  readOption ro,
214  writeOption wo,
215  bool registerObject
216 )
217 :
218  name_(name),
219  headerClassName_(typeName),
220  note_(),
221  instance_(instance),
222  local_(),
223  db_(registry),
224  rOpt_(ro),
225  wOpt_(wo),
226  registerObject_(registerObject),
227  globalObject_(false),
228  objState_(GOOD)
229 {
230  if (objectRegistry::debug)
231  {
233  << "Constructing IOobject called " << name_
234  << " of type " << headerClassName_
235  << endl;
236  }
237 }
238 
239 
241 (
242  const word& name,
243  const fileName& instance,
244  const fileName& local,
245  const objectRegistry& registry,
246  readOption ro,
247  writeOption wo,
248  bool registerObject,
249  bool globalObject
250 )
251 :
252  name_(name),
253  headerClassName_(typeName),
254  note_(),
255  instance_(instance),
256  local_(local),
257  db_(registry),
258  rOpt_(ro),
259  wOpt_(wo),
260  registerObject_(registerObject),
261  globalObject_(globalObject),
262  objState_(GOOD)
263 {
264  if (objectRegistry::debug)
265  {
267  << "Constructing IOobject called " << name_
268  << " of type " << headerClassName_
269  << endl;
270  }
271 }
272 
273 
275 (
276  const fileName& path,
277  const objectRegistry& registry,
278  readOption ro,
279  writeOption wo,
280  bool registerObject,
281  bool globalObject
282 )
283 :
284  name_(),
285  headerClassName_(typeName),
286  note_(),
287  instance_(),
288  local_(),
289  db_(registry),
290  rOpt_(ro),
291  wOpt_(wo),
292  registerObject_(registerObject),
293  globalObject_(globalObject),
294  objState_(GOOD)
295 {
296  if (!fileNameComponents(path, instance_, local_, name_))
297  {
299  << " invalid path specification"
300  << exit(FatalError);
301  }
302 
303  if (objectRegistry::debug)
304  {
306  << "Constructing IOobject called " << name_
307  << " of type " << headerClassName_
308  << endl;
309  }
310 }
311 
312 
314 (
315  const IOobject& io,
316  const objectRegistry& registry
317 )
318 :
319  name_(io.name_),
320  headerClassName_(io.headerClassName_),
321  note_(io.note_),
322  instance_(io.instance_),
323  local_(io.local_),
324  db_(registry),
325  rOpt_(io.rOpt_),
326  wOpt_(io.wOpt_),
327  registerObject_(io.registerObject_),
328  globalObject_(io.globalObject_),
329  objState_(io.objState_)
330 {}
331 
332 
334 (
335  const IOobject& io,
336  const word& name
337 )
338 :
339  name_(name),
340  headerClassName_(io.headerClassName_),
341  note_(io.note_),
342  instance_(io.instance_),
343  local_(io.local_),
344  db_(io.db_),
345  rOpt_(io.rOpt_),
346  wOpt_(io.wOpt_),
347  registerObject_(io.registerObject_),
348  globalObject_(io.globalObject_),
349  objState_(io.objState_)
350 {}
351 
352 
353 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
354 
356 {}
357 
358 
359 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
360 
362 {
363  return db_;
364 }
365 
366 
368 {
369  return db_.time();
370 }
371 
372 
374 {
375  return time().caseName();
376 }
377 
378 
380 {
381  return group(name_);
382 }
383 
384 
386 {
387  return member(name_);
388 }
389 
390 
392 {
393  return time().rootPath();
394 }
395 
396 
398 {
399  if (instance().isAbsolute())
400  {
401  return instance();
402  }
403  else
404  {
405  return rootPath()/caseName()/instance()/db_.dbDir()/local();
406  }
407 }
408 
409 
411 (
412  const word& instance,
413  const fileName& local
414 ) const
415 {
416  // Note: can only be called with relative instance since is word type
417  return rootPath()/caseName()/instance/db_.dbDir()/local;
418 }
419 
420 
422 {
423  // Do not check for undecomposed files
424  return fileHandler().filePath(false, *this, typeName);
425 }
426 
427 
429 {
430  // Check for undecomposed files
431  return fileHandler().filePath(true, *this, typeName);
432 }
433 
434 
435 void Foam::IOobject::setBad(const string& s)
436 {
437  if (objState_ != GOOD)
438  {
440  << "Recurrent failure for object " << s
441  << exit(FatalError);
442  }
443 
444  if (error::level)
445  {
447  << "Broken object " << s << info() << endl;
448  }
449 
450  objState_ = BAD;
451 }
452 
453 
455 {
456  name_ = io.name_;
457  headerClassName_ = io.headerClassName_;
458  note_ = io.note_;
459  instance_ = io.instance_;
460  local_ = io.local_;
461  rOpt_ = io.rOpt_;
462  wOpt_ = io.wOpt_;
463  globalObject_ = io.globalObject_;
464  objState_ = io.objState_;
465 }
466 
467 
468 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:123
const char *const group
Group name for atomic constants.
Abstract base class for registered object with I/O. Used in debug symbol registration.
writeOption
Enumeration defining the write options.
Definition: IOobject.H:116
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:106
A class for handling file names.
Definition: fileName.H:69
addfileModificationCheckingToOpt(const char *name)
Definition: IOobject.C:75
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void operator=(const IOobject &)
Definition: IOobject.C:454
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual void readData(Foam::Istream &is)
Read.
Definition: IOobject.C:83
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:194
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:209
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
word member() const
Return member (name without the extension)
Definition: IOobject.C:385
virtual ~IOobject()
Destructor.
Definition: IOobject.C:355
word group() const
Return group (extension part of name)
Definition: IOobject.C:379
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
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:68
fileName path() const
Return complete path.
Definition: IOobject.C:397
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:421
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:57
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:528
A class for handling words, derived from string.
Definition: word.H:59
void setBad(const string &)
Set the object state to bad.
Definition: IOobject.C:435
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:428
static const word null
An empty word.
Definition: word.H:77
virtual fileName filePath(const bool checkGlobal, const IOobject &, const word &typeName) const =0
Search for an object. checkGlobal : also check undecomposed case.
const fileOperation & fileHandler()
Get current file handler.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
addfileModificationCheckingToOpt addfileModificationCheckingToOpt_("fileModificationChecking")
const Time & time() const
Return time.
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:214
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: IOobject.C:89
const fileName & rootPath() const
Definition: IOobject.C:391
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:131
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:367
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:172
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:361
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:261
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
const fileName & caseName() const
Definition: IOobject.C:373
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
#define InfoInFunction
Report an information message using Foam::Info.