ITstream.C
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-2015 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 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include "ITstream.H"
31 #include "StringStream.H"
32 #include "UIListStream.H"
33 
34 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
35 
36 Foam::label Foam::ITstream::parseStream(ISstream& is, tokenList& tokens)
37 {
38  label nTok = 0;
39 
40  tokens.clear();
41  tokens.resize(64, token());
42 
43  token tok;
44  while (!is.read(tok).bad() && tok.good())
45  {
46  tokens.newElmt(nTok++) = std::move(tok);
47  }
48 
49  tokens.resize(nTok);
50 
51  return nTok;
52 }
53 
54 
56 (
57  const UList<char>& input,
59 )
60 {
62 
63  tokenList tokens;
64  parseStream(is, tokens);
65  return tokens;
66 }
67 
68 
70 (
71  const std::string& input,
73 )
74 {
75  UIListStream is
76  (
77  input.data(),
78  input.size(),
79  format,
81  );
82 
83  tokenList tokens;
84  parseStream(is, tokens);
85  return tokens;
86 }
87 
88 
90 (
91  const char* input,
93 )
94 {
96 
97  tokenList tokens;
98  parseStream(is, tokens);
99  return tokens;
100 }
101 
102 
103 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
104 
105 void Foam::ITstream::reserveCapacity
106 (
107  const label nElem,
108  const bool lazy
109 )
110 {
111  if (lazy)
112  {
113  // Reserve - leave excess capacity for further appends
114 
115  label n = tokenList::size();
116 
117  if (nElem > n)
118  {
119  if (!n) n = 1; // Avoid dead-lock when starting from zero-sized
120 
121  do
122  {
123  n *= 2;
124  }
125  while (nElem >= n);
126 
128  }
129  }
130  else
131  {
132  // Strict capacity
133  tokenList::resize(nElem);
134  }
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
139 
141 (
142  const string& name,
143  const UList<char>& input,
146 )
147 :
149  tokenList(),
150  name_(name),
151  tokenIndex_(0)
152 {
154 
155  parseStream(is, static_cast<tokenList&>(*this));
157 }
158 
159 
161 (
162  const string& name,
163  const std::string& input,
166 )
167 :
169  tokenList(),
170  name_(name),
171  tokenIndex_(0)
172 {
173  UIListStream is(input.data(), input.size(), format, version);
174 
175  parseStream(is, static_cast<tokenList&>(*this));
177 }
178 
179 
181 (
182  const string& name,
183  const char* input,
186 )
187 :
189  tokenList(),
190  name_(name),
191  tokenIndex_(0)
192 {
193  UIListStream is(input, strlen(input), format, version);
194 
195  parseStream(is, static_cast<tokenList&>(*this));
197 }
198 
199 
200 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
201 
203 {
204  os << "ITstream : " << name_.c_str() << ", line ";
205 
206  if (size())
207  {
208  os << tokenList::first().lineNumber();
209 
210  if (tokenList::first().lineNumber() < tokenList::last().lineNumber())
211  {
212  os << '-' << tokenList::last().lineNumber();
213  }
214  }
215  else
216  {
217  os << lineNumber();
218  }
219 
220  os << ", ";
221 
222  IOstream::print(os);
223 }
224 
225 
226 std::string Foam::ITstream::toString() const
227 {
228  OStringStream buf;
229 
230  const tokenList& tokens = *this;
231 
232  label len = tokens.size();
233 
234  // NOTE: may wish to have special handling if there is a single token
235  // and it is already a string or word
236 
237  for (const token& tok : tokens)
238  {
239  buf << tok;
240 
241  if (--len)
242  {
243  buf << ' ';
244  }
245  }
246 
247  return buf.str();
248 }
249 
250 
252 {
253  // Return the put back token if it exists
254  if (Istream::getBack(tok))
255  {
256  lineNumber_ = tok.lineNumber();
257  return *this;
258  }
259 
260  if (tokenIndex_ < size())
261  {
262  tok = operator[](tokenIndex_++);
263  lineNumber_ = tok.lineNumber();
264 
265  if (tokenIndex_ == size())
266  {
267  setEof();
268  }
269  }
270  else
271  {
272  if (eof())
273  {
275  << "attempt to read beyond EOF"
276  << exit(FatalIOError);
277  setBad();
278  }
279  else
280  {
281  setEof();
282  }
283 
284  tok.reset();
285 
286  if (size())
287  {
288  tok.lineNumber() = tokenList::last().lineNumber();
289  }
290  else
291  {
292  tok.lineNumber() = lineNumber();
293  }
294  }
295 
296  return *this;
297 }
298 
299 
301 {
303  return *this;
304 }
305 
306 
308 {
310  return *this;
311 }
312 
313 
315 {
317  return *this;
318 }
319 
320 
322 {
324  return *this;
325 }
326 
327 
329 {
331  return *this;
332 }
333 
334 
336 {
338  return *this;
339 }
340 
341 
342 Foam::Istream& Foam::ITstream::readRaw(char*, std::streamsize)
343 {
345  return *this;
346 }
347 
348 
349 Foam::Istream& Foam::ITstream::read(char*, std::streamsize)
350 {
352  return *this;
353 }
354 
355 
357 {
358  seek(0);
359 }
360 
361 
363 {
364  lineNumber_ = 0;
365  tokenList& toks = *this;
366 
367  if (!pos)
368  {
369  // Seek begin (rewind)
370  tokenIndex_ = 0;
371 
372  if (!toks.empty())
373  {
374  lineNumber_ = toks.first().lineNumber();
375  }
376 
377  setOpened();
378  setGood();
379  }
380  else if (pos < 0 || pos >= toks.size())
381  {
382  // Seek end or seek is out of range
383  tokenIndex_ = toks.size();
384 
385  if (!toks.empty())
386  {
387  lineNumber_ = toks.last().lineNumber();
388  }
389 
390  setEof();
391  }
392  else
393  {
394  // Seek middle (from the beginning)
395  tokenIndex_ = pos;
396 
397  if (!toks.empty())
398  {
399  lineNumber_ = toks[tokenIndex_].lineNumber();
400  }
401 
402  setOpened();
403  setGood();
404  }
405 }
406 
407 
408 void Foam::ITstream::append(const token& t, const bool lazy)
409 {
410  reserveCapacity(tokenIndex_ + 1, lazy);
411  tokenList& toks = *this;
412 
413  toks[tokenIndex_] = t; // copy append
414  ++tokenIndex_;
415 }
416 
417 
418 void Foam::ITstream::append(token&& t, const bool lazy)
419 {
420  reserveCapacity(tokenIndex_ + 1, lazy);
421  tokenList& toks = *this;
422 
423  toks[tokenIndex_] = std::move(t); // move append
424  ++tokenIndex_;
425 }
426 
427 
428 void Foam::ITstream::append(const UList<token>& newTokens, const bool lazy)
429 {
430  reserveCapacity(tokenIndex_ + newTokens.size(), lazy);
431  tokenList& toks = *this;
432 
433  for (const token& t : newTokens)
434  {
435  toks[tokenIndex_] = t; // copy append
436  ++tokenIndex_;
437  }
438 }
439 
440 
441 void Foam::ITstream::append(List<token>&& newTokens, const bool lazy)
442 {
443  reserveCapacity(tokenIndex_ + newTokens.size(), lazy);
444  tokenList& toks = *this;
445 
446  for (token& t : newTokens)
447  {
448  toks[tokenIndex_] = std::move(t); // move append
449  ++tokenIndex_;
450  }
451 
452  newTokens.clear();
453 }
454 
455 
456 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
457 
459 {
460  Istream::operator=(is);
462  name_ = is.name_;
463 
464  rewind();
465 }
466 
467 
469 {
470  tokenList::operator=(toks);
471 
472  rewind();
473 }
474 
475 
477 {
478  tokenList::operator=(std::move(toks));
479 
480  rewind();
481 }
482 
483 
484 // ************************************************************************* //
Foam::ITstream::append
void append(const token &t, const bool lazy)
Definition: ITstream.C:408
Foam::UIListStream
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:202
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
ITstream.H
Foam::token::lineNumber
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:387
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::IOstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:72
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
StringStream.H
Input/output from string buffers.
Foam::FatalIOError
IOerror FatalIOError
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::ITstream::operator=
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:458
n
label n
Definition: TABSMDCalcMethod2.H:31
format
word format(conversionProperties.get< word >("format"))
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
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
error.H
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
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
UIListStream.H
Foam::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:256
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:501
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
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::ITstream::seek
void seek(label pos)
Move the tokenIndex to the specified position.
Definition: ITstream.C:362
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::tokenList
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:91
Foam::Istream::getBack
bool getBack(token &tok)
Get the put back token if there is one and return true.
Definition: Istream.C:75
Foam::ITstream::toString
std::string toString() const
Definition: ITstream.C:226
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::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
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::IOstream::lineNumber
label lineNumber() const
Const access to the current stream line number.
Definition: IOstream.H:309
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177