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-2021 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 Foam::ensightReadFile::ensightReadFile
33 (
34  const fileName& pathname,
36 )
37 :
38  IFstream(pathname, fmt)
39 {}
40 
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
45 (
46  char* buf,
47  std::streamsize count
48 )
49 {
50  stdStream().read(buf, count);
51  return *this;
52 }
53 
54 
56 {
57  if (format() == IOstream::BINARY)
58  {
59  auto& iss = stdStream();
60 
61  // Binary string is *exactly* 80 characters
62  value.resize(80, '\0');
63  iss.read(&value[0], 80);
64 
65  if (!iss)
66  {
67  // Truncated - could also exit here, but no real advantage
68  value.erase(iss.gcount());
69  }
70 
71  // Truncate at the first embedded '\0'
72  auto endp = value.find('\0');
73 
74  if (endp != std::string::npos)
75  {
76  value.erase(endp);
77  }
78 
79  // May have been padded with trailing spaces - remove those
80  endp = value.find_last_not_of(" \t\f\v\n\r");
81 
82  if (endp != std::string::npos)
83  {
84  value.erase(endp + 1);
85  }
86  }
87  else
88  {
89  value.clear();
90  while (value.empty() && !eof())
91  {
92  getLine(value);
93  }
94  }
95 
96  return *this;
97 }
98 
99 
101 {
102  int ivalue;
103 
104  if (format() == IOstream::BINARY)
105  {
106  read
107  (
108  reinterpret_cast<char*>(&ivalue),
109  sizeof(ivalue)
110  );
111  }
112  else
113  {
114  stdStream() >> ivalue;
115  }
116 
117  value = ivalue;
118  return *this;
119 }
120 
121 
123 {
124  float fvalue;
125 
126  if (format() == IOstream::BINARY)
127  {
128  read
129  (
130  reinterpret_cast<char*>(&fvalue),
131  sizeof(fvalue)
132  );
133 
134  value = fvalue;
135  }
136  else
137  {
138  stdStream() >> value;
139  }
140 
141  return *this;
142 }
143 
144 
146 {
147  read(key);
148  return *this;
149 }
150 
151 
153 {
154  if (format() == IOstream::BINARY)
155  {
156  string buffer;
157  read(buffer);
158  }
159 
160  return *this;
161 }
162 
163 
164 // ************************************************************************* //
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
format
word format(conversionProperties.get< word >("format"))
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstream::eof
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::ensightReadFile::readBinaryHeader
Istream & readBinaryHeader()
Read "C Binary" for binary files (eg, geometry/measured)
Definition: ensightReadFile.C:152
Foam::IFstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:94
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
ensightReadFile.H
Foam::ensightReadFile::readKeyword
virtual Istream & readKeyword(string &key)
Read element keyword.
Definition: ensightReadFile.C:145
Foam::ensightReadFile::read
virtual Istream & read(char *buf, std::streamsize count)
Binary read.
Definition: ensightReadFile.C:45
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.