initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / IOstreams / Pstreams / IPstream.C
blob3d889af9615eebd56225102cf0353b9e73799d2b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "IPstream.H"
29 #include "int.H"
30 #include "token.H"
31 #include <cctype>
34 // * * * * * * * * * * * * * Private member functions  * * * * * * * * * * * //
36 inline void Foam::IPstream::checkEof()
38     if (bufPosition_ == messageSize_)
39     {
40         setEof();
41     }
45 template<class T>
46 inline void Foam::IPstream::readFromBuffer(T& t)
48     const size_t align = sizeof(T);
49     bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
51     t = reinterpret_cast<T&>(buf_[bufPosition_]);
52     bufPosition_ += sizeof(T);
53     checkEof();
57 inline void Foam::IPstream::readFromBuffer
59     void* data,
60     size_t count,
61     size_t align
64     if (align > 1)
65     {
66         bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
67     }
69     register const char* bufPtr = &buf_[bufPosition_];
70     register char* dataPtr = reinterpret_cast<char*>(data);
71     register size_t i = count;
72     while (i--) *dataPtr++ = *bufPtr++;
73     bufPosition_ += count;
74     checkEof();
78 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
80 Foam::IPstream::~IPstream()
85 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
87 Foam::Istream& Foam::IPstream::read(token& t)
89     // Return the put back token if it exists
90     if (Istream::getBack(t))
91     {
92         return *this;
93     }
95     char c;
97     // return on error
98     if (!read(c))
99     {
100         t.setBad();
101         return *this;
102     }
104     // Set the line number of this token to the current stream line number
105     t.lineNumber() = lineNumber();
107     // Analyse input starting with this character.
108     switch (c)
109     {
110         // Punctuation
111         case token::END_STATEMENT :
112         case token::BEGIN_LIST :
113         case token::END_LIST :
114         case token::BEGIN_SQR :
115         case token::END_SQR :
116         case token::BEGIN_BLOCK :
117         case token::END_BLOCK :
118         case token::COLON :
119         case token::COMMA :
120         case token::ASSIGN :
121         case token::ADD :
122         case token::SUBTRACT :
123         case token::MULTIPLY :
124         case token::DIVIDE :
125         {
126             t = token::punctuationToken(c);
127             return *this;
128         }
130         // Word
131         case token::WORD :
132         {
133             word* pval = new word;
134             if (read(*pval))
135             {
136                 if (token::compound::isCompound(*pval))
137                 {
138                     t = token::compound::New(*pval, *this).ptr();
139                     delete pval;
140                 }
141                 else
142                 {
143                     t = pval;
144                 }
145             }
146             else
147             {
148                 delete pval;
149                 t.setBad();
150             }
151             return *this;
152         }
154         // String
155         case token::STRING :
156         {
157             string* pval = new string;
158             if (read(*pval))
159             {
160                 t = pval;
161             }
162             else
163             {
164                 delete pval;
165                 t.setBad();
166             }
167             return *this;
168         }
170         // Label
171         case token::LABEL :
172         {
173             label val;
174             if (read(val))
175             {
176                 t = val;
177             }
178             else
179             {
180                 t.setBad();
181             }
182             return *this;
183         }
185         // floatScalar
186         case token::FLOAT_SCALAR :
187         {
188             floatScalar val;
189             if (read(val))
190             {
191                 t = val;
192             }
193             else
194             {
195                 t.setBad();
196             }
197             return *this;
198         }
200         // doubleScalar
201         case token::DOUBLE_SCALAR :
202         {
203             doubleScalar val;
204             if (read(val))
205             {
206                 t = val;
207             }
208             else
209             {
210                 t.setBad();
211             }
212             return *this;
213         }
215         // Character (returned as a single character word) or error
216         default:
217         {
218             if (isalpha(c))
219             {
220                 t = word(c);
221                 return *this;
222             }
224             setBad();
225             t.setBad();
227             return *this;
228         }
229     }
233 Foam::Istream& Foam::IPstream::read(char& c)
235     c = buf_[bufPosition_];
236     bufPosition_++;
237     checkEof();
238     return *this;
242 Foam::Istream& Foam::IPstream::read(word& str)
244     size_t len;
245     readFromBuffer(len);
246     str = &buf_[bufPosition_];
247     bufPosition_ += len + 1;
248     checkEof();
249     return *this;
253 Foam::Istream& Foam::IPstream::read(string& str)
255     size_t len;
256     readFromBuffer(len);
257     str = &buf_[bufPosition_];
258     bufPosition_ += len + 1;
259     checkEof();
260     return *this;
264 Foam::Istream& Foam::IPstream::read(label& val)
266     readFromBuffer(val);
267     return *this;
271 Foam::Istream& Foam::IPstream::read(floatScalar& val)
273     readFromBuffer(val);
274     return *this;
278 Foam::Istream& Foam::IPstream::read(doubleScalar& val)
280     readFromBuffer(val);
281     return *this;
285 Foam::Istream& Foam::IPstream::read(char* data, std::streamsize count)
287     if (format() != BINARY)
288     {
289         FatalErrorIn("IPstream::read(char*, std::streamsize)")
290             << "stream format not binary"
291             << Foam::abort(FatalError);
292     }
294     readFromBuffer(data, count, 8);
295     return *this;
299 Foam::Istream& Foam::IPstream::rewind()
301     bufPosition_ = 0;
302     return *this;
306 // ************************************************************************* //