FreeFOAM The Cross-Platform CFD Toolkit
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) 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 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 <OpenFOAM/OSspecific.H>
33 #include <OpenFOAM/IStringStream.H>
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
38 {
39  if (debug)
40  {
41  Info<< "Time::findTimes(const fileName&): finding times in directory "
42  << directory << endl;
43  }
44 
45  // Read directory entries into a list
46  fileNameList dirEntries(readDir(directory, fileName::DIRECTORY));
47 
48  // Initialise instant list
49  instantList Times(dirEntries.size() + 1);
50  label nTimes = 0;
51 
52  // Check for "constant"
53  bool haveConstant = false;
54  forAll(dirEntries, i)
55  {
56  if (dirEntries[i] == "constant")
57  {
58  Times[nTimes].value() = 0;
59  Times[nTimes].name() = dirEntries[i];
60  nTimes++;
61  haveConstant = true;
62  break;
63  }
64  }
65 
66  // Read and parse all the entries in the directory
67  forAll(dirEntries, i)
68  {
69  IStringStream timeStream(dirEntries[i]);
70  token timeToken(timeStream);
71 
72  if (timeToken.isNumber() && timeStream.eof())
73  {
74  Times[nTimes].value() = timeToken.number();
75  Times[nTimes].name() = dirEntries[i];
76  nTimes++;
77  }
78  }
79 
80  // Reset the length of the times list
81  Times.setSize(nTimes);
82 
83  if (haveConstant)
84  {
85  if (nTimes > 2)
86  {
87  std::sort(&Times[1], Times.end(), instant::less());
88  }
89  }
90  else if (nTimes > 1)
91  {
92  std::sort(&Times[0], Times.end(), instant::less());
93  }
94 
95  return Times;
96 }
97 
98 // ************************ vim: set sw=4 sts=4 et: ************************ //