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-2018 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  t = pval;
158  }
159  }
160  else
161  {
162  delete pval;
163  t.setBad();
164  }
165  return *this;
166  }
167 
168  // String
169  case token::VERBATIMSTRING :
170  {
171  // Recurse to read actual string
172  read(t);
174  return *this;
175  }
176  case token::VARIABLE :
177  {
178  // Recurse to read actual string
179  read(t);
180  t.type() = token::VARIABLE;
181  return *this;
182  }
183  case token::STRING :
184  {
185  string* pval = new string;
186  if (read(*pval))
187  {
188  t = pval;
189  if (c == token::VERBATIMSTRING)
190  {
192  }
193  }
194  else
195  {
196  delete pval;
197  t.setBad();
198  }
199  return *this;
200  }
201 
202  // Label
203  case token::LABEL :
204  {
205  label val;
206  if (read(val))
207  {
208  t = val;
209  }
210  else
211  {
212  t.setBad();
213  }
214  return *this;
215  }
216 
217  // floatScalar
218  case token::FLOAT_SCALAR :
219  {
220  floatScalar val;
221  if (read(val))
222  {
223  t = val;
224  }
225  else
226  {
227  t.setBad();
228  }
229  return *this;
230  }
231 
232  // doubleScalar
233  case token::DOUBLE_SCALAR :
234  {
235  doubleScalar val;
236  if (read(val))
237  {
238  t = val;
239  }
240  else
241  {
242  t.setBad();
243  }
244  return *this;
245  }
246 
247  // longDoubleScalar
249  {
250  longDoubleScalar val;
251  if (read(val))
252  {
253  t = val;
254  }
255  else
256  {
257  t.setBad();
258  }
259  return *this;
260  }
261 
262  // Character (returned as a single character word) or error
263  default:
264  {
265  if (isalpha(c))
266  {
267  t = word(c);
268  return *this;
269  }
270 
271  setBad();
272  t.setBad();
273 
274  return *this;
275  }
276  }
277 }
278 
279 
281 {
282  c = externalBuf_[externalBufPosition_];
283  externalBufPosition_++;
284  checkEof();
285  return *this;
286 }
287 
288 
290 {
291  size_t len;
292  readFromBuffer(len);
293  str = &externalBuf_[externalBufPosition_];
294  externalBufPosition_ += len + 1;
295  checkEof();
296  return *this;
297 }
298 
299 
301 {
302  size_t len;
303  readFromBuffer(len);
304  str = &externalBuf_[externalBufPosition_];
305  externalBufPosition_ += len + 1;
306  checkEof();
307  return *this;
308 }
309 
310 
312 {
313  readFromBuffer(val);
314  return *this;
315 }
316 
317 
319 {
320  readFromBuffer(val);
321  return *this;
322 }
323 
324 
326 {
327  readFromBuffer(val);
328  return *this;
329 }
330 
331 
333 {
334  readFromBuffer(val);
335  return *this;
336 }
337 
338 
339 Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
340 {
341  if (format() != BINARY)
342  {
344  << "stream format not binary"
346  }
347 
348  readFromBuffer(data, count, 8);
349  return *this;
350 }
351 
352 
354 {
355  externalBufPosition_ = 0;
356  return *this;
357 }
358 
359 
361 {
362  os << "Reading from processor " << fromProcNo_
363  << " using communicator " << comm_
364  << " and tag " << tag_
365  << Foam::endl;
366 }
367 
368 
369 // ************************************************************************* //
System integer.
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
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:163
A token holds items read from Istream.
Definition: token.H:69
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
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:429
Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: UIPstream.C:353
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:418
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UIPstream.C:360
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
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:475
tokenType type() const
Definition: tokenI.H:187
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
const dimensionedScalar c
Speed of light in a vacuum.
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:74
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60