ensightReadFile.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2022 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 \*---------------------------------------------------------------------------*/
27 
28 #include "ensightReadFile.H"
29 
30 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31 
34 {
36 
37  // Detect BINARY vs ASCII by testing for initial "(C|Fortran) Binary"
38  {
39  IFstream ifs(pathname, IOstreamOption::BINARY);
40 
41  if (!ifs.good())
42  {
44  << "Cannot read file " << ifs.name() << nl
45  << exit(FatalError);
46  }
47 
48  istream& iss = ifs.stdStream();
49 
50  // Binary string is *exactly* 80 characters
51  string buf(size_t(80), '\0');
52  iss.read(&buf[0], 80);
53 
54  if (!iss)
55  {
56  // Truncated?
57  buf.erase(iss.gcount());
58  }
59 
60  // Truncate at the first embedded '\0'
61  const auto endp = buf.find('\0');
62  if (endp != std::string::npos)
63  {
64  buf.erase(endp);
65  }
66 
67  // ASCII if it does not contain "C Binary"
68  if (!buf.contains("Binary") && !buf.contains("binary"))
69  {
71  }
72  }
73 
74  return fmt;
75 }
76 
77 
78 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 
81 (
82  const fileName& pathname
83 )
84 :
85  IFstream(pathname, ensightReadFile::detectBinaryHeader(pathname))
86 {}
87 
88 
90 (
91  const fileName& pathname,
93 )
94 :
95  IFstream(pathname, fmt)
96 {}
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 (
103  char* buf,
104  std::streamsize count
105 )
106 {
107  stdStream().read(buf, count);
108  syncState();
109  return *this;
110 }
111 
112 
114 {
116  {
117  auto& iss = stdStream();
118 
119  // Binary string is *exactly* 80 characters
120  value.resize(80, '\0');
121  iss.read(&value[0], 80);
122 
123  syncState();
124 
125  if (!iss)
126  {
127  // Truncated - could also exit here, but no real advantage
128  value.erase(iss.gcount());
129  }
130 
131  // Truncate at the first embedded '\0'
132  auto endp = value.find('\0');
133 
134  if (endp != std::string::npos)
135  {
136  value.erase(endp);
137  }
138 
139  // May have been padded with trailing spaces - remove those
140  endp = value.find_last_not_of(" \t\f\v\n\r");
141 
142  if (endp != std::string::npos)
143  {
144  value.erase(endp + 1);
145  }
146  }
147  else
148  {
149  value.clear();
150  while (value.empty() && !eof())
151  {
152  getLine(value);
153  }
154  }
155 
156  return *this;
157 }
158 
159 
161 {
162  int ivalue;
163 
165  {
166  read
167  (
168  reinterpret_cast<char*>(&ivalue),
169  sizeof(ivalue)
170  );
171  }
172  else
173  {
174  stdStream() >> ivalue;
175  syncState();
176  }
177 
178  value = ivalue;
179  return *this;
180 }
181 
182 
184 {
186  {
187  read
188  (
189  reinterpret_cast<char*>(&value),
190  sizeof(value)
191  );
192  }
193  else
194  {
195  stdStream() >> value;
196  syncState();
197  }
198 
199  return *this;
200 }
201 
202 
204 {
205  float fvalue;
206 
208  {
209  read
210  (
211  reinterpret_cast<char*>(&fvalue),
212  sizeof(fvalue)
213  );
214  }
215  else
216  {
217  stdStream() >> fvalue;
218  syncState();
219  }
220 
221  value = fvalue;
222  return *this;
223 }
224 
225 
227 {
228  read(key);
229  return *this;
230 }
231 
232 
234 {
236  {
237  string buffer;
238  read(buffer);
239  }
240 
241  return *this;
242 }
243 
244 
245 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:71
ensightReadFile(const ensightReadFile &)=delete
No copy construct.
bool contains(char c) const noexcept
True if string contains given character (cf. C++23)
Definition: string.H:390
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
"ascii" (normal default)
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:87
static IOstreamOption::streamFormat detectBinaryHeader(const fileName &pathname)
Detect if the file is binary by testing for initial "(C|Fortran) Binary".
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:125
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.
A variant of IFstream with specialised read() for strings, integers and floats. Correctly handles bin...
Istream & readBinaryHeader()
Read "C Binary" for binary files (eg, geometry/measured)
Input from file stream, using an ISstream.
Definition: IFstream.H:49
virtual Istream & readKeyword(string &key)
Read element keyword.
word format(conversionProperties.get< word >("format"))
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:136
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
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:274
virtual Istream & read(char *buf, std::streamsize count)
Binary read.
streamFormat
Data format (ascii | binary)