findInstance.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) 2011-2017 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 Description
25  If "name" is empty: return the location of "directory"
26  If "name" is not empty: return the location of "directory" containing the
27  file "name".
28  Used in reading mesh data.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "Time.H"
33 #include "IOobject.H"
34 #include "IOList.H"
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
39 {
40  // Generate filename for object
41  fileName objPath(fileHandler().objectPath(io, word::null));
42 
43  // Test for either directory or a (valid) file & IOobject
44  bool ok;
45  if (io.name().empty())
46  {
47  ok = fileHandler().isDir(objPath);
48  }
49  else
50  {
51  ok =
52  fileHandler().isFile(objPath)
53  && io.typeHeaderOk<IOList<label>>(false);// object with local scope
54  }
55 
56  if (!ok)
57  {
58  // Re-test with raw objectPath. This is for backwards
59  // compatibility
60  fileName originalPath(io.objectPath());
61  if (originalPath != objPath)
62  {
63  // Test for either directory or a (valid) file & IOobject
64  if (io.name().empty())
65  {
66  ok = fileHandler().isDir(originalPath);
67  }
68  else
69  {
70  ok =
71  fileHandler().isFile(originalPath)
72  && io.typeHeaderOk<IOList<label>>(false);
73  }
74  }
75  }
76 
77  return ok;
78 }
79 
80 
82 (
83  const fileName& dir,
84  const word& name,
85  const IOobject::readOption rOpt,
86  const word& stopInstance
87 ) const
88 {
89  // Note: - if name is empty, just check the directory itself
90  // - check both for isFile and headerOk since the latter does a
91  // filePath so searches for the file.
92  // - check for an object with local file scope (so no looking up in
93  // parent directory in case of parallel)
94 
95  {
96  IOobject io
97  (
98  name, // name might be empty!
99  timeName(),
100  dir,
101  *this
102  );
103 
104  if (exists(io))
105  {
106  if (debug)
107  {
109  << "Found exact match for \"" << name
110  << "\" in " << timeName()/dir
111  << endl;
112  }
113 
114  return timeName();
115  }
116  }
117 
118  // Search back through the time directories to find the time
119  // closest to and lower than current time
120 
121  instantList ts = times();
122  label instanceI;
123 
124  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
125  {
126  if (ts[instanceI].value() <= timeOutputValue())
127  {
128  break;
129  }
130  }
131 
132  // continue searching from here
133  for (; instanceI >= 0; --instanceI)
134  {
135  IOobject io
136  (
137  name, // name might be empty!
138  ts[instanceI].name(),
139  dir,
140  *this
141  );
142 
143  if (exists(io))
144  {
145  if (debug)
146  {
148  << "Found instance match for \"" << name
149  << "\" in " << ts[instanceI].name()/dir
150  << endl;
151  }
152 
153  return ts[instanceI].name();
154  }
155 
156  // Check if hit minimum instance
157  if (ts[instanceI].name() == stopInstance)
158  {
159  if (debug)
160  {
161  //InfoInFunction
162  Pout<< "findInstance : "
163  << "Hit stopInstance " << stopInstance
164  << endl;
165  }
166 
167  if
168  (
169  rOpt == IOobject::MUST_READ
171  )
172  {
173  if (name.empty())
174  {
176  << "Cannot find directory "
177  << dir << " in times " << timeName()
178  << " down to " << stopInstance
179  << exit(FatalError);
180  }
181  else
182  {
184  << "Cannot find file \"" << name << "\" in directory "
185  << dir << " in times " << timeName()
186  << " down to " << stopInstance
187  << exit(FatalError);
188  }
189  }
190 
191  return ts[instanceI].name();
192  }
193  }
194 
195 
196  // not in any of the time directories, try constant
197 
198  // Note. This needs to be a hard-coded constant, rather than the
199  // constant function of the time, because the latter points to
200  // the case constant directory in parallel cases
201 
202  IOobject io
203  (
204  name,
205  constant(),
206  dir,
207  *this
208  );
209 
210  if (exists(io))
211  {
212  if (debug)
213  {
215  << "Found constant match for \"" << name
216  << "\" in " << constant()/dir
217  << endl;
218  }
219 
220  return constant();
221  }
222 
224  {
226  << "Cannot find file \"" << name << "\" in directory "
227  << dir << " in times " << timeName()
228  << " down to " << constant()
229  << exit(FatalError);
230  }
231 
232  return constant();
233 }
234 
235 
236 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:291
bool typeHeaderOk(const bool checkType=true)
Read header (uses typeFilePath to find file) and check header.
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: findInstance.C:82
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
virtual bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist as a FILE in the file system?
virtual word timeName() const
Return current time name.
Definition: Time.C:654
A class for handling words, derived from string.
Definition: word.H:59
static bool exists(IOobject &)
Find IOobject in the objectPath.
Definition: findInstance.C:38
const word & constant() const
Return constant name.
Definition: TimePaths.H:124
const scalar & value() const
Return const reference to value.
static const word null
An empty word.
Definition: word.H:77
const fileOperation & fileHandler()
Get current file handler.
const word & name() const
Return const reference to name.
scalar timeOutputValue() const
Return current time value.
Definition: TimeStateI.H:29
virtual bool isDir(const fileName &, const bool followLink=true) const =0
Does the name exist as a DIRECTORY in the file system?
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
instantList times() const
Search the case for valid time directories.
Definition: Time.C:660
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:412
#define InfoInFunction
Report an information message using Foam::Info.