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-2016 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 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
38 (
39  const fileName& dir,
40  const word& name,
41  const IOobject::readOption rOpt,
42  const word& stopInstance
43 ) const
44 {
45  // Note: if name is empty, just check the directory itself
46 
47 
48  const fileName tPath(path());
49  const fileName dirPath(tPath/timeName()/dir);
50 
51  // check the current time directory
52  if
53  (
54  name.empty()
55  ? isDir(dirPath)
56  :
57  (
58  isFile(dirPath/name)
59  && IOobject(name, timeName(), dir, *this).headerOk()
60  )
61  )
62  {
63  if (debug)
64  {
66  << "Found \"" << name
67  << "\" in " << timeName()/dir
68  << endl;
69  }
70 
71  return timeName();
72  }
73 
74  // Search back through the time directories to find the time
75  // closest to and lower than current time
76 
77  instantList ts = times();
78  label instanceI;
79 
80  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
81  {
82  if (ts[instanceI].value() <= timeOutputValue())
83  {
84  break;
85  }
86  }
87 
88  // continue searching from here
89  for (; instanceI >= 0; --instanceI)
90  {
91  if
92  (
93  name.empty()
94  ? isDir(tPath/ts[instanceI].name()/dir)
95  :
96  (
97  isFile(tPath/ts[instanceI].name()/dir/name)
98  && IOobject(name, ts[instanceI].name(), dir, *this).headerOk()
99  )
100  )
101  {
102  if (debug)
103  {
105  << "Found \"" << name
106  << "\" in " << ts[instanceI].name()/dir
107  << endl;
108  }
109 
110  return ts[instanceI].name();
111  }
112 
113  // Check if hit minimum instance
114  if (ts[instanceI].name() == stopInstance)
115  {
116  if (debug)
117  {
119  << "Hit stopInstance " << stopInstance
120  << endl;
121  }
122 
123  if
124  (
125  rOpt == IOobject::MUST_READ
126  || rOpt == IOobject::MUST_READ_IF_MODIFIED
127  )
128  {
129  if (name.empty())
130  {
132  << "Cannot find directory "
133  << dir << " in times " << timeName()
134  << " down to " << stopInstance
135  << exit(FatalError);
136  }
137  else
138  {
140  << "Cannot find file \"" << name << "\" in directory "
141  << dir << " in times " << timeName()
142  << " down to " << stopInstance
143  << exit(FatalError);
144  }
145  }
146 
147  return ts[instanceI].name();
148  }
149  }
150 
151 
152  // not in any of the time directories, try constant
153 
154  // Note. This needs to be a hard-coded constant, rather than the
155  // constant function of the time, because the latter points to
156  // the case constant directory in parallel cases
157 
158  if
159  (
160  name.empty()
161  ? isDir(tPath/constant()/dir)
162  :
163  (
164  isFile(tPath/constant()/dir/name)
165  && IOobject(name, constant(), dir, *this).headerOk()
166  )
167  )
168  {
169  if (debug)
170  {
172  << "Found \"" << name
173  << "\" in " << constant()/dir
174  << endl;
175  }
176 
177  return constant();
178  }
179 
180  if (rOpt == IOobject::MUST_READ || rOpt == IOobject::MUST_READ_IF_MODIFIED)
181  {
183  << "Cannot find file \"" << name << "\" in directory "
184  << dir << " in times " << timeName()
185  << " down to " << constant()
186  << exit(FatalError);
187  }
188 
189  return constant();
190 }
191 
192 
193 // ************************************************************************* //
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
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:76
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
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:38
A class for handling words, derived from string.
Definition: word.H:59
word timeName
Definition: getTimeIndex.H:3
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Constant dispersed-phase particle diameter model.
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
#define InfoInFunction
Report an information message using Foam::Info.