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-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::ITstream
29 
30 Description
31  An input stream of tokens.
32 
33  Although ITstream is principally meant to be used as a read-only
34  input stream, it also provides additional methods to help when
35  composing its contents (eg, when parsing).
36 
37 SourceFiles
38  ITstream.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_ITstream_H
43 #define Foam_ITstream_H
44 
45 #include "Istream.H"
46 #include "tokenList.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class ITstream Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class ITstream
58 :
59  public Istream,
60  public tokenList
61 {
62  // Private Data
63 
64  //- Name associated with the stream
65  fileName name_;
66 
67  //- Index of token currently being read
68  label tokenIndex_;
69 
70 
71  // Private Member Functions
72 
73  //- An ad hoc combination of reserve and setCapacity somewhat
74  //- similar to DynamicList.
75  //
76  // Increase list size if needed,
77  // but leave any excess capacity (ie, like reserve).
78  void reserveCapacity(const label newCapacity);
79 
80  //- Failsafe read-access to token at specified location
81  //- or undefinedToken
82  inline const token& peekNoFail(const label i) const
83  {
84  return
85  (
86  (i >= 0 && i < tokenList::size())
87  ? tokenList::operator[](i)
89  );
90  }
91 
92 
93 public:
94 
95  // Constructors
96 
97  //- Copy construct
98  ITstream(const ITstream& is);
99 
100  //- Move construct
101  ITstream(ITstream&& is);
102 
103  //- Default construct. Empty stream, optionally with given name
104  explicit ITstream
105  (
106  IOstreamOption streamOpt = IOstreamOption(),
107  const string& name = "input"
108  );
109 
110  //- Construct empty, optionally with given name
111  explicit ITstream
112  (
113  const Foam::zero,
114  const string& name = "input",
115  IOstreamOption streamOpt = IOstreamOption()
116  );
117 
118  //- Copy construct from tokens, optionally with given name
119  explicit ITstream
120  (
121  const UList<token>& tokens,
122  IOstreamOption streamOpt = IOstreamOption(),
123  const string& name = "input"
124  );
125 
126  //- Move construct from tokens, optionally with given name
127  explicit ITstream
128  (
129  List<token>&& tokens,
130  IOstreamOption streamOpt = IOstreamOption(),
131  const string& name = "input"
132  );
133 
134  //- Construct token list by parsing the input character sequence
135  // Uses static parse function internally.
136  explicit ITstream
137  (
138  const UList<char>& input,
139  IOstreamOption streamOpt = IOstreamOption(),
140  const string& name = "input"
141  );
142 
143  //- Construct token list by parsing the input string
144  // Uses static parse function internally.
145  explicit ITstream
146  (
147  const std::string& input,
148  IOstreamOption streamOpt = IOstreamOption(),
149  const string& name = "input"
150  );
151 
152  //- Construct token list by parsing the input character sequence
153  // Uses static parse function internally.
154  explicit ITstream
155  (
156  const char* input,
157  IOstreamOption streamOpt = IOstreamOption(),
158  const string& name = "input"
159  );
160 
161 
162  // Additional constructors
163 
164  //- Copy construct from tokens, with given name
165  ITstream
166  (
167  const string& name,
168  const UList<token>& tokens,
169  IOstreamOption streamOpt = IOstreamOption()
170  )
171  :
172  ITstream(tokens, streamOpt, name)
173  {}
174 
175  //- Move construct from tokens, with given name
176  ITstream
177  (
178  const string& name,
179  List<token>&& tokens,
180  IOstreamOption streamOpt = IOstreamOption()
181  )
182  :
183  ITstream(std::move(tokens), streamOpt, name)
184  {}
185 
186 
187  //- Destructor
188  virtual ~ITstream() = default;
189 
190 
191  // Static Functions
192 
193  //- Return reference to an empty ITstream, for functions needing to
194  //- return an ITstream reference but which don't have anything valid
195  //- of their own. <b>The stream shall be considered \em read-only.</b>
196  //
197  // The returned stream has no tokens and has a 'bad' state,
198  // to indicate that it is invalid for reading.
199  //
200  // \caution the caller is still able to modify its contents,
201  // but they should not do that!
202  static ITstream& empty_stream();
203 
204  //- Create token list by parsing the input character sequence
205  //- until no good tokens remain.
207  (
208  const UList<char>& input,
209  IOstreamOption streamOpt = IOstreamOption()
210  );
211 
212  //- Create token list by parsing the input string
213  //- until no good tokens remain.
214  static tokenList parse
215  (
216  const std::string& input,
217  IOstreamOption streamOpt = IOstreamOption()
218  );
219 
220  //- Create token list by parsing the input character sequence
221  //- until no good tokens remain.
222  static tokenList parse
223  (
224  const char* input,
225  IOstreamOption streamOpt = IOstreamOption()
226  );
227 
228 
229  // Member Functions
230 
231  // Characteristics
232 
233  //- The name of the input token stream.
234  virtual const fileName& name() const override { return name_; }
235 
236  //- The name of the input token stream, for modification.
237  virtual fileName& name() { return name_; }
238 
239  //- The line number of the first token in stream
240  label startLineNumber() const
241  {
242  return (tokenList::empty() ? -1 : tokenList::front().lineNumber());
243  }
244 
245  //- The line number of the last token in stream
246  label endLineNumber() const
247  {
248  return (tokenList::empty() ? -1 : tokenList::back().lineNumber());
249  }
250 
251 
252  // Token Access
253 
254  //- True if putback token is in use
255  bool hasPutback() const noexcept { return Istream::hasPutback(); }
256 
257  //- Failsafe peek at the token at the \b front of the tokenList
258  // \return \c undefinedToken if the list is empty.
259  const token& front() const { return peekNoFail(0); }
260 
261  //- Failsafe peek at the token at the \b back of the tokenList
262  // \return \c undefinedToken if the list is empty.
263  const token& back() const { return peekNoFail(tokenList::size()-1); }
264 
265  //- Failsafe peek at what the next read would return,
266  //- including handling of any putback
267  // \return \c undefinedToken if list is exhausted
268  const token& peek() const;
269 
270  //- Read access to the token at the current tokenIndex.
271  //- \returns \c undefinedToken if list is exhausted
272  const token& currentToken() const { return peekNoFail(tokenIndex_); }
273 
274  //- Write access to the token at the current tokenIndex.
275  //- Fatal if not in range
277 
278  //- Failsafe read access to token at given position in the tokenList
279  // \return \c undefinedToken for out-of-range
280  const token& peekToken(const label i) const { return peekNoFail(i); };
282  //- The current token index when reading, or the insertion point.
283  label tokenIndex() const noexcept { return tokenIndex_; }
284 
285  //- Non-const access to the current token index
286  label& tokenIndex() noexcept { return tokenIndex_; }
287 
288  //- Set the token index (no checks). \return the previous value
289  label tokenIndex(const label num) noexcept
290  {
291  label old(tokenIndex_);
292  tokenIndex_ = num;
293  return old;
294  }
295 
296  //- Number of tokens remaining
297  label nRemainingTokens() const noexcept
298  {
299  return (tokenList::size() - tokenIndex_);
300  }
301 
302  //- Move tokenIndex to the specified position
303  // Using seek(0) is identical to rewind.
304  // Using seek(-1) moves to the end.
305  void seek(label pos);
306 
307  //- Move tokenIndex relative to the current position.
308  // Will not overrun the beginning (0) or one-past end positions.
309  //
310  // Use skip(2) to move forward two tokens.
311  // Use skip(-2) to move backward two tokens.
312  // \return True if the indexing completed without underflow/overflow
313  bool skip(label n = 1);
314 
315 
316  // Searching
317 
318  //- Regular searching
320 
321  //- Find range containing matching delimiter pair, starting at the
322  //- the specified position. The position -1 indicates to continue
323  //- from the present tokenIndex() position.
325  (
326  const token::punctuationToken delimOpen,
327  const token::punctuationToken delimClose,
328  label pos = 0
329  ) const;
330 
331 
332  // Token list modification
334  //- Remove a (start,size) subset from the list and move remaining
335  //- elements down.
336  // If the tokenIndex() is within or to the right of the removed
337  // region, it will be adjusted to remain valid. However, this may
338  // not correspond to the expected parsing point so external bookkeeping
339  // is likely necessary.
340  // \note The range is subsetted with the list size itself to ensure
341  // the result always addresses a valid section of the list.
343 
344  //- Remove a (start,size) subset from the list and move remaining
345  //- elements down.
346  // If the tokenIndex() is within or to the right of the removed
347  // region, it will be adjusted to remain valid. However, this may
348  // not correspond to the expected parsing point so external bookkeeping
349  // is likely necessary.
350  // \note The range is subsetted with the list size itself to ensure
351  // the result always addresses a valid section of the list.
352  label remove(const labelRange& range);
353 
354  //- Copy append a token at the current tokenIndex,
355  //- incrementing the index.
356  void add_tokens(const token& tok);
357 
358  //- Move append a token at the current tokenIndex,
359  //- incrementing the index.
360  void add_tokens(token&& tok);
362  //- Copy append a list of tokens at the current tokenIndex,
363  //- incrementing the index.
364  //
365  // \param newTokens the list of tokens to copy append
366  // \param lazy leaves any excess capacity for further appends.
367  // The caller will be responsible for resizing later.
368  void add_tokens(const UList<token>& toks);
369 
370  //- Move append a list of tokens at the current tokenIndex,
371  //- incrementing the index.
372  //
373  // \param newTokens the list of tokens to move append
374  // \param lazy leaves any excess capacity for further appends.
375  // The caller will be responsible for resizing later.
376  void add_tokens(List<token>&& toks);
377 
378 
379  // Stream State Functions
380 
381  //- Get stream flags - always 0
382  virtual ios_base::fmtflags flags() const override
383  {
384  return ios_base::fmtflags(0);
385  }
386 
387  //- Set flags of stream - ignored
388  ios_base::fmtflags flags(const ios_base::fmtflags) override
389  {
390  return ios_base::fmtflags(0);
391  }
392 
393 
394  // Read Functions
395 
396  //- Return next token from stream
397  virtual Istream& read(token& tok) override;
398 
399  //- Read a character : triggers not implemented error
400  virtual Istream& read(char&) override;
401 
402  //- Read a word : triggers not implemented error
403  virtual Istream& read(word&) override;
404 
405  //- Read a string (including enclosing double-quotes) :
406  //- triggers not implemented error
407  virtual Istream& read(string&) override;
408 
409  //- Read a label : triggers not implemented error
410  virtual Istream& read(label&) override;
411 
412  //- Read a float : triggers not implemented error
413  virtual Istream& read(float&) override;
414 
415  //- Read a double : triggers not implemented error
416  virtual Istream& read(double&) override;
417 
418  //- Read binary block : triggers not implemented error
419  virtual Istream& read(char* data, std::streamsize) override;
420 
421  //- Low-level raw binary read : triggers not implemented error
422  virtual Istream& readRaw(char* data, std::streamsize count) override;
423 
424  //- Start of low-level raw binary read : no-op
425  virtual bool beginRawRead() override { return false; }
426 
427  //- End of low-level raw binary read : no-op
428  virtual bool endRawRead() override { return false; }
429 
430  //- Rewind the stream so that it may be read again. Same as seek(0)
431  virtual void rewind() override;
432 
433 
434  // Output
435 
436  //- Print stream description to Ostream
437  void print(Ostream& os) const override;
438 
439  //- Concatenate tokens into a space-separated std::string.
440  //- The resulting string may contain quote characters.
441  std::string toString() const;
442 
443 
444  // Member Operators
445 
446  //- Return a non-const reference to const Istream
447  using Istream::operator();
448 
449  //- Copy assignment, with rewind()
450  void operator=(const ITstream& is);
451 
452  //- Copy assignment of tokens, with rewind()
453  void operator=(const UList<token>& toks);
454 
455  //- Move assignment of tokens, with rewind()
456  void operator=(List<token>&& toks);
457 
458 
459  // Additional constructors and methods (as per v2012 and earlier)
460  #ifdef Foam_IOstream_extras
461 
462  //- Construct from components, copying the tokens
463  ITstream
464  (
465  const string& name,
466  const UList<token>& tokens,
468  )
469  :
470  ITstream(name, tokens, IOstreamOption(fmt))
471  {}
472 
473  //- Construct from components, transferring the tokens
474  ITstream
475  (
476  const string& name,
477  List<token>&& tokens,
479  )
480  :
481  ITstream(name, std::move(tokens), IOstreamOption(fmt))
482  {}
483 
484  #endif /* Foam_IOstream_extras */
485 
486 
487  // Housekeeping
488 
489  //- Same as front()
490  FOAM_DEPRECATED_STRICT(2022-11, "front()")
491  const token& peekFirst() const { return front(); }
493  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
494  void append(const token& t, bool) { add_tokens(t); }
495 
496  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
497  void append(token&& t, bool) { add_tokens(std::move(t)); }
498 
499  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
500  void append(const UList<token>& t, bool) { add_tokens(t); }
501 
502  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
503  void append(List<token>&& t, bool) { add_tokens(std::move(t)); }
504 
505  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
506  void push_back(const token& t, bool) { add_tokens(t); }
507 
508  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
509  void push_back(token&& t, bool) { add_tokens(std::move(t)); }
510 
511  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
512  void push_back(const UList<token>& t, bool) { add_tokens(t); }
513 
514  FOAM_DEPRECATED_STRICT(2023-10, "add_tokens()")
515  void push_back(List<token>&& t, bool) { add_tokens(std::move(t)); }
516 };
517 
518 
519 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
520 
521 } // End namespace Foam
522 
523 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
524 
525 #endif
526 
527 // ************************************************************************* //
virtual bool beginRawRead() override
Start of low-level raw binary read : no-op.
Definition: ITstream.H:549
void add_tokens(const token &tok)
Copy append a token at the current tokenIndex, incrementing the index.
Definition: ITstream.C:735
static const token undefinedToken
An undefined token.
Definition: token.H:542
const token & front() const
Failsafe peek at the token at the front of the tokenList.
Definition: ITstream.H:312
A class for handling file names.
Definition: fileName.H:72
void seek(label pos)
Move tokenIndex to the specified position.
Definition: ITstream.C:380
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
label endLineNumber() const
The line number of the last token in stream.
Definition: ITstream.H:294
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
A token holds an item read from Istream.
Definition: token.H:65
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:272
T & front()
Access first element of the list, position [0].
Definition: UListI.H:237
A simple container for options an IOstream can normally have.
std::string toString() const
Concatenate tokens into a space-separated std::string. The resulting string may contain quote charact...
Definition: ITstream.C:316
label startLineNumber() const
The line number of the first token in stream.
Definition: ITstream.H:286
label tokenIndex() const noexcept
The current token index when reading, or the insertion point.
Definition: ITstream.H:351
label nRemainingTokens() const noexcept
Number of tokens remaining.
Definition: ITstream.H:371
scalar range
const token & peek() const
Failsafe peek at what the next read would return, including handling of any putback.
Definition: ITstream.C:354
static tokenList parse(const UList< char > &input, IOstreamOption streamOpt=IOstreamOption())
Create token list by parsing the input character sequence until no good tokens remain.
Definition: ITstream.C:88
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual Istream & read(token &tok) override
Return next token from stream.
Definition: ITstream.C:474
virtual Istream & readRaw(char *data, std::streamsize count) override
Low-level raw binary read : triggers not implemented error.
Definition: ITstream.C:715
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
bool hasPutback() const noexcept
True if putback token is in use.
Definition: ITstream.H:305
const token & peekFirst() const
Same as front()
Definition: ITstream.H:637
void push_back(const token &t, bool)
Definition: ITstream.H:652
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:126
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
const token & currentToken() const
Read access to the token at the current tokenIndex.
Definition: ITstream.H:333
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:105
label find(const T &val) const
Find index of the first occurrence of the value.
Definition: UList.C:173
void append(const token &t, bool)
Definition: ITstream.H:640
const direction noexcept
Definition: Scalar.H:258
virtual const fileName & name() const override
The name of the input token stream.
Definition: ITstream.H:276
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
OBJstream os(runTime.globalPath()/outputName)
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:390
virtual ios_base::fmtflags flags() const override
Get stream flags - always 0.
Definition: ITstream.H:484
constexpr UList() noexcept
Default construct, zero-sized and nullptr.
Definition: UListI.H:28
virtual ~ITstream()=default
Destructor.
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:81
labelRange find(const token::punctuationToken delimOpen, const token::punctuationToken delimClose, label pos=0) const
Find range containing matching delimiter pair, starting at the the specified position. The position -1 indicates to continue from the present tokenIndex() position.
Definition: ITstream.C:527
streamFormat
Data format (ascii | binary)
T & back()
Access last element of the list, position [size()-1].
Definition: UListI.H:251
static ITstream & empty_stream()
Return reference to an empty ITstream, for functions needing to return an ITstream reference but whic...
Definition: ITstream.C:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
virtual void rewind() override
Rewind the stream so that it may be read again. Same as seek(0)
Definition: ITstream.C:729
void print(Ostream &os) const override
Print stream description to Ostream.
Definition: ITstream.C:291
virtual bool endRawRead() override
End of low-level raw binary read : no-op.
Definition: ITstream.H:554
bool skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:427
ITstream(const ITstream &is)
Copy construct.
Definition: ITstream.C:156
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:776
const token & back() const
Failsafe peek at the token at the back of the tokenList.
Definition: ITstream.H:319
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
An input stream of tokens.
Definition: ITstream.H:52
Namespace for OpenFOAM.
Istream(const Istream &)=default
Copy construct.
const token & peekToken(const label i) const
Failsafe read access to token at given position in the tokenList.
Definition: ITstream.H:346
ITstream extract(const labelRange &range)
Remove a (start,size) subset from the list and move remaining elements down.
Definition: ITstream.C:580