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 
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 // ************************************************************************* //
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:377
~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:384
Database for solution and other reduced data.
Definition: data.H:54
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:59
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:82
A token holds items read from Istream.
Definition: token.H:73
@ LABEL
Definition: token.H:88
@ VARIABLE
Definition: token.H:85
@ WORD
Definition: token.H:83
@ FLOAT_SCALAR
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:90
@ LONG_DOUBLE_SCALAR
Definition: token.H:91
@ VERBATIMSTRING
Definition: token.H:87
@ FUNCTIONNAME
Definition: token.H:84
@ STRING
Definition: token.H:86
punctuationToken
Standard punctuation tokens.
Definition: token.H:99
@ DIVIDE
Definition: token.H:123
@ BEGIN_BLOCK
Definition: token.H:110
@ BEGIN_SQR
Definition: token.H:108
@ END_BLOCK
Definition: token.H:111
@ ASSIGN
Definition: token.H:119
@ END_STATEMENT
Definition: token.H:105
@ BEGIN_LIST
Definition: token.H:106
@ SUBTRACT
Definition: token.H:121
@ END_LIST
Definition: token.H:107
@ END_SQR
Definition: token.H:109
@ MULTIPLY
Definition: token.H:122
void setBad()
Set bad.
Definition: tokenI.H:550
label lineNumber() const
Definition: tokenI.H:539
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:306
System integer.
const dimensionedScalar c
Speed of light in a vacuum.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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"))