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-2012 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  Info<< "Time::findTimes(const fileName&): finding times in directory "
46  << directory << endl;
47  }
48 
49  // Read directory entries into a list
50  fileNameList dirEntries(readDir(directory, fileName::DIRECTORY));
51 
52  // Initialise instant list
53  instantList Times(dirEntries.size() + 1);
54  label nTimes = 0;
55 
56  // Check for "constant"
57  bool haveConstant = false;
58  forAll(dirEntries, i)
59  {
60  if (dirEntries[i] == constantName)
61  {
62  Times[nTimes].value() = 0;
63  Times[nTimes].name() = dirEntries[i];
64  nTimes++;
65  haveConstant = true;
66  break;
67  }
68  }
69 
70  // Read and parse all the entries in the directory
71  forAll(dirEntries, i)
72  {
73  IStringStream timeStream(dirEntries[i]);
74  token timeToken(timeStream);
75 
76  if (timeToken.isNumber() && timeStream.eof())
77  {
78  Times[nTimes].value() = timeToken.number();
79  Times[nTimes].name() = dirEntries[i];
80  nTimes++;
81  }
82  }
83 
84  // Reset the length of the times list
85  Times.setSize(nTimes);
86 
87  if (haveConstant)
88  {
89  if (nTimes > 2)
90  {
91  std::sort(&Times[1], Times.end(), instant::less());
92  }
93  }
94  else if (nTimes > 1)
95  {
96  std::sort(&Times[0], Times.end(), instant::less());
97  }
98 
99  return Times;
100 }
101 
102 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::token::isNumber
bool isNumber() const
Definition: tokenI.H:340
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
Time.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
IStringStream.H
Foam::token::number
scalar number() const
Definition: tokenI.H:345
Foam::label
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
Foam::Info
messageStream Info
Foam::instant::less
Less function class used in sorting instants.
Definition: instant.H:76
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::Time::findTimes
static instantList findTimes(const fileName &, const word &constantName="constant")
Search a given directory for valid time directories.
Definition: findTimes.C:38
Foam::sort
void sort(UList< T > &)
Definition: UList.C:107
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::readDir
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:660