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-2019 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  objState_(GOOD)
228 {
229  if (objectRegistry::debug)
230  {
232  << "Constructing IOobject called " << name_
233  << " of type " << headerClassName_
234  << endl;
235  }
236 }
237 
238 
240 (
241  const word& name,
242  const fileName& instance,
243  const fileName& local,
244  const objectRegistry& registry,
245  readOption ro,
246  writeOption wo,
247  bool registerObject
248 )
249 :
250  name_(name),
251  headerClassName_(typeName),
252  note_(),
253  instance_(instance),
254  local_(local),
255  db_(registry),
256  rOpt_(ro),
257  wOpt_(wo),
258  registerObject_(registerObject),
259  objState_(GOOD)
260 {
261  if (objectRegistry::debug)
262  {
264  << "Constructing IOobject called " << name_
265  << " of type " << headerClassName_
266  << endl;
267  }
268 }
269 
270 
272 (
273  const fileName& path,
274  const objectRegistry& registry,
275  readOption ro,
276  writeOption wo,
277  bool registerObject
278 )
279 :
280  name_(),
281  headerClassName_(typeName),
282  note_(),
283  instance_(),
284  local_(),
285  db_(registry),
286  rOpt_(ro),
287  wOpt_(wo),
288  registerObject_(registerObject),
289  objState_(GOOD)
290 {
291  if (!fileNameComponents(path, instance_, local_, name_))
292  {
294  << " invalid path specification"
295  << exit(FatalError);
296  }
297 
298  if (objectRegistry::debug)
299  {
301  << "Constructing IOobject called " << name_
302  << " of type " << headerClassName_
303  << endl;
304  }
305 }
306 
307 
309 (
310  const IOobject& io,
311  const objectRegistry& registry
312 )
313 :
314  name_(io.name_),
315  headerClassName_(io.headerClassName_),
316  note_(io.note_),
317  instance_(io.instance_),
318  local_(io.local_),
319  db_(registry),
320  rOpt_(io.rOpt_),
321  wOpt_(io.wOpt_),
322  registerObject_(io.registerObject_),
323  objState_(io.objState_)
324 {}
325 
326 
328 (
329  const IOobject& io,
330  const word& name
331 )
332 :
333  name_(name),
334  headerClassName_(io.headerClassName_),
335  note_(io.note_),
336  instance_(io.instance_),
337  local_(io.local_),
338  db_(io.db_),
339  rOpt_(io.rOpt_),
340  wOpt_(io.wOpt_),
341  registerObject_(io.registerObject_),
342  objState_(io.objState_)
343 {}
344 
345 
346 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
347 
349 {}
350 
351 
352 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
353 
355 {
356  return db_;
357 }
358 
359 
361 {
362  return db_.time();
363 }
364 
365 
367 {
368  return time().caseName();
369 }
370 
371 
373 {
374  return group(name_);
375 }
376 
377 
379 {
380  return member(name_);
381 }
382 
383 
385 {
386  return time().rootPath();
387 }
388 
389 
391 {
392  if (instance().isAbsolute())
393  {
394  return instance();
395  }
396  else
397  {
398  return rootPath()/caseName()/instance()/db_.dbDir()/local();
399  }
400 }
401 
402 
404 (
405  const word& instance,
406  const fileName& local
407 ) const
408 {
409  // Note: can only be called with relative instance since is word type
410  return rootPath()/caseName()/instance/db_.dbDir()/local;
411 }
412 
413 
415 {
416  // Do not check for undecomposed files
417  return fileHandler().filePath(false, *this, typeName);
418 }
419 
420 
422 {
423  // Check for undecomposed files
424  return fileHandler().filePath(true, *this, typeName);
425 }
426 
427 
428 void Foam::IOobject::setBad(const string& s)
429 {
430  if (objState_ != GOOD)
431  {
433  << "Recurrent failure for object " << s
434  << exit(FatalError);
435  }
436 
437  if (error::level)
438  {
440  << "Broken object " << s << info() << endl;
441  }
442 
443  objState_ = BAD;
444 }
445 
446 
448 {
449  name_ = io.name_;
450  headerClassName_ = io.headerClassName_;
451  note_ = io.note_;
452  instance_ = io.instance_;
453  local_ = io.local_;
454  rOpt_ = io.rOpt_;
455  wOpt_ = io.wOpt_;
456  objState_ = io.objState_;
457 }
458 
459 
460 // ************************************************************************* //
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:79
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:447
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:198
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:378
virtual ~IOobject()
Destructor.
Definition: IOobject.C:348
word group() const
Return group (extension part of name)
Definition: IOobject.C:372
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:390
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:414
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:61
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
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:428
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:421
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.
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:211
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:384
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:360
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:354
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:366
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())
#define InfoInFunction
Report an information message using Foam::Info.