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-2025 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 {
36 }
37 
40 {
41  "timeStamp",
42  "timeStampMaster",
43  "inotify",
44  "inotifyMaster"
45 };
46 
47 // Default fileCheck type
49 (
51  (
52  "fileModificationChecking",
53  fileCheckTypesNames,
54  fileModificationChecking
55  )
56 );
57 
58 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
59 
61 (
62  const fileName& path,
63  fileName& instance,
64  fileName& local,
65  word& name
66 )
67 {
68  instance.clear();
69  local.clear();
70  name.clear();
71 
72  // called with directory
73  if (isDir(path))
74  {
76  << " called with directory: " << path << endl;
77 
78  return false;
79  }
80 
81  if (path.isAbsolute())
82  {
83  string::size_type last = path.rfind('/');
84  instance = path.substr(0, last);
85 
86  // Check afterwards
87  name.string::operator=(path.substr(last+1));
88  }
89  else
90  {
91  string::size_type first = path.find('/');
92 
93  if (first == string::npos)
94  {
95  // no '/' found - no instance or local
96 
97  // check afterwards
98  name.string::operator=(path);
99  }
100  else
101  {
102  instance = path.substr(0, first);
103 
104  string::size_type last = path.rfind('/');
105  if (last > first)
106  {
107  // with local
108  local = path.substr(first+1, last-first-1);
109  }
110 
111  // check afterwards
112  name.string::operator=(path.substr(last+1));
113  }
114  }
115 
116 
117  // Check for valid (and stripped) name, regardless of the debug level
118  if (name.empty() || string::stripInvalid<word>(name))
119  {
121  << "has invalid word for name: \"" << name
122  << "\"\nwhile processing path: " << path << endl;
123 
124  return false;
125  }
126 
127  return true;
128 }
129 
130 
132 {
133  word::size_type i = name.find_last_of('.');
134 
135  if (i == word::npos || i == 0)
136  {
137  return word::null;
138  }
139  else
140  {
141  return name.substr(i+1, word::npos);
142  }
143 }
144 
145 
147 {
148  word::size_type i = name.find_last_of('.');
149 
150  if (i == word::npos || i == 0)
151  {
152  return name;
153  }
154  else
155  {
156  return name.substr(0, i);
157  }
158 }
159 
160 
161 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
162 
164 (
165  const word& name,
166  const fileName& instance,
167  const objectRegistry& registry,
168  readOption ro,
169  writeOption wo,
170  bool registerObject
171 )
172 :
173  name_(name),
174  headerClassName_(typeName),
175  note_(),
176  instance_(instance),
177  local_(),
178  db_(registry),
179  rOpt_(ro),
180  wOpt_(wo),
181  registerObject_(registerObject),
182  objState_(GOOD)
183 {
184  if (objectRegistry::debug)
185  {
187  << "Constructing IOobject called " << name_
188  << " of type " << headerClassName_
189  << endl;
190  }
191 }
192 
193 
195 (
196  const word& name,
197  const fileName& instance,
198  const fileName& local,
199  const objectRegistry& registry,
200  readOption ro,
201  writeOption wo,
202  bool registerObject
203 )
204 :
205  name_(name),
206  headerClassName_(typeName),
207  note_(),
208  instance_(instance),
209  local_(local),
210  db_(registry),
211  rOpt_(ro),
212  wOpt_(wo),
213  registerObject_(registerObject),
214  objState_(GOOD)
215 {
216  if (objectRegistry::debug)
217  {
219  << "Constructing IOobject called " << name_
220  << " of type " << headerClassName_
221  << endl;
222  }
223 }
224 
225 
227 (
228  const fileName& path,
229  const objectRegistry& registry,
230  readOption ro,
231  writeOption wo,
232  bool registerObject
233 )
234 :
235  name_(),
236  headerClassName_(typeName),
237  note_(),
238  instance_(),
239  local_(),
240  db_(registry),
241  rOpt_(ro),
242  wOpt_(wo),
243  registerObject_(registerObject),
244  objState_(GOOD)
245 {
246  if (!fileNameComponents(path, instance_, local_, name_))
247  {
249  << " invalid path specification"
250  << exit(FatalError);
251  }
252 
253  if (objectRegistry::debug)
254  {
256  << "Constructing IOobject called " << name_
257  << " of type " << headerClassName_
258  << endl;
259  }
260 }
261 
262 
264 (
265  const IOobject& io,
266  const objectRegistry& registry
267 )
268 :
269  name_(io.name_),
270  headerClassName_(io.headerClassName_),
271  note_(io.note_),
272  instance_(io.instance_),
273  local_(io.local_),
274  db_(registry),
275  rOpt_(io.rOpt_),
276  wOpt_(io.wOpt_),
277  registerObject_(io.registerObject_),
278  objState_(io.objState_)
279 {}
280 
281 
283 (
284  const IOobject& io,
285  const word& name
286 )
287 :
288  name_(name),
289  headerClassName_(io.headerClassName_),
290  note_(io.note_),
291  instance_(io.instance_),
292  local_(io.local_),
293  db_(io.db_),
294  rOpt_(io.rOpt_),
295  wOpt_(io.wOpt_),
296  registerObject_(io.registerObject_),
297  objState_(io.objState_)
298 {}
299 
300 
301 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
302 
304 {}
305 
306 
307 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
308 
310 {
311  return db_;
312 }
313 
314 
316 {
317  return db_.time();
318 }
319 
320 
322 {
323  return group(name_);
324 }
325 
326 
328 {
329  return member(name_);
330 }
331 
332 
334 {
335  return time().rootPath();
336 }
337 
338 
339 const Foam::fileName& Foam::IOobject::caseName(const bool global) const
340 {
341  if (global)
342  {
343  return time().globalCaseName();
344  }
345  else
346  {
347  return time().caseName();
348  }
349 }
350 
351 
353 {
354  return instance_;
355 }
356 
357 
359 {
360  if
361  (
362  !instance_.isAbsolute()
363  && instance_ != time().system()
364  && instance_ != time().constant()
365  && instance_ != time().name()
366  )
367  {
368  scalar timeValue;
369  if (readScalar(instance_.c_str(), timeValue))
370  {
371  instance_ = time().name();
372  }
373  }
374 }
375 
376 
378 {
379  instance_ = time().name();
380 }
381 
382 
383 Foam::fileName Foam::IOobject::path(const bool global) const
384 {
385  if (instance_.isAbsolute())
386  {
387  return instance_;
388  }
389  else
390  {
391  return rootPath()/caseName(global)/instance()/db_.dbDir()/local();
392  }
393 }
394 
395 
397 {
398  if (instance().isAbsolute())
399  {
400  return instance();
401  }
402  else
403  {
404  return instance()/db_.dbDir()/local();
405  }
406 }
407 
408 
409 Foam::fileName Foam::IOobject::filePath(const bool global) const
410 {
411  return fileHandler().filePath(global, *this);
412 }
413 
414 
416 {
417  name_ = io.name_;
418  headerClassName_ = io.headerClassName_;
419  note_ = io.note_;
420  instance_ = io.instance_;
421  local_ = io.local_;
422  rOpt_ = io.rOpt_;
423  wOpt_ = io.wOpt_;
424  objState_ = io.objState_;
425 }
426 
427 
428 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const fileName & local() const
Definition: IOobject.H:400
const fileName & caseName(const bool global) const
Definition: IOobject.C:339
const Time & time() const
Return time.
Definition: IOobject.C:315
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
fileName relativePath() const
Return the path relative to the case directory.
Definition: IOobject.C:396
fileName & instance() const
Return the instance directory, constant, system, <time> etc.
Definition: IOobject.C:352
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:309
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:164
static bool fileNameComponents(const fileName &path, fileName &instance, fileName &local, word &name)
Split path into instance, local, name components.
Definition: IOobject.C:61
void updateInstance() const
If the instance is a time directory update to the current time.
Definition: IOobject.C:358
fileName filePath(const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:409
word group() const
Return group (extension part of name)
Definition: IOobject.C:321
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:223
void operator=(const IOobject &)
Definition: IOobject.C:415
word member() const
Return member (name without the extension)
Definition: IOobject.C:327
const word & name() const
Return name.
Definition: IOobject.H:307
const fileName & rootPath() const
Definition: IOobject.C:333
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: IOobject.H:140
void updateTimeInstance() const
Update instance to the current time.
Definition: IOobject.C:377
fileCheckTypes
Enumeration defining the file checking options.
Definition: IOobject.H:133
writeOption
Enumeration defining the write options.
Definition: IOobject.H:126
virtual ~IOobject()
Destructor.
Definition: IOobject.C:303
fileName path(const bool global) const
Return complete path including the processor sub-directory.
Definition: IOobject.C:383
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A class for handling file names.
Definition: fileName.H:82
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:73
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:210
virtual fileName filePath(const bool globalFile, const IOobject &) const =0
Search for an object. globalFile : also check undecomposed case.
Registry of regIOobjects.
const Time & time() const
Return time.
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
const char *const group
Group name for atomic constants.
Enum namedEnumOptimisationSwitch(const char *name, const NamedEnum< Enum, nEnum > &enumNames, const Enum defaultValue)
Lookup optimisation switch or add default value.
Definition: debug.H:94
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
defineTypeNameAndDebug(combustionModel, 0)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError