Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::ensightReadFile
28
29Description
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
44namespace Foam
45{
46
47/*---------------------------------------------------------------------------*\
48 Class ensightReadFile Declaration
49\*---------------------------------------------------------------------------*/
50
51class 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
85public:
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
101 ensightReadFile
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 );
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(int32_t& value) override;
137
138 //- Read integer as "%10d" or as binary (narrowed) int
139 virtual Istream& read(int64_t& value) override;
140
141 //- Not implemented
142 virtual Istream& read(uint32_t& value) override;
143
144 //- Not implemented
145 virtual Istream& read(uint64_t& value) override;
146
147 //- Read floating-point as "%12.5e" or as binary
148 virtual Istream& read(float& value) override;
149
150 //- Read floating-point as "%12.5e" or as a binary (narrowed) float
151 virtual Istream& read(double& value) override;
152
153 //- Read element keyword. Currently the same as read(string)
154 Istream& readKeyword(string& key);
155
156
157 // Special Read Functions
158
159 //- Component-wise reading of points/coordinates.
160 //- Read all x components, y components and z components.
161 void readPoints(const label nPoints, List<floatVector>& points);
162
163 //- Component-wise reading of points/coordinates.
164 //- Reads x components, y components and z components.
165 void readPoints(const label nPoints, List<doubleVector>& points);
166
167 //- Read and discard specified number of elements
168 template<class Type>
169 void skip(label n = 1)
170 {
171 Type dummy;
172 for (; n > 0; --n)
173 {
174 this->read(dummy);
175 }
176 }
177
178
179 // Transient single-file format
180
181 //- Transient single-file:
182 //- the position of the FILE_INDEX footer
183 int64_t timeStepFooterBegin() const noexcept
184 {
185 return timeStepFooterBegin_;
186 }
187
188 //- Transient single-file:
189 //- the number of time steps within the file
190 label nTimes() const noexcept
191 {
192 return timeStepOffsets_.size();
193 }
194
195 //- Transient single-file:
196 //- the file-offsets for time steps within the file
197 const UList<int64_t>& timeStepOffets() const noexcept
198 {
199 return timeStepOffsets_;
200 }
201
202 //- Transient single-file:
203 //- seek to the file position corresponding to the given time index.
204 bool seekTime(const label timeIndex);
205
206
207 // Housekeeping
208
209 //- Detect if the file is \em binary by testing for initial
210 //- "(C|Fortran) Binary"
211 FOAM_DEPRECATED_FOR(2024-05, "detected on construct")
213 detectBinaryHeader(const fileName& pathname)
214 {
215 ensightReadFile reader(pathname);
216 return reader.format();
217 }
218};
219
220
221// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222
223} // End namespace Foam
224
225// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226
227#endif
228
229// ************************************************************************* //
label n
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname, default or specified stream options.
Definition IFstream.C:149
A simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
streamFormat
Data format (ascii | binary | coherent).
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
bool seekTime(const label timeIndex)
Transient single-file: seek to the file position corresponding to the given time index.
~ensightReadFile()=default
Destructor.
void skip(label n=1)
Read and discard specified number of elements.
static IOstreamOption::streamFormat detectBinaryHeader(const fileName &pathname)
Detect if the file is binary by testing for initial "(C|Fortran) Binary".
void readPoints(const label nPoints, List< floatVector > &points)
Component-wise reading of points/coordinates. Read all x components, y components and z components.
int64_t timeStepFooterBegin() const noexcept
Transient single-file: the position of the FILE_INDEX footer.
static int64_t getTimeStepFooter(IFstream &is, List< int64_t > &offsets)
Extract time step footer information (if any).
label nTimes() const noexcept
Transient single-file: the number of time steps within the file.
const UList< int64_t > & timeStepOffets() const noexcept
Transient single-file: the file-offsets for time steps within the file.
static int debug
Debug switch.
Istream & readKeyword(string &key)
Read element keyword. Currently the same as read(string).
virtual Istream & read(char *buf, std::streamsize count) override
Binary read.
A class for handling file names.
Definition fileName.H:75
const pointField & points
label nPoints
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265
label timeIndex
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43