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 
36 {
38  {
39  setEof();
40  }
41 }
42 
43 
44 template<class 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 
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  {
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 // ************************************************************************* //
token.H
Foam::token::compound::New
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
Foam::token::END_STATEMENT
@ END_STATEMENT
Definition: token.H:99
format
word format(conversionProperties.lookup("format"))
Foam::doubleScalar
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
int.H
System integer.
Foam::token::ADD
@ ADD
Definition: token.H:114
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::UIPstream::externalBufPosition_
label & externalBufPosition_
Definition: UIPstream.H:64
Foam::UIPstream::checkEof
void checkEof()
Check the bufferPosition against messageSize_ for EOF.
Definition: UIPstream.C:35
Foam::token::LABEL
@ LABEL
Definition: token.H:82
Foam::token::COMMA
@ COMMA
Definition: token.H:107
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::floatScalar
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
Foam::token::punctuationToken
punctuationToken
Standard punctuation tokens.
Definition: token.H:92
Foam::IOstream::setEof
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:475
Foam::token::SUBTRACT
@ SUBTRACT
Definition: token.H:115
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::token::lineNumber
label lineNumber() const
Definition: tokenI.H:381
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::token::COLON
@ COLON
Definition: token.H:106
Foam::UIPstream::read
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
Foam::token::FLOAT_SCALAR
@ FLOAT_SCALAR
Definition: token.H:83
Foam::token::type
tokenType type() const
Definition: tokenI.H:178
Foam::label
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.H
Foam::UIPstream::print
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: UIPstream.C:337
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::token::MULTIPLY
@ MULTIPLY
Definition: token.H:116
Foam::token::VARIABLE
@ VARIABLE
Definition: token.H:79
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::UIPstream::~UIPstream
~UIPstream()
Destructor.
Definition: UIPstream.C:81
Foam::token::compound::isCompound
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
Foam::UIPstream::readFromBuffer
void readFromBuffer(T &)
Read a T from the transfer buffer.
Definition: UIPstream.C:45
Foam::FatalError
error FatalError
Foam::token::STRING
@ STRING
Definition: token.H:80
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::UIPstream::messageSize_
int messageSize_
Definition: UIPstream.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::UIPstream::rewind
Istream & rewind()
Rewind and return the stream so that it may be read again.
Definition: UIPstream.C:330
Foam::token::DIVIDE
@ DIVIDE
Definition: token.H:117
Foam::token::WORD
@ WORD
Definition: token.H:78
Foam::token::END_SQR
@ END_SQR
Definition: token.H:103
Foam::token::DOUBLE_SCALAR
@ DOUBLE_SCALAR
Definition: token.H:84
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
Foam::token::BEGIN_SQR
@ BEGIN_SQR
Definition: token.H:102
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::token::ASSIGN
@ ASSIGN
Definition: token.H:113
Foam::Istream::getBack
bool getBack(token &)
Get the put back token if there is one and return true.
Definition: Istream.C:52
UIPstream.H
Foam::token::VERBATIMSTRING
@ VERBATIMSTRING
Definition: token.H:81
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::setBad
void setBad()
Set bad.
Definition: tokenI.H:392