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 "OSspecific.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 (
54  (
55  "fileModificationChecking",
56  fileCheckTypesNames,
57  fileModificationChecking
58  )
59 );
60 
61 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
62 
64 (
65  const fileName& path,
66  fileName& instance,
67  fileName& local,
68  word& name
69 )
70 {
71  instance.clear();
72  local.clear();
73  name.clear();
74 
75  // called with directory
76  if (isDir(path))
77  {
79  << " called with directory: " << path << endl;
80 
81  return false;
82  }
83 
84  if (path.isAbsolute())
85  {
86  string::size_type last = path.rfind('/');
87  instance = path.substr(0, last);
88 
89  // Check afterwards
90  name.string::operator=(path.substr(last+1));
91  }
92  else
93  {
94  string::size_type first = path.find('/');
95 
96  if (first == string::npos)
97  {
98  // no '/' found - no instance or local
99 
100  // check afterwards
101  name.string::operator=(path);
102  }
103  else
104  {
105  instance = path.substr(0, first);
106 
107  string::size_type last = path.rfind('/');
108  if (last > first)
109  {
110  // with local
111  local = path.substr(first+1, last-first-1);
112  }
113 
114  // check afterwards
115  name.string::operator=(path.substr(last+1));
116  }
117  }
118 
119 
120  // Check for valid (and stripped) name, regardless of the debug level
121  if (name.empty() || string::stripInvalid<word>(name))
122  {
124  << "has invalid word for name: \"" << name
125  << "\"\nwhile processing path: " << path << endl;
126 
127  return false;
128  }
129 
130  return true;
131 }
132 
133 
135 {
136  word::size_type i = name.find_last_of('.');
137 
138  if (i == word::npos || i == 0)
139  {
140  return word::null;
141  }
142  else
143  {
144  return name.substr(i+1, word::npos);
145  }
146 }
147 
148 
150 {
151  word::size_type i = name.find_last_of('.');
152 
153  if (i == word::npos || i == 0)
154  {
155  return name;
156  }
157  else
158  {
159  return name.substr(0, i);
160  }
161 }
162 
163 
164 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
165 
167 (
168  const word& name,
169  const fileName& instance,
170  const objectRegistry& registry,
171  readOption ro,
172  writeOption wo,
173  bool registerObject
174 )
175 :
176  name_(name),
177  headerClassName_(typeName),
178  note_(),
179  instance_(instance),
180  local_(),
181  db_(registry),
182  rOpt_(ro),
183  wOpt_(wo),
184  registerObject_(registerObject),
185  objState_(GOOD)
186 {
187  if (objectRegistry::debug)
188  {
190  << "Constructing IOobject called " << name_
191  << " of type " << headerClassName_
192  << endl;
193  }
194 }
195 
196 
198 (
199  const word& name,
200  const fileName& instance,
201  const fileName& local,
202  const objectRegistry& registry,
203  readOption ro,
204  writeOption wo,
205  bool registerObject
206 )
207 :
208  name_(name),
209  headerClassName_(typeName),
210  note_(),
211  instance_(instance),
212  local_(local),
213  db_(registry),
214  rOpt_(ro),
215  wOpt_(wo),
216  registerObject_(registerObject),
217  objState_(GOOD)
218 {
219  if (objectRegistry::debug)
220  {
222  << "Constructing IOobject called " << name_
223  << " of type " << headerClassName_
224  << endl;
225  }
226 }
227 
228 
230 (
231  const fileName& path,
232  const objectRegistry& registry,
233  readOption ro,
234  writeOption wo,
235  bool registerObject
236 )
237 :
238  name_(),
239  headerClassName_(typeName),
240  note_(),
241  instance_(),
242  local_(),
243  db_(registry),
244  rOpt_(ro),
245  wOpt_(wo),
246  registerObject_(registerObject),
247  objState_(GOOD)
248 {
249  if (!fileNameComponents(path, instance_, local_, name_))
250  {
252  << " invalid path specification"
253  << exit(FatalError);
254  }
255 
256  if (objectRegistry::debug)
257  {
259  << "Constructing IOobject called " << name_
260  << " of type " << headerClassName_
261  << endl;
262  }
263 }
264 
265 
267 (
268  const IOobject& io,
269  const objectRegistry& registry
270 )
271 :
272  name_(io.name_),
273  headerClassName_(io.headerClassName_),
274  note_(io.note_),
275  instance_(io.instance_),
276  local_(io.local_),
277  db_(registry),
278  rOpt_(io.rOpt_),
279  wOpt_(io.wOpt_),
280  registerObject_(io.registerObject_),
281  objState_(io.objState_)
282 {}
283 
284 
286 (
287  const IOobject& io,
288  const word& name
289 )
290 :
291  name_(name),
292  headerClassName_(io.headerClassName_),
293  note_(io.note_),
294  instance_(io.instance_),
295  local_(io.local_),
296  db_(io.db_),
297  rOpt_(io.rOpt_),
298  wOpt_(io.wOpt_),
299  registerObject_(io.registerObject_),
300  objState_(io.objState_)
301 {}
302 
303 
304 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
305 
307 {}
308 
309 
310 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
311 
313 {
314  return db_;
315 }
316 
317 
319 {
320  return db_.time();
321 }
322 
323 
325 {
326  return time().caseName();
327 }
328 
329 
331 {
332  return group(name_);
333 }
334 
335 
337 {
338  return member(name_);
339 }
340 
341 
343 {
344  return time().rootPath();
345 }
346 
347 
349 {
350  if (instance().isAbsolute())
351  {
352  return instance();
353  }
354  else
355  {
356  return rootPath()/caseName()/instance()/db_.dbDir()/local();
357  }
358 }
359 
360 
362 (
363  const word& instance,
364  const fileName& local
365 ) const
366 {
367  // Note: can only be called with relative instance since is word type
368  return rootPath()/caseName()/instance/db_.dbDir()/local;
369 }
370 
371 
373 {
374  if (instance().isAbsolute())
375  {
376  return instance();
377  }
378  else
379  {
380  return instance()/db_.dbDir()/local();
381  }
382 }
383 
384 
386 {
387  // Do not check for undecomposed files
388  return fileHandler().filePath(false, *this, typeName);
389 }
390 
391 
393 {
394  // Check for undecomposed files
395  return fileHandler().filePath(true, *this, typeName);
396 }
397 
398 
399 void Foam::IOobject::setBad(const string& s)
400 {
401  if (objState_ != GOOD)
402  {
404  << "Recurrent failure for object " << s
405  << exit(FatalError);
406  }
407 
408  if (error::level)
409  {
411  << "Broken object " << s << info() << endl;
412  }
413 
414  objState_ = BAD;
415 }
416 
417 
419 {
420  name_ = io.name_;
421  headerClassName_ = io.headerClassName_;
422  note_ = io.note_;
423  instance_ = io.instance_;
424  local_ = io.local_;
425  rOpt_ = io.rOpt_;
426  wOpt_ = io.wOpt_;
427  objState_ = io.objState_;
428 }
429 
430 
431 // ************************************************************************* //
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:64
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:418
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:210
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:167
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:336
virtual ~IOobject()
Destructor.
Definition: IOobject.C:306
word group() const
Return group (extension part of name)
Definition: IOobject.C:330
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:348
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:385
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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:73
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:399
fileName globalFilePath(const word &typeName) const
Helper for filePath that searches up if in parallel.
Definition: IOobject.C:392
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:342
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:318
Enum namedEnumOptimisationSwitch(const char *name, const NamedEnum< Enum, nEnum > &enumNames, const Enum defaultValue)
Lookup optimisation switch or add default value.
Definition: debug.H:94
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
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:324
fileName localPath() const
Return the path relative to the case.
Definition: IOobject.C:372
Namespace for OpenFOAM.
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())
#define InfoInFunction
Report an information message using Foam::Info.