ensightReadFile.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::ensightReadFile
28 
29 Description
30  A variant of IFstream with specialised handling for Ensight reading
31  of strings, integers and floats (ASCII and BINARY).
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_ensightReadFile_H
36 #define Foam_ensightReadFile_H
37 
38 #include "IFstream.H"
39 #include "IOstream.H"
40 #include "vector.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class ensightReadFile Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 class ensightReadFile
52 :
53  public IFstream
54 {
55  // Private Data
56 
57  //- Transient single-file:
58  //- beginning of time-step footer
59  //- as read from the file
60  int64_t timeStepFooterBegin_;
61 
62  //- Transient single-file:
63  //- the time-step file-offsets (position after "BEGIN TIME STEP")
64  //- as read from the file
65  List<int64_t> timeStepOffsets_;
66 
67 
68  // Private Member Functions
69 
70  //- Read string as "%80s" or as binary
71  void readString(std::string& value);
72 
73  //- Initializes read information
74  //- (optional detection of "C Binary" header)
75  //- and scan for transient single-file format.
76  void init(bool detectFormat);
77 
78  //- No copy construct
79  ensightReadFile(const ensightReadFile&) = delete;
80 
81  //- No copy assignment
82  void operator=(const ensightReadFile&) = delete;
83 
84 
85 public:
86 
87  //- Debug switch
88  static int debug;
89 
90 
91  // Constructors
92 
93  //- Construct a geometry reader, auto-detecting the "C Binary" header
94  //- for binary files and skipping past it.
95  explicit ensightReadFile
96  (
97  const fileName& pathname
98  );
99 
100  //- Construct from pathname, use the specified (ascii/binary) format
102  (
103  const fileName& pathname,
105  );
106 
107 
108  //- Destructor
109  ~ensightReadFile() = default;
110 
111 
112  // Static Functions
113 
114  //- Extract time step footer information (if any).
115  // \return the begin of footer position.
116  static int64_t getTimeStepFooter
117  (
118  IFstream& is,
120  List<int64_t>& offsets
121  );
122 
123 
124  // Read Functions
125 
126  //- Inherit read from Istream
127  using Istream::read;
128 
129  //- Binary read
130  virtual Istream& read(char* buf, std::streamsize count) override;
131 
132  //- Read string as "%80s" or as binary
133  virtual Istream& read(string& value) override;
134 
135  //- Read integer as "%10d" or as binary (narrowed) int
136  virtual Istream& read(label& value) override;
137 
138  //- Read floating-point as "%12.5e" or as binary
139  virtual Istream& read(float& value) override;
140 
141  //- Read floating-point as "%12.5e" or as a binary (narrowed) float
142  virtual Istream& read(double& value) override;
143 
144  //- Read element keyword. Currently the same as read(string)
145  Istream& readKeyword(string& key);
146 
147 
148  // Special Read Functions
149 
150  //- Component-wise reading of points/coordinates.
151  //- Read all x components, y components and z components.
152  void readPoints(const label nPoints, List<floatVector>& points);
153 
154  //- Component-wise reading of points/coordinates.
155  //- Reads x components, y components and z components.
156  void readPoints(const label nPoints, List<doubleVector>& points);
157 
158  //- Read and discard specified number of elements
159  template<class Type>
160  void skip(label n = 1)
161  {
162  Type dummy;
163  for (; n > 0; --n)
164  {
165  this->read(dummy);
166  }
167  }
168 
169 
170  // Transient single-file format
171 
172  //- Transient single-file:
173  //- the position of the FILE_INDEX footer
174  int64_t timeStepFooterBegin() const noexcept
175  {
176  return timeStepFooterBegin_;
177  }
178 
179  //- Transient single-file:
180  //- the number of time steps within the file
181  label nTimes() const noexcept
182  {
183  return timeStepOffsets_.size();
184  }
185 
186  //- Transient single-file:
187  //- the file-offsets for time steps within the file
188  const UList<int64_t>& timeStepOffets() const noexcept
189  {
190  return timeStepOffsets_;
191  }
192 
193  //- Transient single-file:
194  //- seek to the file position corresponding to the given time index.
195  bool seekTime(const label timeIndex);
197 
198  // Housekeeping
199 
200  //- Detect if the file is \em binary by testing for initial
201  //- "(C|Fortran) Binary"
202  FOAM_DEPRECATED_FOR(2024-05, "detected on construct")
204  detectBinaryHeader(const fileName& pathname)
205  {
206  ensightReadFile reader(pathname);
207  return reader.format();
208  }
209 };
210 
211 
212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
213 
214 } // End namespace Foam
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 #endif
219 
220 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
static IOstreamOption::streamFormat detectBinaryHeader(const fileName &pathname)
Detect if the file is binary by testing for initial "(C|Fortran) Binary".
virtual Istream & read(char *buf, std::streamsize count) override
Binary read.
A class for handling file names.
Definition: fileName.H:72
bool seekTime(const label timeIndex)
Transient single-file: seek to the file position corresponding to the given time index.
static int debug
Debug switch.
void readPoints(const label nPoints, List< floatVector > &points)
Component-wise reading of points/coordinates. Read all x components, y components and z components...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void skip(label n=1)
Read and discard specified number of elements.
A simple container for options an IOstream can normally have.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
label nTimes() const noexcept
Transient single-file: the number of time steps within the file.
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual Istream & read(token &)=0
Return next token from stream.
const pointField & points
label nPoints
A variant of IFstream with specialised handling for Ensight reading of strings, integers and floats (...
const UList< int64_t > & timeStepOffets() const noexcept
Transient single-file: the file-offsets for time steps within the file.
const direction noexcept
Definition: Scalar.H:258
static int64_t getTimeStepFooter(IFstream &is, List< int64_t > &offsets)
Extract time step footer information (if any).
Input from file stream as an ISstream, normally using std::ifstream for the actual input...
Definition: IFstream.H:51
~ensightReadFile()=default
Destructor.
Istream & readKeyword(string &key)
Read element keyword. Currently the same as read(string)
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
streamFormat
Data format (ascii | binary)
label n
int64_t timeStepFooterBegin() const noexcept
Transient single-file: the position of the FILE_INDEX footer.
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.
label timeIndex
Definition: getTimeIndex.H:24