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 from components
104  (
105  const string& name,
106  const UList<token>& tokens,
109  )
110  :
112  tokenList(tokens),
113  name_(name),
114  tokenIndex_(0)
115  {
116  setOpened();
117  setGood();
118  }
119 
120  //- Construct from components, transferring the tokens
122  (
123  const string& name,
124  List<token>&& tokens,
127  )
128  :
130  tokenList(std::move(tokens)),
131  name_(name),
132  tokenIndex_(0)
133  {
134  setOpened();
135  setGood();
136  }
137 
138  //- Construct token list by parsing the input character sequence
139  // Uses UIListStream internally.
140  ITstream
141  (
142  const string& name,
143  const UList<char>& input,
145  versionNumber version=currentVersion
146  );
147 
148  //- Construct token list by parsing the input string
149  // Uses UIListStream internally.
150  ITstream
151  (
152  const string& name,
153  const std::string& input,
155  versionNumber version=currentVersion
156  );
157 
158  //- Construct token list by parsing the input character sequence
159  // Uses UIListStream internally.
160  ITstream
161  (
162  const string& name,
163  const char* input,
165  versionNumber version=currentVersion
166  );
167 
168 
169  //- Destructor
170  virtual ~ITstream() = default;
171 
172 
173  // Static Functions
174 
175  //- Create token list by parsing the input character sequence until
176  //- no good tokens remain.
177  static tokenList parse
178  (
179  const UList<char>& input,
181  );
182 
183  //- Create token list by parsing the input string until
184  //- no good tokens remain.
185  static tokenList parse
186  (
187  const std::string& input,
189  );
190 
191  //- Create token list by parsing the input character sequence until
192  //- no good tokens remain.
193  static tokenList parse
194  (
195  const char* input,
197  );
198 
199 
200  // Member Functions
201 
202  // Inquiry
203 
204  //- Return the name of the stream
205  virtual const fileName& name() const
206  {
207  return name_;
208  }
209 
210  //- Return non-const access to the name of the stream
211  virtual fileName& name()
212  {
213  return name_;
214  }
215 
216  //- The current token index when reading, or the insertion point.
217  label tokenIndex() const
218  {
219  return tokenIndex_;
220  }
221 
222  //- Non-const access to the current token index
223  label& tokenIndex()
224  {
225  return tokenIndex_;
226  }
227 
228  //- The number of remaining tokens
229  label nRemainingTokens() const
230  {
231  return size() - tokenIndex_;
232  }
233 
234  //- Return flags of output stream
235  ios_base::fmtflags flags() const
236  {
237  return ios_base::fmtflags(0);
238  }
239 
240 
241  // Read Functions
242 
243  //- Return next token from stream
244  virtual Istream& read(token& tok);
245 
246  //- Read a character
247  virtual Istream& read(char&);
248 
249  //- Read a word
250  virtual Istream& read(word&);
251 
252  // Read a string (including enclosing double-quotes)
253  virtual Istream& read(string&);
254 
255  //- Read a label
256  virtual Istream& read(label&);
257 
258  //- Read a floatScalar
259  virtual Istream& read(floatScalar&);
260 
261  //- Read a doubleScalar
262  virtual Istream& read(doubleScalar&);
263 
264  //- Read binary block
265  // \note Not implemented
266  virtual Istream& read(char* data, std::streamsize);
267 
268  //- Low-level raw binary read
269  // \note Not implemented
270  virtual Istream& readRaw(char* data, std::streamsize count);
271 
272  //- Start of low-level raw binary read
273  virtual bool beginRawRead()
274  {
275  return false;
276  }
277 
278  //- End of low-level raw binary read
279  virtual bool endRawRead()
280  {
281  return false;
282  }
283 
284  //- Rewind the stream so that it may be read again
285  virtual void rewind();
286 
287  //- Move the tokenIndex to the specified position.
288  // Using seek(0) is identical to rewind.
289  // Using seek(-1) moves to the end.
290  void seek(label pos);
291 
292 
293  // Edit
294 
295  //- Copy append a token at the current tokenIndex,
296  //- incrementing the index.
297  void append(const token& t, const bool lazy);
298 
299  //- Move append a token at the current tokenIndex,
300  //- incrementing the index.
301  void append(token&& t, const bool lazy);
302 
303  //- Copy append a tokenList at the current tokenIndex,
304  //- incrementing the index.
305  //
306  // \param newTokens the list of tokens to copy append
307  // \param lazy leaves any excess capacity for further appends.
308  // The caller will be responsible for resizing later.
309  void append(const tokenList& newTokens, const bool lazy);
310 
311  //- Move append a tokenList at the current tokenIndex,
312  //- incrementing the index.
313  //
314  // \param newTokens the list of tokens to move append
315  // \param lazy leaves any excess capacity for further appends.
316  // The caller will be responsible for resizing later.
317  void append(tokenList&& newTokens, const bool lazy);
318 
319  //- Set flags of stream
320  ios_base::fmtflags flags(const ios_base::fmtflags)
321  {
322  return ios_base::fmtflags(0);
323  }
324 
325 
326  // Output
327 
328  //- Print stream description to Ostream
329  void print(Ostream& os) const;
330 
331  //- Concatenate tokens into a space-separated std::string.
332  //- The resulting string may contain quote characters.
333  std::string toString() const;
334 
335 
336  // Member Operators
337 
338  //- Copy assignment, with rewind()
339  void operator=(const ITstream& is);
340 
341  //- Copy assignment of tokens, with rewind()
342  void operator=(const tokenList& toks);
343 
344  //- Move assignment of tokens, with rewind()
345  void operator=(tokenList&& toks);
346 };
347 
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 } // End namespace Foam
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 #endif
356 
357 // ************************************************************************* //
Foam::ITstream::beginRawRead
virtual bool beginRawRead()
Start of low-level raw binary read.
Definition: ITstream.H:272
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:216
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::Istream::Istream
Istream(const Istream &)=default
Copy construct.
Foam::ITstream::nRemainingTokens
label nRemainingTokens() const
The number of remaining tokens.
Definition: ITstream.H:228
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:210
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:69
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:204
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:319
Foam::ITstream::flags
ios_base::fmtflags flags() const
Return flags of output stream.
Definition: ITstream.H:234
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:222
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:74
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::ITstream::endRawRead
virtual bool endRawRead()
End of low-level raw binary read.
Definition: ITstream.H:278
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::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:54
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177