ISstream.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) 2011-2012 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::ISstream
29 
30 Description
31  Generic input stream using a standard (STL) stream.
32 
33 SourceFiles
34  ISstreamI.H
35  ISstream.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_ISstream_H
40 #define Foam_ISstream_H
41 
42 #include "Istream.H"
43 #include "fileName.H"
44 #include <limits>
45 #include <iostream>
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class ISstream Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class ISstream
57 :
58  public Istream
59 {
60  // Private Data
61 
62  fileName name_;
63 
64  std::istream& is_;
65 
66 
67  // Private Member Functions
68 
69  //- Get the next valid (non-whitespace) character,
70  //- after skipping any C/C++ comments.
71  char nextValid();
72 
73  //- Read into compound token (assumed to be a known type)
74  virtual bool readCompoundToken(token& tok, const word& compoundType);
75 
76  //- No copy assignment
77  void operator=(const ISstream&) = delete;
78 
79 
80 public:
81 
82  // Constructors
83 
84  //- Construct wrapper around std::istream, set stream status
85  // Default stream options (ASCII, uncompressed)
86  inline ISstream
87  (
88  std::istream& is,
89  const string& streamName,
90  IOstreamOption streamOpt = IOstreamOption()
91  );
92 
93  //- Construct wrapper around std::istream, set stream status
94  ISstream
95  (
96  std::istream& is,
97  const string& streamName,
100  )
101  :
102  ISstream(is, streamName, IOstreamOption(fmt, cmp))
103  {}
104 
105  //- Construct wrapper around std::istream, set stream status
106  ISstream
107  (
108  std::istream& is,
109  const string& streamName,
113  )
114  :
115  ISstream(is, streamName, IOstreamOption(fmt, ver, cmp))
116  {}
117 
118 
119  //- Destructor
120  virtual ~ISstream() = default;
121 
122 
123  // Member Functions
124 
125  // Characteristics
126 
127  //- The name of the input serial stream.
128  //- (eg, the name of the Fstream file name)
129  virtual const fileName& name() const override { return name_; }
130 
131  //- The name of the input serial stream, for modification.
132  virtual fileName& name() { return name_; }
133 
134 
135  // STL stream
136 
137  //- Const access to underlying std::istream
138  virtual const std::istream& stdStream() const { return is_; }
139 
140  //- Access to underlying std::istream
141  virtual std::istream& stdStream() { return is_; }
142 
143 
144  // Stream State
145 
146  //- Return flags of output stream
147  virtual ios_base::fmtflags flags() const override
148  {
149  return is_.flags();
150  }
151 
152  //- Set stream flags
153  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override
154  {
155  return is_.flags(f);
156  }
157 
158  //- Set stream state to match that of the std::istream
159  void syncState()
160  {
161  setState(is_.rdstate());
162  }
163 
164 
165  // Special-purpose Functions
166 
167  //- Discard until end of C-style comment '*/'
168  // \return False if stream exhausted before finding the comment end
169  bool seekCommentEnd_Cstyle();
170 
171  //- Raw, low-level get into a string.
172  //- Continues reading \b after an initial left-brace until it finds
173  //- the matching closing right-brace.
174  // Tracks balanced pairs, trims out leading/trailing whitespace.
175  //
176  // \return False if stream exhausted before finding closing brace
178  (
179  std::string& str,
180  const bool stripComments = true
181  );
182 
184  // Serial-stream functions
185 
186  //- Raw, low-level get character function.
187  inline ISstream& get(char& c);
188 
189  //- Raw, low-level peek function.
190  // Does not remove the character from the stream.
191  // Returns the next character in the stream or EOF if the
192  // end of file is read.
193  inline int peek();
194 
195  //- Raw, low-level getline (until delimiter) into a string.
196  inline ISstream& getLine(std::string& str, char delim = '\n');
197 
198  //- Low-level discard until delimiter
199  // \return the number of characters extracted
200  inline std::streamsize getLine(std::nullptr_t, char delim = '\n');
201 
202  //- Raw, low-level putback character function.
203  inline ISstream& putback(const char c);
204 
205 
206  // Read Functions
207 
208  //- Return next token from stream
209  virtual Istream& read(token& t) override;
210 
211  //- Read a character
212  virtual Istream& read(char& c) override;
213 
214  //- Read a word
215  virtual Istream& read(word& str) override;
216 
217  //- Read a string (including enclosing double-quotes).
218  // Backslashes are retained, except when escaping double-quotes
219  // and an embedded newline character.
220  virtual Istream& read(string& str) override;
221 
222  //- Read a label
223  virtual Istream& read(label& val) override;
224 
225  //- Read a float
226  virtual Istream& read(float& val) override;
227 
228  //- Read a double
229  virtual Istream& read(double& val) override;
230 
231  //- Read binary block (with any possible block delimiters).
232  //- Reading into a null pointer behaves like a forward seek of
233  //- count characters.
234  virtual Istream& read(char* data, std::streamsize count) override;
235 
236  //- Low-level raw binary read (without possible block delimiters).
237  //- Reading into a null pointer behaves like a forward seek of
238  //- count characters.
239  virtual Istream& readRaw
240  (
241  char* data,
242  std::streamsize count
243  ) override;
244 
245  //- Start of low-level raw binary read
246  virtual bool beginRawRead() override;
247 
248  //- End of low-level raw binary read
249  virtual bool endRawRead() override;
250 
251  //- Rewind the stream so that it may be read again
252  virtual void rewind() override;
253 
254 
255  // Print
256 
257  //- Print stream description to Ostream
258  virtual void print(Ostream& os) const override;
259 };
260 
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 } // End namespace Foam
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #include "ISstreamI.H"
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #endif
273 
274 // ************************************************************************* //
**return False if stream exhausted before finding the comment end *bool seekCommentEnd_Cstyle()
Discard until end of C-style comment &#39;.
Definition: ISstream.C:84
virtual Istream & readRaw(char *data, std::streamsize count) override
Low-level raw binary read (without possible block delimiters). Reading into a null pointer behaves li...
Definition: ISstream.C:1037
A class for handling file names.
Definition: fileName.H:72
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
ISstream & putback(const char c)
Raw, low-level putback character function.
Definition: ISstreamI.H:99
virtual const std::istream & stdStream() const
Const access to underlying std::istream.
Definition: ISstream.H:154
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
virtual ios_base::fmtflags flags() const override
Return flags of output stream.
Definition: ISstream.H:167
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:25
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
void syncState()
Set stream state to match that of the std::istream.
Definition: ISstream.H:183
bool continueReadUntilRightBrace(std::string &str, const bool stripComments=true)
Raw, low-level get into a string. Continues reading after an initial left-brace until it finds the ma...
Definition: ISstream.C:518
A simple container for options an IOstream can normally have.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
virtual bool endRawRead() override
End of low-level raw binary read.
Definition: ISstream.C:1070
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual ~ISstream()=default
Destructor.
A class for handling words, derived from Foam::string.
Definition: word.H:63
int peek()
Raw, low-level peek function.
Definition: ISstreamI.H:63
virtual bool beginRawRead() override
Start of low-level raw binary read.
Definition: ISstream.C:1055
virtual const fileName & name() const override
The name of the input serial stream. (eg, the name of the Fstream file name)
Definition: ISstream.H:141
Representation of a major/minor version number.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:69
OBJstream os(runTime.globalPath()/outputName)
labelList f(nPoints)
virtual Istream & read(token &t) override
Return next token from stream.
Definition: ISstream.C:535
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
const dimensionedScalar c
Speed of light in a vacuum.
streamFormat
Data format (ascii | binary)
virtual void print(Ostream &os) const override
Print stream description to Ostream.
Definition: SstreamsPrint.C:30
Namespace for OpenFOAM.