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  //- The input stream path
63  fileName name_;
64 
65  //- The input stream
66  std::istream& is_;
67 
68 
69  // Private Member Functions
70 
71  //- Get the next valid (non-whitespace) character,
72  //- after skipping any C/C++ comments.
73  char nextValid();
74 
75  //- Read into compound token (assumed to be a known type)
76  virtual bool readCompoundToken(token& tok, const word& compoundType);
77 
78  //- No copy assignment
79  void operator=(const ISstream&) = delete;
80 
81 
82 public:
83 
84  // Constructors
85 
86  //- Construct wrapper around std::istream, set stream status
87  // Default stream options (ASCII, uncompressed)
88  inline ISstream
89  (
90  std::istream& is,
91  const string& streamName,
92  IOstreamOption streamOpt = IOstreamOption()
93  );
94 
95  //- Construct wrapper around std::istream, set stream status
96  ISstream
97  (
98  std::istream& is,
99  const string& streamName,
102  )
103  :
104  ISstream(is, streamName, IOstreamOption(fmt, cmp))
105  {}
106 
107  //- Construct wrapper around std::istream, set stream status
108  ISstream
109  (
110  std::istream& is,
111  const string& streamName,
115  )
116  :
117  ISstream(is, streamName, IOstreamOption(fmt, ver, cmp))
118  {}
119 
120 
121  //- Destructor
122  virtual ~ISstream() = default;
123 
124 
125  // Member Functions
126 
127  // Characteristics
128 
129  //- The name of the input serial stream.
130  //- (eg, the name of the Fstream file name)
131  virtual const fileName& name() const override { return name_; }
132 
133  //- The name of the input serial stream, for modification.
134  // Use with caution since some classes (eg, Fstream)
135  // also use this for filesystem information!
136  virtual fileName& name() { return name_; }
137 
138 
139  // STL stream
140 
141  //- Const access to underlying std::istream
142  virtual const std::istream& stdStream() const { return is_; }
143 
144  //- Access to underlying std::istream
145  virtual std::istream& stdStream() { return is_; }
146 
148  // Stream State
149 
150  //- Return current stream flags
151  virtual std::ios_base::fmtflags flags() const override
152  {
153  return is_.flags();
154  }
156  //- Set stream flags, return old stream flags
157  virtual std::ios_base::fmtflags flags
158  (
159  std::ios_base::fmtflags f
160  ) override
161  {
162  return is_.flags(f);
163  }
164 
165  //- Set stream state to match that of the std::istream
166  void syncState()
167  {
168  setState(is_.rdstate());
169  }
170 
171 
172  // Special-purpose Functions
173 
174  //- Discard until end of C-style comment '*/'
175  // \return False if stream exhausted before finding the comment end
177 
178  //- Raw, low-level get into a string.
179  //- Continues reading \b after an initial left-brace until it finds
180  //- the matching closing right-brace.
181  // Tracks balanced pairs, trims out leading/trailing whitespace.
182  //
183  // \return False if stream exhausted before finding closing brace
185  (
186  std::string& str,
187  const bool stripComments = true
188  );
189 
190 
191  // Serial-stream functions
192 
193  //- Raw, low-level get character function.
194  inline ISstream& get(char& c);
196  //- Raw, low-level peek function.
197  // Does not remove the character from the stream.
198  // Returns the next character in the stream or EOF if the
199  // end of file is read.
200  inline int peek();
201 
202  //- Raw, low-level getline (until delimiter) into a string.
203  inline ISstream& getLine(std::string& str, char delim = '\n');
204 
205  //- Low-level discard until delimiter
206  // \return the number of characters extracted
207  inline std::streamsize getLine(std::nullptr_t, char delim = '\n');
208 
209  //- Raw, low-level putback character function.
210  inline ISstream& putback(const char c);
211 
212 
213  // Read Functions
214 
215  //- Return next token from stream
216  virtual Istream& read(token& t) override;
217 
218  //- Read a character
219  virtual Istream& read(char& c) override;
220 
221  //- Read a word
222  virtual Istream& read(word& str) override;
223 
224  //- Read a string (including enclosing double-quotes).
225  // Backslashes are retained, except when escaping double-quotes
226  // and an embedded newline character.
227  virtual Istream& read(string& str) override;
228 
229  //- Read a label
230  virtual Istream& read(label& val) override;
231 
232  //- Read a float
233  virtual Istream& read(float& val) override;
234 
235  //- Read a double
236  virtual Istream& read(double& val) override;
237 
238  //- Read binary block (with any possible block delimiters).
239  //- Reading into a null pointer behaves like a forward seek of
240  //- count characters.
241  virtual Istream& read(char* data, std::streamsize count) override;
242 
243  //- Low-level raw binary read (without possible block delimiters).
244  //- Reading into a null pointer behaves like a forward seek of
245  //- count characters.
246  virtual Istream& readRaw
247  (
248  char* data,
249  std::streamsize count
250  ) override;
251 
252  //- Start of low-level raw binary read
253  virtual bool beginRawRead() override;
254 
255  //- End of low-level raw binary read
256  virtual bool endRawRead() override;
257 
258  //- Rewind the stream so that it may be read again
259  virtual void rewind() override;
260 
261 
262  // Print
263 
264  //- Print stream description to Ostream
265  virtual void print(Ostream& os) const override;
266 };
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 } // End namespace Foam
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #include "ISstreamI.H"
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 #endif
280 
281 // ************************************************************************* //
**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:163
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:25
virtual std::ios_base::fmtflags flags() const override
Return current stream flags.
Definition: ISstream.H:176
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:195
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:147
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.