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 | Copyright (C) 2011-2017 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 
176 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
177 
179 (
180  const word& name,
181  const fileName& instance,
182  const objectRegistry& registry,
183  readOption ro,
184  writeOption wo,
185  bool registerObject
186 )
187 :
188  name_(name),
189  headerClassName_(typeName),
190  note_(),
191  instance_(instance),
192  local_(),
193  db_(registry),
194  rOpt_(ro),
195  wOpt_(wo),
196  registerObject_(registerObject),
197  globalObject_(false),
198  objState_(GOOD)
199 {
200  if (objectRegistry::debug)
201  {
203  << "Constructing IOobject called " << name_
204  << " of type " << headerClassName_
205  << endl;
206  }
207 }
208 
209 
211 (
212  const word& name,
213  const fileName& instance,
214  const fileName& local,
215  const objectRegistry& registry,
216  readOption ro,
217  writeOption wo,
218  bool registerObject,
219  bool globalObject
220 )
221 :
222  name_(name),
223  headerClassName_(typeName),
224  note_(),
225  instance_(instance),
226  local_(local),
227  db_(registry),
228  rOpt_(ro),
229  wOpt_(wo),
230  registerObject_(registerObject),
231  globalObject_(globalObject),
232  objState_(GOOD)
233 {
234  if (objectRegistry::debug)
235  {
237  << "Constructing IOobject called " << name_
238  << " of type " << headerClassName_
239  << endl;
240  }
241 }
242 
243 
245 (
246  const fileName& path,
247  const objectRegistry& registry,
248  readOption ro,
249  writeOption wo,
250  bool registerObject,
251  bool globalObject
252 )
253 :
254  name_(),
255  headerClassName_(typeName),
256  note_(),
257  instance_(),
258  local_(),
259  db_(registry),
260  rOpt_(ro),
261  wOpt_(wo),
262  registerObject_(registerObject),
263  globalObject_(globalObject),
264  objState_(GOOD)
265 {
266  if (!fileNameComponents(path, instance_, local_, name_))
267  {
269  << " invalid path specification"
270  << exit(FatalError);
271  }
272 
273  if (objectRegistry::debug)
274  {
276  << "Constructing IOobject called " << name_
277  << " of type " << headerClassName_
278  << endl;
279  }
280 }
281 
282 
284 (
285  const IOobject& io,
286  const objectRegistry& registry
287 )
288 :
289  name_(io.name_),
290  headerClassName_(io.headerClassName_),
291  note_(io.note_),
292  instance_(io.instance_),
293  local_(io.local_),
294  db_(registry),
295  rOpt_(io.rOpt_),
296  wOpt_(io.wOpt_),
297  registerObject_(io.registerObject_),
298  globalObject_(io.globalObject_),
299  objState_(io.objState_)
300 {}
301 
302 
304 (
305  const IOobject& io,
306  const word& name
307 )
308 :
309  name_(name),
310  headerClassName_(io.headerClassName_),
311  note_(io.note_),
312  instance_(io.instance_),
313  local_(io.local_),
314  db_(io.db_),
315  rOpt_(io.rOpt_),
316  wOpt_(io.wOpt_),
317  registerObject_(io.registerObject_),
318  globalObject_(io.globalObject_),
319  objState_(io.objState_)
320 {}
321 
322 
323 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
324 
326 {}
327 
328 
329 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
330 
332 {
333  return db_;
334 }
335 
336 
338 {
339  return db_.time();
340 }
341 
342 
344 {
345  return time().caseName();
346 }
347 
348 
350 {
351  word::size_type i = name_.find_last_of('.');
352 
353  if (i == word::npos || i == 0)
354  {
355  return word::null;
356  }
357  else
358  {
359  return name_.substr(i+1, word::npos);
360  }
361 }
362 
363 
365 {
366  word::size_type i = name_.find_last_of('.');
367 
368  if (i == word::npos || i == 0)
369  {
370  return name_;
371  }
372  else
373  {
374  return name_.substr(0, i);
375  }
376 }
377 
378 
380 {
381  return time().rootPath();
382 }
383 
384 
386 {
387  if (instance().isAbsolute())
388  {
389  return instance();
390  }
391  else
392  {
393  return rootPath()/caseName()/instance()/db_.dbDir()/local();
394  }
395 }
396 
397 
399 (
400  const word& instance,
401  const fileName& local
402 ) const
403 {
404  // Note: can only be called with relative instance since is word type
405  return rootPath()/caseName()/instance/db_.dbDir()/local;
406 }
407 
408 
410 {
411  // Do not check for undecomposed files
412  return fileHandler().filePath(false, *this, typeName);
413 }
414 
415 
417 {
418  // Check for undecomposed files
419  return fileHandler().filePath(true, *this, typeName);
420 }
421 
422 
423 void Foam::IOobject::setBad(const string& s)
424 {
425  if (objState_ != GOOD)
426  {
428  << "Recurrent failure for object " << s
429  << exit(FatalError);
430  }
431 
432  if (error::level)
433  {
435  << "Broken object " << s << info() << endl;
436  }
437 
438  objState_ = BAD;
439 }
440 
441 
443 {
444  name_ = io.name_;
445  headerClassName_ = io.headerClassName_;
446  note_ = io.note_;
447  instance_ = io.instance_;
448  local_ = io.local_;
449  rOpt_ = io.rOpt_;
450  wOpt_ = io.wOpt_;
451  globalObject_ = io.globalObject_;
452  objState_ = io.objState_;
453 }
454 
455 
456 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:123
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:442
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 /), subsitute for FOAM_CASE.
Definition: fileName.C:195
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:179
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
word member() const
Return member (name without the extension)
Definition: IOobject.C:364
virtual ~IOobject()
Destructor.
Definition: IOobject.C:325
word group() const
Return group (extension part of name)
Definition: IOobject.C:349
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
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:385
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:409
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:58
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:536
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:423
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:416
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:208
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:379
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:337
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:162
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:331
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:251
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:343
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.