findTimes.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  Searches the current case directory for valid times
26  and sets the time list to these.
27  This is done if a times File does not exist.
28 
29 \*---------------------------------------------------------------------------*/
30 
31 #include "Time.H"
32 #include "OSspecific.H"
33 #include "IStringStream.H"
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
38 (
39  const fileName& directory,
40  const word& constantName
41 )
42 {
43  if (debug)
44  {
45  InfoInFunction << "Finding times in directory " << directory << endl;
46  }
47 
48  // Read directory entries into a list
49  fileNameList dirEntries(readDir(directory, fileName::DIRECTORY));
50 
51  // Initialise instant list
52  instantList Times(dirEntries.size() + 1);
53  label nTimes = 0;
54 
55  // Check for "constant"
56  bool haveConstant = false;
57  forAll(dirEntries, i)
58  {
59  if (dirEntries[i] == constantName)
60  {
61  Times[nTimes].value() = 0;
62  Times[nTimes].name() = dirEntries[i];
63  nTimes++;
64  haveConstant = true;
65  break;
66  }
67  }
68 
69  // Read and parse all the entries in the directory
70  forAll(dirEntries, i)
71  {
72  IStringStream timeStream(dirEntries[i]);
73  token timeToken(timeStream);
74 
75  if (timeToken.isNumber() && timeStream.eof())
76  {
77  Times[nTimes].value() = timeToken.number();
78  Times[nTimes].name() = dirEntries[i];
79  nTimes++;
80  }
81  }
82 
83  // Reset the length of the times list
84  Times.setSize(nTimes);
85 
86  if (haveConstant)
87  {
88  if (nTimes > 2)
89  {
90  std::sort(&Times[1], Times.end(), instant::less());
91  }
92  }
93  else if (nTimes > 1)
94  {
95  std::sort(&Times[0], Times.end(), instant::less());
96  }
97 
98  return Times;
99 }
100 
101 // ************************************************************************* //
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
A token holds items read from Istream.
Definition: token.H:69
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static bool less(const vector &x, const vector &y)
To compare normals.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
void sort(UList< T > &)
Definition: UList.C:115
static instantList findTimes(const fileName &, const word &constantName="constant")
Search a given directory for valid time directories.
Definition: findTimes.C:38
void setSize(const label)
Reset size of List.
Definition: List.C:295
scalar number() const
Definition: tokenI.H:337
Input from memory buffer stream.
Definition: IStringStream.H:49
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:527
bool isNumber() const
Definition: tokenI.H:332
#define InfoInFunction
Report an information message using Foam::Info.