UIPstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "UIPstream.H"
28 #include "int.H"
29 #include "token.H"
30 
31 #include <cctype>
32 
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 inline void Foam::UIPstream::checkEof()
37 {
38  if (externalBufPosition_ == messageSize_)
39  {
40  setEof();
41  }
42 }
43 
44 
45 template<class T>
46 inline void Foam::UIPstream::readFromBuffer(T& t)
47 {
48  const size_t align = sizeof(T);
49  externalBufPosition_ = align + ((externalBufPosition_ - 1) & ~(align - 1));
50 
51  t = reinterpret_cast<T&>(externalBuf_[externalBufPosition_]);
52  externalBufPosition_ += sizeof(T);
53  checkEof();
54 }
55 
56 
57 inline void Foam::UIPstream::readFromBuffer
58 (
59  void* data,
60  size_t count,
61  size_t align
62 )
63 {
64  if (align > 1)
65  {
66  externalBufPosition_ =
67  align
68  + ((externalBufPosition_ - 1) & ~(align - 1));
69  }
70 
71  const char* bufPtr = &externalBuf_[externalBufPosition_];
72  char* dataPtr = reinterpret_cast<char*>(data);
73  size_t i = count;
74  while (i--) *dataPtr++ = *bufPtr++;
75  externalBufPosition_ += count;
76  checkEof();
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
81 
83 {
84  if (clearAtEnd_ && eof())
85  {
86  if (debug)
87  {
88  Pout<< "UIPstream::~UIPstream() : tag:" << tag_
89  << " fromProcNo:" << fromProcNo_
90  << " clearing externalBuf_ of size "
91  << externalBuf_.size()
92  << " messageSize_:" << messageSize_ << endl;
93  }
94  externalBuf_.clearStorage();
95  }
96 }
97 
98 
99 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
100 
102 {
103  // Return the put back token if it exists
104  if (Istream::getBack(t))
105  {
106  return *this;
107  }
108 
109  char c;
110 
111  // return on error
112  if (!read(c))
113  {
114  t.setBad();
115  return *this;
116  }
117 
118  // Set the line number of this token to the current stream line number
119  t.lineNumber() = lineNumber();
120 
121  // Analyse input starting with this character.
122  switch (c)
123  {
124  // Punctuation
125  case token::END_STATEMENT :
126  case token::BEGIN_LIST :
127  case token::END_LIST :
128  case token::BEGIN_SQR :
129  case token::END_SQR :
130  case token::BEGIN_BLOCK :
131  case token::END_BLOCK :
132  case token::COLON :
133  case token::COMMA :
134  case token::ASSIGN :
135  case token::ADD :
136  case token::SUBTRACT :
137  case token::MULTIPLY :
138  case token::DIVIDE :
139  {
141  return *this;
142  }
143 
144  // Word
145  case token::WORD :
146  {
147  word* pval = new word;
148  if (read(*pval))
149  {
150  if (token::compound::isCompound(*pval))
151  {
152  t = token::compound::New(*pval, *this).ptr();
153  delete pval;
154  }
155  else
156  {
157  if (pval->size() > 1 && (*pval)[0] == '#')
158  {
159  t = new functionName(*pval);
160  delete pval;
161  }
162  else
163  {
164  t = pval;
165  }
166  }
167  }
168  else
169  {
170  delete pval;
171  t.setBad();
172  }
173  return *this;
174  }
175 
176  // FunctionName
177  case token::FUNCTIONNAME :
178  {
180  << "Binary IO of function names not supported"
182  return *this;
183  }
184 
185  // Variable
186  case token::VARIABLE :
187  {
189  << "Binary IO of variables not supported"
191  return *this;
192  }
193 
194  // String
195  case token::STRING :
196  {
197  string* pval = new string;
198  if (read(*pval))
199  {
200  t = pval;
201  }
202  else
203  {
204  delete pval;
205  t.setBad();
206  }
207  return *this;
208  }
209 
210  // Verbatim string
211  case token::VERBATIMSTRING :
212  {
213  verbatimString* pval = new verbatimString;
214  if (read(*pval))
215  {
216  t = pval;
217  }
218  else
219  {
220  delete pval;
221  t.setBad();
222  }
223  return *this;
224  }
225 
226  // Label
227  case token::LABEL :
228  {
229  label val;
230  if (read(val))
231  {
232  t = val;
233  }
234  else
235  {
236  t.setBad();
237  }
238  return *this;
239  }
240 
241  // floatScalar
242  case token::FLOAT_SCALAR :
243  {
244  floatScalar val;
245  if (read(val))
246  {
247  t = val;
248  }
249  else
250  {
251  t.setBad();
252  }
253  return *this;
254  }
255 
256  // doubleScalar
257  case token::DOUBLE_SCALAR :
258  {
259  doubleScalar val;
260  if (read(val))
261  {
262  t = val;
263  }
264  else
265  {
266  t.setBad();
267  }
268  return *this;
269  }
270 
271  // longDoubleScalar
273  {
274  longDoubleScalar val;
275  if (read(val))
276  {
277  t = val;
278  }
279  else
280  {
281  t.setBad();
282  }
283  return *this;
284  }
285 
286  // Character (returned as a single character word) or error
287  default:
288  {
289  if (isalpha(c))
290  {
291  t = word(c);
292  return *this;
293  }
294 
295  setBad();
296  t.setBad();
297 
298  return *this;
299  }
300  }
301 }
302 
303 
305 {
306  c = externalBuf_[externalBufPosition_];
307  externalBufPosition_++;
308  checkEof();
309  return *this;
310 }
311 
312 
314 {
315  size_t len;
316  readFromBuffer(len);
317  str = &externalBuf_[externalBufPosition_];
318  externalBufPosition_ += len + 1;
319  checkEof();
320  return *this;
321 }
322 
323 
325 {
326  size_t len;
327  readFromBuffer(len);
328  str = &externalBuf_[externalBufPosition_];
329  externalBufPosition_ += len + 1;
330  checkEof();
331  return *this;
332 }
333 
334 
336 {
337  readFromBuffer(val);
338  return *this;
339 }
340 
341 
343 {
344  readFromBuffer(val);
345  return *this;
346 }
347 
348 
350 {
351  readFromBuffer(val);
352  return *this;
353 }
354 
355 
357 {
358  readFromBuffer(val);
359  return *this;
360 }
361 
362 
363 Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
364 {
365  if (format() != BINARY)
366  {
368  << "stream format not binary"
370  }
371 
372  readFromBuffer(data, count, 8);
373  return *this;
374 }
375 
376 
378 {
379  externalBufPosition_ = 0;
380  return *this;
381 }
382 
383 
385 {
386  os << "Reading from processor " << fromProcNo_
387  << " using communicator " << comm_
388  << " and tag " << tag_
389  << Foam::endl;
390 }
391 
392 
393 // ************************************************************************* //
A class for handling verbatimStrings, derived from string.
System integer.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A token holds items read from Istream.
Definition: token.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
const dimensionedScalar c
Speed of light in a vacuum.
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
A class for handling words, derived from string.
Definition: word.H:59
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
void setBad()
Set bad.
Definition: tokenI.H:550
Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: UIPstream.C:377
punctuationToken
Standard punctuation tokens.
Definition: token.H:98
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:539
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UIPstream.C:384
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Read into given buffer from given processor and return the.
Definition: UIPread.C:79
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
~UIPstream()
Destructor.
Definition: UIPstream.C:82
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
long double longDoubleScalar
Lang double precision floating point scalar type.
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:472
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:243
A class for handling character strings derived from std::string.
Definition: string.H:76
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60