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-2025 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 
79 public:
80 
81  //- Debug switch
82  static int debug;
83 
84 
85  // Generated Methods
86 
87  //- No copy construct
88  ensightReadFile(const ensightReadFile&) = delete;
89 
90  //- No copy assignment
91  void operator=(const ensightReadFile&) = delete;
92 
93 
94  // Constructors
95 
96  //- Construct a geometry reader, auto-detecting the "C Binary" header
97  //- for binary files and skipping past it.
98  explicit ensightReadFile
99  (
100  const fileName& pathname
101  );
102 
103  //- Construct from pathname, use the specified (ascii/binary) format
105  (
106  const fileName& pathname,
108  );
109 
110 
111  //- Destructor
112  ~ensightReadFile() = default;
113 
114 
115  // Static Functions
116 
117  //- Extract time step footer information (if any).
118  // \return the begin of footer position.
119  static int64_t getTimeStepFooter
120  (
121  IFstream& is,
123  List<int64_t>& offsets
124  );
125 
126 
127  // Read Functions
128 
129  //- Inherit read from Istream
130  using Istream::read;
131 
132  //- Binary read
133  virtual Istream& read(char* buf, std::streamsize count) override;
134 
135  //- Read string as "%80s" or as binary
136  virtual Istream& read(string& value) override;
137 
138  //- Read integer as "%10d" or as binary (narrowed) int
139  virtual Istream& read(int32_t& value) override;
140 
141  //- Read integer as "%10d" or as binary (narrowed) int
142  virtual Istream& read(int64_t& value) override;
143 
144  //- Not implemented
145  virtual Istream& read(uint32_t& value) override;
146 
147  //- Not implemented
148  virtual Istream& read(uint64_t& value) override;
149 
150  //- Read floating-point as "%12.5e" or as binary
151  virtual Istream& read(float& value) override;
152 
153  //- Read floating-point as "%12.5e" or as a binary (narrowed) float
154  virtual Istream& read(double& value) override;
155 
156  //- Read element keyword. Currently the same as read(string)
157  Istream& readKeyword(string& key);
158 
159 
160  // Special Read Functions
161 
162  //- Component-wise reading of points/coordinates.
163  //- Read all x components, y components and z components.
164  void readPoints(const label nPoints, List<floatVector>& points);
165 
166  //- Component-wise reading of points/coordinates.
167  //- Reads x components, y components and z components.
168  void readPoints(const label nPoints, List<doubleVector>& points);
169 
170  //- Read and discard specified number of elements
171  template<class Type>
172  void skip(label n = 1)
173  {
174  Type dummy;
175  for (; n > 0; --n)
176  {
177  this->read(dummy);
178  }
179  }
180 
181 
182  // Transient single-file format
183 
184  //- Transient single-file:
185  //- the position of the FILE_INDEX footer
186  int64_t timeStepFooterBegin() const noexcept
187  {
188  return timeStepFooterBegin_;
189  }
190 
191  //- Transient single-file:
192  //- the number of time steps within the file
193  label nTimes() const noexcept
194  {
195  return timeStepOffsets_.size();
196  }
197 
198  //- Transient single-file:
199  //- the file-offsets for time steps within the file
200  const UList<int64_t>& timeStepOffets() const noexcept
201  {
202  return timeStepOffsets_;
203  }
204 
205  //- Transient single-file:
206  //- seek to the file position corresponding to the given time index.
207  bool seekTime(const label timeIndex);
208 };
209 
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 } // End namespace Foam
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 #endif
218 
219 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:119
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.
ensightReadFile(const ensightReadFile &)=delete
No copy construct.
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.
void operator=(const ensightReadFile &)=delete
No copy assignment.
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: scalarImpl.H:265
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)
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
Definition: foamGltfBase.H:105
streamFormat
Data format (ascii | binary | coherent)
label n
int64_t timeStepFooterBegin() const noexcept
Transient single-file: the position of the FILE_INDEX footer.
Namespace for OpenFOAM.
label timeIndex
Definition: getTimeIndex.H:24