FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/Time.H>
28 #include <OpenFOAM/IFstream.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
35 
36 // Return components following the IOobject requirements
37 //
38 // behaviour
39 // input IOobject(instance, local, name)
40 // ----- ------
41 // "foo" ("", "", "foo")
42 // "foo/bar" ("foo", "", "bar")
43 // "/XXX" ERROR - no absolute path
44 // "foo/bar/" ERROR - no name
45 // "foo/xxx/bar" ("foo", "xxx", "bar")
46 // "foo/xxx/yyy/bar" ("foo", "xxx/yyy", "bar")
48 (
49  const fileName& path,
50  fileName& instance,
51  fileName& local,
52  word& name
53 )
54 {
55  instance.clear();
56  local.clear();
57  name.clear();
58 
59  // called with directory
60  if (isDir(path))
61  {
62  WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
63  << " called with directory: " << path << "\n";
64  return false;
65  }
66 
67  string::size_type first = path.find('/');
68 
69  if (first == 0)
70  {
71  // called with absolute path
72  WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
73  << "called with absolute path: " << path << "\n";
74  return false;
75  }
76 
77  if (first == string::npos)
78  {
79  // no '/' found - no instance or local
80 
81  // check afterwards
82  name.string::operator=(path);
83  }
84  else
85  {
86  instance = path.substr(0, first);
87 
88  string::size_type last = path.rfind('/');
89  if (last > first)
90  {
91  // with local
92  local = path.substr(first+1, last-first-1);
93  }
94 
95  // check afterwards
96  name.string::operator=(path.substr(last+1));
97  }
98 
99 
100  // check for valid (and stripped) name, regardless of the debug level
101  if (name.empty() || string::stripInvalid<word>(name))
102  {
103  WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
104  << "has invalid word for name: \"" << name
105  << "\"\nwhile processing path: " << path << "\n";
106  return false;
107  }
108 
109  return true;
110 }
111 
112 
113 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114 
116 (
117  const word& name,
118  const fileName& instance,
119  const objectRegistry& registry,
120  readOption ro,
121  writeOption wo,
122  bool registerObject
123 )
124 :
125  name_(name),
126  headerClassName_(typeName),
127  note_(),
128  instance_(instance),
129  local_(),
130  db_(registry),
131  rOpt_(ro),
132  wOpt_(wo),
133  registerObject_(registerObject),
134  objState_(GOOD)
135 {
136  if (objectRegistry::debug)
137  {
138  Info<< "Constructing IOobject called " << name_
139  << " of type " << headerClassName_
140  << endl;
141  }
142 }
143 
144 
146 (
147  const word& name,
148  const fileName& instance,
149  const fileName& local,
150  const objectRegistry& registry,
151  readOption ro,
152  writeOption wo,
153  bool registerObject
154 )
155 :
156  name_(name),
157  headerClassName_(typeName),
158  note_(),
159  instance_(instance),
160  local_(local),
161  db_(registry),
162  rOpt_(ro),
163  wOpt_(wo),
164  registerObject_(registerObject),
165  objState_(GOOD)
166 {
167  if (objectRegistry::debug)
168  {
169  Info<< "Constructing IOobject called " << name_
170  << " of type " << headerClassName_
171  << endl;
172  }
173 }
174 
175 
177 (
178  const fileName& path,
179  const objectRegistry& registry,
180  readOption ro,
181  writeOption wo,
182  bool registerObject
183 )
184 :
185  name_(),
186  headerClassName_(typeName),
187  note_(),
188  instance_(),
189  local_(),
190  db_(registry),
191  rOpt_(ro),
192  wOpt_(wo),
193  registerObject_(registerObject),
194  objState_(GOOD)
195 {
196  if (!fileNameComponents(path, instance_, local_, name_))
197  {
199  (
200  "IOobject::IOobject" "(const fileName&, const objectRegistry&, ...)"
201  )
202  << " invalid path specification\n"
203  << exit(FatalError);
204  }
205 
206  if (objectRegistry::debug)
207  {
208  Info<< "Constructing IOobject called " << name_
209  << " of type " << headerClassName_
210  << endl;
211  }
212 }
213 
214 
215 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
216 
218 {}
219 
220 
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222 
224 {
225  return db_;
226 }
227 
228 
230 {
231  return db_.time();
232 }
233 
234 
236 {
237  return time().caseName();
238 }
239 
240 
242 {
243  return time().rootPath();
244 }
245 
246 
248 {
249  return rootPath()/caseName()/instance()/db_.dbDir()/local();
250 }
251 
252 
254 (
255  const word& instance,
256  const fileName& local
257 ) const
258 {
259  return rootPath()/caseName()/instance/db_.dbDir()/local;
260 }
261 
262 
264 {
265  fileName path = this->path();
266  fileName objectPath = path/name();
267 
268  if (isFile(objectPath))
269  {
270  return objectPath;
271  }
272  else
273  {
274  if
275  (
276  time().processorCase()
277  && (
278  instance() == time().system()
279  || instance() == time().constant()
280  )
281  )
282  {
283  fileName parentObjectPath =
284  rootPath()/caseName()
285  /".."/instance()/db_.dbDir()/local()/name();
286 
287  if (isFile(parentObjectPath))
288  {
289  return parentObjectPath;
290  }
291  }
292 
293  if (!isDir(path))
294  {
295  word newInstancePath = time().findInstancePath(instant(instance()));
296 
297  if (newInstancePath.size())
298  {
299  fileName fName
300  (
301  rootPath()/caseName()
302  /newInstancePath/db_.dbDir()/local()/name()
303  );
304 
305  if (isFile(fName))
306  {
307  return fName;
308  }
309  }
310  }
311  }
312 
313  return fileName::null;
314 }
315 
316 
318 {
319  fileName fName = filePath();
320 
321  if (fName.size())
322  {
323  IFstream* isPtr = new IFstream(fName);
324 
325  if (isPtr->good())
326  {
327  return isPtr;
328  }
329  else
330  {
331  delete isPtr;
332  return NULL;
333  }
334  }
335  else
336  {
337  return NULL;
338  }
339 }
340 
341 
343 {
344  bool ok = true;
345 
346  Istream* isPtr = objectStream();
347 
348  // If the stream has failed return
349  if (!isPtr)
350  {
351  if (objectRegistry::debug)
352  {
353  Info
354  << "IOobject::headerOk() : "
355  << "file " << objectPath() << " could not be opened"
356  << endl;
357  }
358 
359  ok = false;
360  }
361  else
362  {
363  // Try reading header
364  if (!readHeader(*isPtr))
365  {
366  if (objectRegistry::debug)
367  {
368  IOWarningIn("IOobject::headerOk()", (*isPtr))
369  << "failed to read header of file " << objectPath()
370  << endl;
371  }
372 
373  ok = false;
374  }
375  }
376 
377  delete isPtr;
378 
379  return ok;
380 }
381 
382 
383 void Foam::IOobject::setBad(const string& s)
384 {
385  if (objState_ != GOOD)
386  {
387  FatalErrorIn("IOobject::setBad(const string&)")
388  << "recurrent failure for object " << s
389  << exit(FatalError);
390  }
391 
392  if (error::level)
393  {
394  Info<< "IOobject::setBad(const string&) : "
395  << "broken object " << s << info() << endl;
396  }
397 
398  objState_ = BAD;
399 }
400 
401 const char* Foam::IOobject::getBannerString(bool noHint)
402 {
403  static bool bannerSet = false;
404  static char spaces[50];
405 
406  static string bannerWithHint;
407  static string bannerNoHint;
408 
409  if (!bannerSet)
410  {
411  memset(spaces, ' ', 50);
412  size_t len = strlen(Foam::FOAMfullVersion);
413  if (len < 48)
414  {
415  spaces[48 - len] = '\0';
416  }
417  else
418  {
419  spaces[0] = '\0';
420  }
421  bannerWithHint =
422  "| ______ _ ____ __ __ |\n"
423  "| | ____| _| |_ / __ \\ /\\ | \\/ | |\n"
424  "| | |__ _ __ ___ ___ / \\| | | | / \\ | \\ / | |\n"
425  "| | __| '__/ _ \\/ _ ( (| |) ) | | |/ /\\ \\ | |\\/| | |\n"
426  "| | | | | | __/ __/\\_ _/| |__| / ____ \\| | | | |\n"
427  "| |_| |_| \\___|\\___| |_| \\____/_/ \\_\\_| |_| |\n"
428  "| |\n"
429  "| FreeFOAM: The Cross-Platform CFD Toolkit |\n"
430  "| Version: " + string(FOAMfullVersion) + spaces + "|\n"
431  "| Web: http://freefoam.sourceforge.net |\n"
432  "\\*---------------------------------------------------------------------------*/\n";
433  bannerNoHint =
434  "/*---------------------------------------------------------------------------*\\\n"
435  + bannerWithHint;
436  bannerWithHint =
437  "/*-------------------*- FOAMDict -*-- vim: set ft=foamdict: -----------------*\\\n"
438  + bannerWithHint;
439  bannerSet = true;
440  }
441 
442  if (noHint)
443  {
444  return bannerNoHint.c_str();
445  }
446  else
447  {
448  return bannerWithHint.c_str();
449  }
450 
451 }
452 
453 
455 {
456  name_ = io.name_;
457  headerClassName_ = io.headerClassName_;
458  note_ = io.note_;
459  instance_ = io.instance_;
460  local_ = io.local_;
461  rOpt_ = io.rOpt_;
462  wOpt_ = io.wOpt_;
463  objState_ = io.objState_;
464 }
465 
466 
467 // ************************ vim: set sw=4 sts=4 et: ************************ //