OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

IPstream.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 #include "IPstream.H"
00029 #include "int.H"
00030 #include "token.H"
00031 #include <cctype>
00032 
00033 
00034 // * * * * * * * * * * * * * Private member functions  * * * * * * * * * * * //
00035 
00036 inline void Foam::IPstream::checkEof()
00037 {
00038     if (bufPosition_ == messageSize_)
00039     {
00040         setEof();
00041     }
00042 }
00043 
00044 
00045 template<class T>
00046 inline void Foam::IPstream::readFromBuffer(T& t)
00047 {
00048     const size_t align = sizeof(T);
00049     bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
00050 
00051     t = reinterpret_cast<T&>(buf_[bufPosition_]);
00052     bufPosition_ += sizeof(T);
00053     checkEof();
00054 }
00055 
00056 
00057 inline void Foam::IPstream::readFromBuffer
00058 (
00059     void* data,
00060     size_t count,
00061     size_t align
00062 )
00063 {
00064     if (align > 1)
00065     {
00066         bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
00067     }
00068 
00069     register const char* bufPtr = &buf_[bufPosition_];
00070     register char* dataPtr = reinterpret_cast<char*>(data);
00071     register size_t i = count;
00072     while (i--) *dataPtr++ = *bufPtr++;
00073     bufPosition_ += count;
00074     checkEof();
00075 }
00076 
00077 
00078 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00079 
00080 Foam::IPstream::~IPstream()
00081 {}
00082 
00083 
00084 
00085 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00086 
00087 Foam::Istream& Foam::IPstream::read(token& t)
00088 {
00089     // Return the put back token if it exists
00090     if (Istream::getBack(t))
00091     {
00092         return *this;
00093     }
00094 
00095     char c;
00096 
00097     // return on error
00098     if (!read(c))
00099     {
00100         t.setBad();
00101         return *this;
00102     }
00103 
00104     // Set the line number of this token to the current stream line number
00105     t.lineNumber() = lineNumber();
00106 
00107     // Analyse input starting with this character.
00108     switch (c)
00109     {
00110         // Punctuation
00111         case token::END_STATEMENT :
00112         case token::BEGIN_LIST :
00113         case token::END_LIST :
00114         case token::BEGIN_SQR :
00115         case token::END_SQR :
00116         case token::BEGIN_BLOCK :
00117         case token::END_BLOCK :
00118         case token::COLON :
00119         case token::COMMA :
00120         case token::ASSIGN :
00121         case token::ADD :
00122         case token::SUBTRACT :
00123         case token::MULTIPLY :
00124         case token::DIVIDE :
00125         {
00126             t = token::punctuationToken(c);
00127             return *this;
00128         }
00129 
00130         // Word
00131         case token::WORD :
00132         {
00133             word* pval = new word;
00134             if (read(*pval))
00135             {
00136                 if (token::compound::isCompound(*pval))
00137                 {
00138                     t = token::compound::New(*pval, *this).ptr();
00139                     delete pval;
00140                 }
00141                 else
00142                 {
00143                     t = pval;
00144                 }
00145             }
00146             else
00147             {
00148                 delete pval;
00149                 t.setBad();
00150             }
00151             return *this;
00152         }
00153 
00154         // String
00155         case token::STRING :
00156         {
00157             string* pval = new string;
00158             if (read(*pval))
00159             {
00160                 t = pval;
00161             }
00162             else
00163             {
00164                 delete pval;
00165                 t.setBad();
00166             }
00167             return *this;
00168         }
00169 
00170         // Label
00171         case token::LABEL :
00172         {
00173             label val;
00174             if (read(val))
00175             {
00176                 t = val;
00177             }
00178             else
00179             {
00180                 t.setBad();
00181             }
00182             return *this;
00183         }
00184 
00185         // floatScalar
00186         case token::FLOAT_SCALAR :
00187         {
00188             floatScalar val;
00189             if (read(val))
00190             {
00191                 t = val;
00192             }
00193             else
00194             {
00195                 t.setBad();
00196             }
00197             return *this;
00198         }
00199 
00200         // doubleScalar
00201         case token::DOUBLE_SCALAR :
00202         {
00203             doubleScalar val;
00204             if (read(val))
00205             {
00206                 t = val;
00207             }
00208             else
00209             {
00210                 t.setBad();
00211             }
00212             return *this;
00213         }
00214 
00215         // Character (returned as a single character word) or error
00216         default:
00217         {
00218             if (isalpha(c))
00219             {
00220                 t = word(c);
00221                 return *this;
00222             }
00223 
00224             setBad();
00225             t.setBad();
00226 
00227             return *this;
00228         }
00229     }
00230 }
00231 
00232 
00233 Foam::Istream& Foam::IPstream::read(char& c)
00234 {
00235     c = buf_[bufPosition_];
00236     bufPosition_++;
00237     checkEof();
00238     return *this;
00239 }
00240 
00241 
00242 Foam::Istream& Foam::IPstream::read(word& str)
00243 {
00244     size_t len;
00245     readFromBuffer(len);
00246     str = &buf_[bufPosition_];
00247     bufPosition_ += len + 1;
00248     checkEof();
00249     return *this;
00250 }
00251 
00252 
00253 Foam::Istream& Foam::IPstream::read(string& str)
00254 {
00255     size_t len;
00256     readFromBuffer(len);
00257     str = &buf_[bufPosition_];
00258     bufPosition_ += len + 1;
00259     checkEof();
00260     return *this;
00261 }
00262 
00263 
00264 Foam::Istream& Foam::IPstream::read(label& val)
00265 {
00266     readFromBuffer(val);
00267     return *this;
00268 }
00269 
00270 
00271 Foam::Istream& Foam::IPstream::read(floatScalar& val)
00272 {
00273     readFromBuffer(val);
00274     return *this;
00275 }
00276 
00277 
00278 Foam::Istream& Foam::IPstream::read(doubleScalar& val)
00279 {
00280     readFromBuffer(val);
00281     return *this;
00282 }
00283 
00284 
00285 Foam::Istream& Foam::IPstream::read(char* data, std::streamsize count)
00286 {
00287     if (format() != BINARY)
00288     {
00289         FatalErrorIn("IPstream::read(char*, std::streamsize)")
00290             << "stream format not binary"
00291             << Foam::abort(FatalError);
00292     }
00293 
00294     readFromBuffer(data, count, 8);
00295     return *this;
00296 }
00297 
00298 
00299 Foam::Istream& Foam::IPstream::rewind()
00300 {
00301     bufPosition_ = 0;
00302     return *this;
00303 }
00304 
00305 
00306 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd