ITstream.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-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 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::ITstream
29 
30 Description
31  An input stream of tokens.
32 
33 SourceFiles
34  ITstream.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef ITstream_H
39 #define ITstream_H
40 
41 #include "Istream.H"
42 #include "tokenList.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 class ISstream;
51 
52 /*---------------------------------------------------------------------------*\
53  Class ITstream Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class ITstream
57 :
58  public Istream,
59  public tokenList
60 {
61  // Private Data
62 
63  //- Name associated with the stream
64  fileName name_;
65 
66  //- Index of token currently being read
67  label tokenIndex_;
68 
69 
70  // Private Member Functions
71 
72  //- Convert input sequence into a list of tokens.
73  // \return the number of tokens in the resulting list.
74  static label parseStream(ISstream& input, tokenList& tokens);
75 
76  //- An ad hoc combination of reserve and setCapacity somewhat
77  //- similar to DynamicList.
78  //
79  // In lazy mode, increase list size if needed, but leave any
80  // excess capacity - works like reserve.
81  //
82  // In non-lazy mode, set exact capacity
83  void reserveCapacity(const label nElem, const bool lazy);
84 
85 
86 public:
87 
88  // Constructors
89 
90  //- Copy construct
91  ITstream(const ITstream& is)
92  :
94  tokenList(is),
95  name_(is.name_),
96  tokenIndex_(0)
97  {
98  setOpened();
99  setGood();
100  }
101 
102  //- Construct empty stream with given name
103  explicit ITstream
104  (
105  const string& name,
106  IOstreamOption streamOpt = IOstreamOption()
107  )
108  :
109  Istream(streamOpt),
110  tokenList(),
111  name_(name),
112  tokenIndex_(0)
113  {
114  setOpened();
115  setGood();
116  }
117 
118  //- Construct from components
120  (
121  const string& name,
122  const UList<token>& tokens,
125  )
126  :
128  tokenList(tokens),
129  name_(name),
130  tokenIndex_(0)
131  {
132  setOpened();
133  setGood();
134  }
135 
136  //- Construct from components, transferring the tokens
138  (
139  const string& name,
140  List<token>&& tokens,
143  )
144  :
146  tokenList(std::move(tokens)),
147  name_(name),
148  tokenIndex_(0)
149  {
150  setOpened();
151  setGood();
152  }
153 
154  //- Construct token list by parsing the input character sequence
155  // Uses UIListStream internally.
156  ITstream
157  (
158  const string& name,
159  const UList<char>& input,
161  versionNumber version=currentVersion
162  );
163 
164  //- Construct token list by parsing the input string
165  // Uses UIListStream internally.
166  ITstream
167  (
168  const string& name,
169  const std::string& input,
171  versionNumber version=currentVersion
172  );
173 
174  //- Construct token list by parsing the input character sequence
175  // Uses UIListStream internally.
176  ITstream
177  (
178  const string& name,
179  const char* input,
181  versionNumber version=currentVersion
182  );
183 
184 
185  //- Destructor
186  virtual ~ITstream() = default;
187 
188 
189  // Static Functions
190 
191  //- Create token list by parsing the input character sequence until
192  //- no good tokens remain.
193  static tokenList parse
194  (
195  const UList<char>& input,
197  );
198 
199  //- Create token list by parsing the input string until
200  //- no good tokens remain.
201  static tokenList parse
202  (
203  const std::string& input,
205  );
206 
207  //- Create token list by parsing the input character sequence until
208  //- no good tokens remain.
209  static tokenList parse
210  (
211  const char* input,
213  );
214 
215 
216  // Member Functions
217 
218  // Inquiry
219 
220  //- Return the name of the stream
221  virtual const fileName& name() const
222  {
223  return name_;
224  }
225 
226  //- Return non-const access to the name of the stream
227  virtual fileName& name()
228  {
229  return name_;
230  }
231 
232  //- The current token index when reading, or the insertion point.
233  label tokenIndex() const
234  {
235  return tokenIndex_;
236  }
237 
238  //- Non-const access to the current token index
239  label& tokenIndex()
240  {
241  return tokenIndex_;
242  }
243 
244  //- The number of remaining tokens
245  label nRemainingTokens() const
246  {
247  return size() - tokenIndex_;
248  }
249 
250  //- Return flags of output stream
251  ios_base::fmtflags flags() const
252  {
253  return ios_base::fmtflags(0);
254  }
255 
256 
257  // Read Functions
258 
259  //- Return next token from stream
260  virtual Istream& read(token& tok);
261 
262  //- Read a character
263  virtual Istream& read(char&);
264 
265  //- Read a word
266  virtual Istream& read(word&);
267 
268  // Read a string (including enclosing double-quotes)
269  virtual Istream& read(string&);
270 
271  //- Read a label
272  virtual Istream& read(label&);
273 
274  //- Read a floatScalar
275  virtual Istream& read(floatScalar&);
276 
277  //- Read a doubleScalar
278  virtual Istream& read(doubleScalar&);
279 
280  //- Read binary block
281  // \note Not implemented
282  virtual Istream& read(char* data, std::streamsize);
283 
284  //- Low-level raw binary read
285  // \note Not implemented
286  virtual Istream& readRaw(char* data, std::streamsize count);
287 
288  //- Start of low-level raw binary read
289  virtual bool beginRawRead()
290  {
291  return false;
292  }
293 
294  //- End of low-level raw binary read
295  virtual bool endRawRead()
296  {
297  return false;
298  }
299 
300  //- Rewind the stream so that it may be read again
301  virtual void rewind();
302 
303  //- Move the tokenIndex to the specified position.
304  // Using seek(0) is identical to rewind.
305  // Using seek(-1) moves to the end.
306  void seek(label pos);
307 
308 
309  // Edit
310 
311  //- Copy append a token at the current tokenIndex,
312  //- incrementing the index.
313  void append(const token& t, const bool lazy);
314 
315  //- Move append a token at the current tokenIndex,
316  //- incrementing the index.
317  void append(token&& t, const bool lazy);
318 
319  //- Copy append a list of tokens at the current tokenIndex,
320  //- incrementing the index.
321  //
322  // \param newTokens the list of tokens to copy append
323  // \param lazy leaves any excess capacity for further appends.
324  // The caller will be responsible for resizing later.
325  void append(const UList<token>& newTokens, const bool lazy);
326 
327  //- Move append a list of tokens at the current tokenIndex,
328  //- incrementing the index.
329  //
330  // \param newTokens the list of tokens to move append
331  // \param lazy leaves any excess capacity for further appends.
332  // The caller will be responsible for resizing later.
333  void append(List<token>&& newTokens, const bool lazy);
334 
335  //- Set flags of stream
336  ios_base::fmtflags flags(const ios_base::fmtflags)
337  {
338  return ios_base::fmtflags(0);
339  }
340 
341 
342  // Output
343 
344  //- Print stream description to Ostream
345  void print(Ostream& os) const;
346 
347  //- Concatenate tokens into a space-separated std::string.
348  //- The resulting string may contain quote characters.
349  std::string toString() const;
350 
351 
352  // Member Operators
353 
354  //- Copy assignment, with rewind()
355  void operator=(const ITstream& is);
356 
357  //- Copy assignment of tokens, with rewind()
358  void operator=(const UList<token>& toks);
359 
360  //- Move assignment of tokens, with rewind()
361  void operator=(List<token>&& toks);
362 };
363 
364 
365 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
366 
367 } // End namespace Foam
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 #endif
372 
373 // ************************************************************************* //
Foam::ITstream::beginRawRead
virtual bool beginRawRead()
Start of low-level raw binary read.
Definition: ITstream.H:288
Foam::ITstream::append
void append(const token &t, const bool lazy)
Definition: ITstream.C:408
Foam::ITstream::tokenIndex
label tokenIndex() const
The current token index when reading, or the insertion point.
Definition: ITstream.H:232
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:356
Foam::ITstream::readRaw
virtual Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
Definition: ITstream.C:342
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::Istream::Istream
Istream(const Istream &)=default
Copy construct.
Foam::ITstream::nRemainingTokens
label nRemainingTokens() const
The number of remaining tokens.
Definition: ITstream.H:244
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::ITstream::name
virtual fileName & name()
Return non-const access to the name of the stream.
Definition: ITstream.H:226
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::ITstream::read
virtual Istream & read(token &tok)
Return next token from stream.
Definition: ITstream.C:251
Foam::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.H:123
Foam::ITstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: ITstream.H:220
Foam::ITstream::operator=
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:458
Foam::ITstream::~ITstream
virtual ~ITstream()=default
Destructor.
Foam::ITstream::ITstream
ITstream(const ITstream &is)
Copy construct.
Definition: ITstream.H:90
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::ITstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: ITstream.C:202
tokenList.H
Foam::ITstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream.
Definition: ITstream.H:335
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::ITstream::flags
ios_base::fmtflags flags() const
Return flags of output stream.
Definition: ITstream.H:250
Istream.H
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:341
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::ITstream::parse
static tokenList parse(const UList< char > &input, streamFormat format=ASCII)
Definition: ITstream.C:56
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ITstream::tokenIndex
label & tokenIndex()
Non-const access to the current token index.
Definition: ITstream.H:238
Foam::ITstream::seek
void seek(label pos)
Move the tokenIndex to the specified position.
Definition: ITstream.C:362
Foam::tokenList
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
Foam::ITstream::toString
std::string toString() const
Definition: ITstream.C:226
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::ITstream::endRawRead
virtual bool endRawRead()
End of low-level raw binary read.
Definition: ITstream.H:294
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177