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  const std::streamsize gcount = iss.gcount();
54  buf.erase(gcount <= 0 ? 0 : gcount); // Truncated?
55 
56  // Could exit on truncated input, but no real advantage
57 
58  // Truncate at the first embedded '\0'
59  const auto endp = buf.find('\0');
60  if (endp != std::string::npos)
61  {
62  buf.erase(endp);
63  }
64 
65  // ASCII if it does not contain "C Binary"
66  if (!buf.contains("Binary") && !buf.contains("binary"))
67  {
69  }
70  }
71 
72  return fmt;
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
79 (
80  const fileName& pathname
81 )
82 :
83  IFstream(pathname, ensightReadFile::detectBinaryHeader(pathname))
84 {}
85 
86 
88 (
89  const fileName& pathname,
91 )
92 :
93  IFstream(pathname, fmt)
94 {}
95 
96 
97 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
98 
100 (
101  char* buf,
102  std::streamsize count
103 )
104 {
105  stdStream().read(buf, count);
106  syncState();
107  return *this;
108 }
109 
110 
112 {
114  {
115  auto& iss = stdStream();
116 
117  // Binary string is *exactly* 80 characters
118  value.resize(80, '\0');
119  iss.read(&value[0], 80);
120  const std::streamsize gcount = iss.gcount();
121  value.erase(gcount <= 0 ? 0 : gcount); // Truncated?
122 
123  // Could exit on truncated input, but no real advantage
124 
125  syncState();
126 
127  // Truncate at the first embedded '\0'
128  auto endp = value.find('\0');
129 
130  if (endp != std::string::npos)
131  {
132  value.erase(endp);
133  }
134 
135  // May have been padded with trailing spaces - remove those
136  endp = value.find_last_not_of(" \t\f\v\n\r");
137 
138  if (endp != std::string::npos)
139  {
140  value.erase(endp + 1);
141  }
142  }
143  else
144  {
145  value.clear();
146  while (value.empty() && !eof())
147  {
148  getLine(value);
149  }
150  }
151 
152  return *this;
153 }
154 
155 
157 {
158  int ivalue;
159 
161  {
162  read
163  (
164  reinterpret_cast<char*>(&ivalue),
165  sizeof(ivalue)
166  );
167  }
168  else
169  {
170  stdStream() >> ivalue;
171  syncState();
172  }
173 
174  value = ivalue;
175  return *this;
176 }
177 
178 
180 {
182  {
183  read
184  (
185  reinterpret_cast<char*>(&value),
186  sizeof(value)
187  );
188  }
189  else
190  {
191  stdStream() >> value;
192  syncState();
193  }
194 
195  return *this;
196 }
197 
198 
200 {
201  float fvalue;
202 
204  {
205  read
206  (
207  reinterpret_cast<char*>(&fvalue),
208  sizeof(fvalue)
209  );
210  }
211  else
212  {
213  stdStream() >> fvalue;
214  syncState();
215  }
216 
217  value = fvalue;
218  return *this;
219 }
220 
221 
223 {
224  read(key);
225  return *this;
226 }
227 
228 
230 {
232  {
233  string buffer;
234  read(buffer);
235  }
236 
237  return *this;
238 }
239 
240 
241 // ************************************************************************* //
virtual Istream & read(char *buf, std::streamsize count) override
Binary read.
A class for handling file names.
Definition: fileName.H:72
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:411
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:598
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:50
"ascii" (normal default)
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:127
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition: ISstream.H:141
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)
virtual std::istream & stdStream() override
Access to underlying std::istream.
Definition: IFstream.C:126
Input from file stream, using an ISstream.
Definition: IFstream.H:49
Istream & readKeyword(string &key)
Read element keyword. Currently the same as read(string)
word format(conversionProperties.get< word >("format"))
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:281
streamFormat
Data format (ascii | binary)