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-2024 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  // 32-bit integer
227  case token::INTEGER_32 :
228  {
229  int32_t val;
230  if (read(val))
231  {
232  t = val;
233  }
234  else
235  {
236  t.setBad();
237  }
238  return *this;
239  }
240 
241  // 64-bit integer
242  case token::INTEGER_64 :
243  {
244  int64_t val;
245  if (read(val))
246  {
247  t = val;
248  }
249  else
250  {
251  t.setBad();
252  }
253  return *this;
254  }
255 
256  // unsigned 32-bit integer
258  {
259  uint32_t val;
260  if (read(val))
261  {
262  t = val;
263  }
264  else
265  {
266  t.setBad();
267  }
268  return *this;
269  }
270 
271  // Unsigned 64-bit integer
273  {
274  uint64_t val;
275  if (read(val))
276  {
277  t = val;
278  }
279  else
280  {
281  t.setBad();
282  }
283  return *this;
284  }
285 
286  // floatScalar
287  case token::FLOAT_SCALAR :
288  {
289  floatScalar val;
290  if (read(val))
291  {
292  t = val;
293  }
294  else
295  {
296  t.setBad();
297  }
298  return *this;
299  }
300 
301  // doubleScalar
302  case token::DOUBLE_SCALAR :
303  {
304  doubleScalar val;
305  if (read(val))
306  {
307  t = val;
308  }
309  else
310  {
311  t.setBad();
312  }
313  return *this;
314  }
315 
316  // longDoubleScalar
318  {
319  longDoubleScalar val;
320  if (read(val))
321  {
322  t = val;
323  }
324  else
325  {
326  t.setBad();
327  }
328  return *this;
329  }
330 
331  // Character (returned as a single character word) or error
332  default:
333  {
334  if (isalpha(c))
335  {
336  t = word(c);
337  return *this;
338  }
339 
340  setBad();
341  t.setBad();
342 
343  return *this;
344  }
345  }
346 }
347 
348 
350 {
351  c = externalBuf_[externalBufPosition_];
352  externalBufPosition_++;
353  checkEof();
354  return *this;
355 }
356 
357 
359 {
360  size_t len;
361  readFromBuffer(len);
362  str = &externalBuf_[externalBufPosition_];
363  externalBufPosition_ += len + 1;
364  checkEof();
365  return *this;
366 }
367 
368 
370 {
371  size_t len;
372  readFromBuffer(len);
373  str = &externalBuf_[externalBufPosition_];
374  externalBufPosition_ += len + 1;
375  checkEof();
376  return *this;
377 }
378 
379 
381 {
382  readFromBuffer(val);
383  return *this;
384 }
385 
386 
388 {
389  readFromBuffer(val);
390  return *this;
391 }
392 
393 
395 {
396  readFromBuffer(val);
397  return *this;
398 }
399 
400 
402 {
403  readFromBuffer(val);
404  return *this;
405 }
406 
407 
409 {
410  readFromBuffer(val);
411  return *this;
412 }
413 
414 
416 {
417  readFromBuffer(val);
418  return *this;
419 }
420 
421 
423 {
424  readFromBuffer(val);
425  return *this;
426 }
427 
428 
429 Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
430 {
431  if (format() != BINARY)
432  {
434  << "stream format not binary"
436  }
437 
438  readFromBuffer(data, count, 8);
439  return *this;
440 }
441 
442 
444 {
445  externalBufPosition_ = 0;
446  return *this;
447 }
448 
449 
451 {
452  os << "Reading from processor " << fromProcNo_
453  << " using communicator " << comm_
454  << " and tag " << tag_
455  << Foam::endl;
456 }
457 
458 
459 // ************************************************************************* //
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:472
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: UIPstream.C:443
~UIPstream()
Destructor.
Definition: UIPstream.C:82
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
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UIPstream.C:450
A functionName is a word starting with '#'.
Definition: functionName.H:60
A class for handling character strings derived from std::string.
Definition: string.H:79
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:78
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:118
A token holds items read from Istream.
Definition: token.H:73
@ VARIABLE
Definition: token.H:85
@ WORD
Definition: token.H:83
@ UNSIGNED_INTEGER_32
Definition: token.H:90
@ FLOAT_SCALAR
Definition: token.H:92
@ INTEGER_64
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:93
@ LONG_DOUBLE_SCALAR
Definition: token.H:94
@ VERBATIMSTRING
Definition: token.H:87
@ FUNCTIONNAME
Definition: token.H:84
@ UNSIGNED_INTEGER_64
Definition: token.H:91
@ INTEGER_32
Definition: token.H:88
@ STRING
Definition: token.H:86
punctuationToken
Standard punctuation tokens.
Definition: token.H:102
@ DIVIDE
Definition: token.H:126
@ BEGIN_BLOCK
Definition: token.H:113
@ BEGIN_SQR
Definition: token.H:111
@ END_BLOCK
Definition: token.H:114
@ ASSIGN
Definition: token.H:122
@ END_STATEMENT
Definition: token.H:108
@ BEGIN_LIST
Definition: token.H:109
@ SUBTRACT
Definition: token.H:124
@ END_LIST
Definition: token.H:110
@ END_SQR
Definition: token.H:112
@ MULTIPLY
Definition: token.H:125
void setBad()
Set bad.
Definition: tokenI.H:814
label lineNumber() const
Definition: tokenI.H:803
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
System integer.
const dimensionedScalar c
Speed of light in a vacuum.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
long double longDoubleScalar
Lang double precision floating point scalar type.
word format(conversionProperties.lookup("format"))