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-2020 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 #include "registerNamedEnum.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(IOobject, 0);
36 
37  template<>
39  {
40  "timeStamp",
41  "timeStampMaster",
42  "inotify",
43  "inotifyMaster"
44  };
45 }
46 
49 
50 // Default fileCheck type
52 (
53  fileCheckTypesNames.read
54  (
56  (
57  "fileModificationChecking"
58  )
59  )
60 );
61 
62 // Register re-reader
64 (
65  "fileModificationChecking",
66  Foam::IOobject::fileCheckTypesNames,
67  Foam::IOobject::fileModificationChecking
68 );
69 
70 
71 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
72 
74 (
75  const fileName& path,
76  fileName& instance,
77  fileName& local,
78  word& name
79 )
80 {
81  instance.clear();
82  local.clear();
83  name.clear();
84 
85  // called with directory
86  if (isDir(path))
87  {
89  << " called with directory: " << path << endl;
90 
91  return false;
92  }
93 
94  if (path.isAbsolute())
95  {
96  string::size_type last = path.rfind('/');
97  instance = path.substr(0, last);
98 
99  // Check afterwards
100  name.string::operator=(path.substr(last+1));
101  }
102  else
103  {
104  string::size_type first = path.find('/');
105 
106  if (first == string::npos)
107  {
108  // no '/' found - no instance or local
109 
110  // check afterwards
111  name.string::operator=(path);
112  }
113  else
114  {
115  instance = path.substr(0, first);
116 
117  string::size_type last = path.rfind('/');
118  if (last > first)
119  {
120  // with local
121  local = path.substr(first+1, last-first-1);
122  }
123 
124  // check afterwards
125  name.string::operator=(path.substr(last+1));
126  }
127  }
128 
129 
130  // Check for valid (and stripped) name, regardless of the debug level
131  if (name.empty() || string::stripInvalid<word>(name))
132  {
134  << "has invalid word for name: \"" << name
135  << "\"\nwhile processing path: " << path << endl;
136 
137  return false;
138  }
139 
140  return true;
141 }
142 
143 
145 {
146  word::size_type i = name.find_last_of('.');
147 
148  if (i == word::npos || i == 0)
149  {
150  return word::null;
151  }
152  else
153  {
154  return name.substr(i+1, word::npos);
155  }
156 }
157 
158 
160 {
161  word::size_type i = name.find_last_of('.');
162 
163  if (i == word::npos || i == 0)
164  {
165  return name;
166  }
167  else
168  {
169  return name.substr(0, i);
170  }
171 }
172 
173 
174 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
175 
177 (
178  const word& name,
179  const fileName& instance,
180  const objectRegistry& registry,
181  readOption ro,
182  writeOption wo,
183  bool registerObject
184 )
185 :
186  name_(name),
187  headerClassName_(typeName),
188  note_(),
189  instance_(instance),
190  local_(),
191  db_(registry),
192  rOpt_(ro),
193  wOpt_(wo),
194  registerObject_(registerObject),
195  objState_(GOOD)
196 {
197  if (objectRegistry::debug)
198  {
200  << "Constructing IOobject called " << name_
201  << " of type " << headerClassName_
202  << endl;
203  }
204 }
205 
206 
208 (
209  const word& name,
210  const fileName& instance,
211  const fileName& local,
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_(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 fileName& path,
242  const objectRegistry& registry,
243  readOption ro,
244  writeOption wo,
245  bool registerObject
246 )
247 :
248  name_(),
249  headerClassName_(typeName),
250  note_(),
251  instance_(),
252  local_(),
253  db_(registry),
254  rOpt_(ro),
255  wOpt_(wo),
256  registerObject_(registerObject),
257  objState_(GOOD)
258 {
259  if (!fileNameComponents(path, instance_, local_, name_))
260  {
262  << " invalid path specification"
263  << exit(FatalError);
264  }
265 
266  if (objectRegistry::debug)
267  {
269  << "Constructing IOobject called " << name_
270  << " of type " << headerClassName_
271  << endl;
272  }
273 }
274 
275 
277 (
278  const IOobject& io,
279  const objectRegistry& registry
280 )
281 :
282  name_(io.name_),
283  headerClassName_(io.headerClassName_),
284  note_(io.note_),
285  instance_(io.instance_),
286  local_(io.local_),
287  db_(registry),
288  rOpt_(io.rOpt_),
289  wOpt_(io.wOpt_),
290  registerObject_(io.registerObject_),
291  objState_(io.objState_)
292 {}
293 
294 
296 (
297  const IOobject& io,
298  const word& name
299 )
300 :
301  name_(name),
302  headerClassName_(io.headerClassName_),
303  note_(io.note_),
304  instance_(io.instance_),
305  local_(io.local_),
306  db_(io.db_),
307  rOpt_(io.rOpt_),
308  wOpt_(io.wOpt_),
309  registerObject_(io.registerObject_),
310  objState_(io.objState_)
311 {}
312 
313 
314 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
315 
317 {}
318 
319 
320 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
321 
323 {
324  return db_;
325 }
326 
327 
329 {
330  return db_.time();
331 }
332 
333 
335 {
336  return time().caseName();
337 }
338 
339 
341 {
342  return group(name_);
343 }
344 
345 
347 {
348  return member(name_);
349 }
350 
351 
353 {
354  return time().rootPath();
355 }
356 
357 
359 {
360  if (instance().isAbsolute())
361  {
362  return instance();
363  }
364  else
365  {
366  return rootPath()/caseName()/instance()/db_.dbDir()/local();
367  }
368 }
369 
370 
372 (
373  const word& instance,
374  const fileName& local
375 ) const
376 {
377  // Note: can only be called with relative instance since is word type
378  return rootPath()/caseName()/instance/db_.dbDir()/local;
379 }
380 
381 
383 {
384  if (instance().isAbsolute())
385  {
386  return instance();
387  }
388  else
389  {
390  return instance()/db_.dbDir()/local();
391  }
392 }
393 
394 
396 {
397  // Do not check for undecomposed files
398  return fileHandler().filePath(false, *this, typeName);
399 }
400 
401 
403 {
404  // Check for undecomposed files
405  return fileHandler().filePath(true, *this, typeName);
406 }
407 
408 
409 void Foam::IOobject::setBad(const string& s)
410 {
411  if (objState_ != GOOD)
412  {
414  << "Recurrent failure for object " << s
415  << exit(FatalError);
416  }
417 
418  if (error::level)
419  {
421  << "Broken object " << s << info() << endl;
422  }
423 
424  objState_ = BAD;
425 }
426 
427 
429 {
430  name_ = io.name_;
431  headerClassName_ = io.headerClassName_;
432  note_ = io.note_;
433  instance_ = io.instance_;
434  local_ = io.local_;
435  rOpt_ = io.rOpt_;
436  wOpt_ = io.wOpt_;
437  objState_ = io.objState_;
438 }
439 
440 
441 // ************************************************************************* //
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:126
const char *const group
Group name for atomic constants.
writeOption
Enumeration defining the write options.
Definition: IOobject.H:119
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:74
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void operator=(const IOobject &)
Definition: IOobject.C:428
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:198
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:177
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
word member() const
Return member (name without the extension)
Definition: IOobject.C:346
virtual ~IOobject()
Destructor.
Definition: IOobject.C:316
word group() const
Return group (extension part of name)
Definition: IOobject.C:340
readOption
Enumeration defining the read options.
Definition: IOobject.H:110
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:358
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:395
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:409
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:402
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
const Time & time() const
Return time.
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:219
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const fileName & rootPath() const
Definition: IOobject.C:352
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:134
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:328
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:322
registerOptNamedEnum("fileModificationChecking", Foam::IOobject::fileCheckTypesNames, Foam::IOobject::fileModificationChecking)
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:334
fileName localPath() const
Return the path relative to the case.
Definition: IOobject.C:382
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:812
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())
#define InfoInFunction
Report an information message using Foam::Info.