UIPstream.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 | Copyright (C) 2011-2015 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 #include <cctype>
31 
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 inline void Foam::UIPstream::checkEof()
36 {
37  if (externalBufPosition_ == messageSize_)
38  {
39  setEof();
40  }
41 }
42 
43 
44 template<class T>
45 inline void Foam::UIPstream::readFromBuffer(T& t)
46 {
47  const size_t align = sizeof(T);
48  externalBufPosition_ = align + ((externalBufPosition_ - 1) & ~(align - 1));
49 
50  t = reinterpret_cast<T&>(externalBuf_[externalBufPosition_]);
51  externalBufPosition_ += sizeof(T);
52  checkEof();
53 }
54 
55 
56 inline void Foam::UIPstream::readFromBuffer
57 (
58  void* data,
59  size_t count,
60  size_t align
61 )
62 {
63  if (align > 1)
64  {
65  externalBufPosition_ =
66  align
67  + ((externalBufPosition_ - 1) & ~(align - 1));
68  }
69 
70  const char* bufPtr = &externalBuf_[externalBufPosition_];
71  char* dataPtr = reinterpret_cast<char*>(data);
72  size_t i = count;
73  while (i--) *dataPtr++ = *bufPtr++;
74  externalBufPosition_ += count;
75  checkEof();
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
80 
82 {
83  if (clearAtEnd_ && eof())
84  {
85  if (debug)
86  {
87  Pout<< "UIPstream::~UIPstream() : tag:" << tag_
88  << " fromProcNo:" << fromProcNo_
89  << " clearing externalBuf_ of size "
90  << externalBuf_.size()
91  << " messageSize_:" << messageSize_ << endl;
92  }
93  externalBuf_.clearStorage();
94  }
95 }
96 
97 
98 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
99 
101 {
102  // Return the put back token if it exists
103  if (Istream::getBack(t))
104  {
105  return *this;
106  }
107 
108  char c;
109 
110  // return on error
111  if (!read(c))
112  {
113  t.setBad();
114  return *this;
115  }
116 
117  // Set the line number of this token to the current stream line number
118  t.lineNumber() = lineNumber();
119 
120  // Analyse input starting with this character.
121  switch (c)
122  {
123  // Punctuation
124  case token::END_STATEMENT :
125  case token::BEGIN_LIST :
126  case token::END_LIST :
127  case token::BEGIN_SQR :
128  case token::END_SQR :
129  case token::BEGIN_BLOCK :
130  case token::END_BLOCK :
131  case token::COLON :
132  case token::COMMA :
133  case token::ASSIGN :
134  case token::ADD :
135  case token::SUBTRACT :
136  case token::MULTIPLY :
137  case token::DIVIDE :
138  {
140  return *this;
141  }
142 
143  // Word
144  case token::WORD :
145  {
146  word* pval = new word;
147  if (read(*pval))
148  {
149  if (token::compound::isCompound(*pval))
150  {
151  t = token::compound::New(*pval, *this).ptr();
152  delete pval;
153  }
154  else
155  {
156  t = pval;
157  }
158  }
159  else
160  {
161  delete pval;
162  t.setBad();
163  }
164  return *this;
165  }
166 
167  // String
168  case token::VERBATIMSTRING :
169  {
170  // Recurse to read actual string
171  read(t);
173  return *this;
174  }
175  case token::VARIABLE :
176  {
177  // Recurse to read actual string
178  read(t);
179  t.type() = token::VARIABLE;
180  return *this;
181  }
182  case token::STRING :
183  {
184  string* pval = new string;
185  if (read(*pval))
186  {
187  t = pval;
188  if (c == token::VERBATIMSTRING)
189  {
191  }
192  }
193  else
194  {
195  delete pval;
196  t.setBad();
197  }
198  return *this;
199  }
200 
201  // Label
202  case token::LABEL :
203  {
204  label val;
205  if (read(val))
206  {
207  t = val;
208  }
209  else
210  {
211  t.setBad();
212  }
213  return *this;
214  }
215 
216  // floatScalar
217  case token::FLOAT_SCALAR :
218  {
219  floatScalar val;
220  if (read(val))
221  {
222  t = val;
223  }
224  else
225  {
226  t.setBad();
227  }
228  return *this;
229  }
230 
231  // doubleScalar
232  case token::DOUBLE_SCALAR :
233  {
234  doubleScalar val;
235  if (read(val))
236  {
237  t = val;
238  }
239  else
240  {
241  t.setBad();
242  }
243  return *this;
244  }
245 
246  // Character (returned as a single character word) or error
247  default:
248  {
249  if (isalpha(c))
250  {
251  t = word(c);
252  return *this;
253  }
254 
255  setBad();
256  t.setBad();
257 
258  return *this;
259  }
260  }
261 }
262 
263 
265 {
266  c = externalBuf_[externalBufPosition_];
267  externalBufPosition_++;
268  checkEof();
269  return *this;
270 }
271 
272 
274 {
275  size_t len;
276  readFromBuffer(len);
277  str = &externalBuf_[externalBufPosition_];
278  externalBufPosition_ += len + 1;
279  checkEof();
280  return *this;
281 }
282 
283 
285 {
286  size_t len;
287  readFromBuffer(len);
288  str = &externalBuf_[externalBufPosition_];
289  externalBufPosition_ += len + 1;
290  checkEof();
291  return *this;
292 }
293 
294 
296 {
297  readFromBuffer(val);
298  return *this;
299 }
300 
301 
303 {
304  readFromBuffer(val);
305  return *this;
306 }
307 
308 
310 {
311  readFromBuffer(val);
312  return *this;
313 }
314 
315 
316 Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
317 {
318  if (format() != BINARY)
319  {
320  FatalErrorIn("UIPstream::read(char*, std::streamsize)")
321  << "stream format not binary"
323  }
324 
325  readFromBuffer(data, count, 8);
326  return *this;
327 }
328 
329 
331 {
332  externalBufPosition_ = 0;
333  return *this;
334 }
335 
336 
338 {
339  os << "Reading from processor " << fromProcNo_
340  << " using communicator " << comm_
341  << " and tag " << tag_
342  << Foam::endl;
343 }
344 
345 
346 // ************************************************************************* //
System integer.
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:475
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
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
A class for handling character strings derived from std::string.
Definition: string.H:74
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: UIPstream.C:330
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:100
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
punctuationToken
Standard punctuation tokens.
Definition: token.H:92
tokenType type() const
Definition: tokenI.H:178
void setBad()
Set bad.
Definition: tokenI.H:392
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label lineNumber() const
Definition: tokenI.H:381
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
~UIPstream()
Destructor.
Definition: UIPstream.C:81
A token holds items read from Istream.
Definition: token.H:67
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const dimensionedScalar c
Speed of light in a vacuum.
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:249
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UIPstream.C:337
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53