FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/error.H>
00027 #include "IPstream.H"
00028 #include <OpenFOAM/int.H>
00029 #include <OpenFOAM/token.H>
00030 #include <cctype>
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 Foam::autoPtr<Foam::IPstreamImpl> Foam::IPstream::impl_;
00035 
00036 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
00037 
00038 Foam::IPstream::IPstream
00039 (
00040     const commsTypes commsType,
00041     const int fromProcNo,
00042     const label bufSize,
00043     streamFormat format,
00044     versionNumber version
00045 )
00046 :
00047     Pstream(commsType, bufSize),
00048     Istream(format, version),
00049     fromProcNo_(fromProcNo),
00050     messageSize_(0)
00051 {
00052     setOpened();
00053     setGood();
00054 
00055     impl()->init(commsType, bufSize, fromProcNo_, messageSize_, buf_);
00056 
00057 }
00058 
00059 // * * * * * * * * * * * * * Private member functions  * * * * * * * * * * * //
00060 
00061 inline void Foam::IPstream::checkEof()
00062 {
00063     if (bufPosition_ == messageSize_)
00064     {
00065         setEof();
00066     }
00067 }
00068 
00069 
00070 template<class T>
00071 inline void Foam::IPstream::readFromBuffer(T& t)
00072 {
00073     const size_t align = sizeof(T);
00074     bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
00075 
00076     t = reinterpret_cast<T&>(buf_[bufPosition_]);
00077     bufPosition_ += sizeof(T);
00078     checkEof();
00079 }
00080 
00081 
00082 inline void Foam::IPstream::readFromBuffer
00083 (
00084     void* data,
00085     size_t count,
00086     size_t align
00087 )
00088 {
00089     if (align > 1)
00090     {
00091         bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
00092     }
00093 
00094     register const char* bufPtr = &buf_[bufPosition_];
00095     register char* dataPtr = reinterpret_cast<char*>(data);
00096     register size_t i = count;
00097     while (i--) *dataPtr++ = *bufPtr++;
00098     bufPosition_ += count;
00099     checkEof();
00100 }
00101 
00102 
00103 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00104 
00105 Foam::IPstream::~IPstream()
00106 {}
00107 
00108 
00109 
00110 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00111 
00112 Foam::Istream& Foam::IPstream::read(token& t)
00113 {
00114     // Return the put back token if it exists
00115     if (Istream::getBack(t))
00116     {
00117         return *this;
00118     }
00119 
00120     char c;
00121 
00122     // return on error
00123     if (!read(c))
00124     {
00125         t.setBad();
00126         return *this;
00127     }
00128 
00129     // Set the line number of this token to the current stream line number
00130     t.lineNumber() = lineNumber();
00131 
00132     // Analyse input starting with this character.
00133     switch (c)
00134     {
00135         // Punctuation
00136         case token::END_STATEMENT :
00137         case token::BEGIN_LIST :
00138         case token::END_LIST :
00139         case token::BEGIN_SQR :
00140         case token::END_SQR :
00141         case token::BEGIN_BLOCK :
00142         case token::END_BLOCK :
00143         case token::COLON :
00144         case token::COMMA :
00145         case token::ASSIGN :
00146         case token::ADD :
00147         case token::SUBTRACT :
00148         case token::MULTIPLY :
00149         case token::DIVIDE :
00150         {
00151             t = token::punctuationToken(c);
00152             return *this;
00153         }
00154 
00155         // Word
00156         case token::WORD :
00157         {
00158             word* pval = new word;
00159             if (read(*pval))
00160             {
00161                 if (token::compound::isCompound(*pval))
00162                 {
00163                     t = token::compound::New(*pval, *this).ptr();
00164                     delete pval;
00165                 }
00166                 else
00167                 {
00168                     t = pval;
00169                 }
00170             }
00171             else
00172             {
00173                 delete pval;
00174                 t.setBad();
00175             }
00176             return *this;
00177         }
00178 
00179         // String
00180         case token::STRING :
00181         {
00182             string* pval = new string;
00183             if (read(*pval))
00184             {
00185                 t = pval;
00186             }
00187             else
00188             {
00189                 delete pval;
00190                 t.setBad();
00191             }
00192             return *this;
00193         }
00194 
00195         // Label
00196         case token::LABEL :
00197         {
00198             label val;
00199             if (read(val))
00200             {
00201                 t = val;
00202             }
00203             else
00204             {
00205                 t.setBad();
00206             }
00207             return *this;
00208         }
00209 
00210         // floatScalar
00211         case token::FLOAT_SCALAR :
00212         {
00213             floatScalar val;
00214             if (read(val))
00215             {
00216                 t = val;
00217             }
00218             else
00219             {
00220                 t.setBad();
00221             }
00222             return *this;
00223         }
00224 
00225         // doubleScalar
00226         case token::DOUBLE_SCALAR :
00227         {
00228             doubleScalar val;
00229             if (read(val))
00230             {
00231                 t = val;
00232             }
00233             else
00234             {
00235                 t.setBad();
00236             }
00237             return *this;
00238         }
00239 
00240         // Character (returned as a single character word) or error
00241         default:
00242         {
00243             if (isalpha(c))
00244             {
00245                 t = word(c);
00246                 return *this;
00247             }
00248 
00249             setBad();
00250             t.setBad();
00251 
00252             return *this;
00253         }
00254     }
00255 }
00256 
00257 
00258 Foam::Istream& Foam::IPstream::read(char& c)
00259 {
00260     c = buf_[bufPosition_];
00261     bufPosition_++;
00262     checkEof();
00263     return *this;
00264 }
00265 
00266 
00267 Foam::Istream& Foam::IPstream::read(word& str)
00268 {
00269     size_t len;
00270     readFromBuffer(len);
00271     str = &buf_[bufPosition_];
00272     bufPosition_ += len + 1;
00273     checkEof();
00274     return *this;
00275 }
00276 
00277 
00278 Foam::Istream& Foam::IPstream::read(string& str)
00279 {
00280     size_t len;
00281     readFromBuffer(len);
00282     str = &buf_[bufPosition_];
00283     bufPosition_ += len + 1;
00284     checkEof();
00285     return *this;
00286 }
00287 
00288 
00289 Foam::Istream& Foam::IPstream::read(label& val)
00290 {
00291     readFromBuffer(val);
00292     return *this;
00293 }
00294 
00295 
00296 Foam::Istream& Foam::IPstream::read(floatScalar& val)
00297 {
00298     readFromBuffer(val);
00299     return *this;
00300 }
00301 
00302 
00303 Foam::Istream& Foam::IPstream::read(doubleScalar& val)
00304 {
00305     readFromBuffer(val);
00306     return *this;
00307 }
00308 
00309 
00310 Foam::Istream& Foam::IPstream::read(char* data, std::streamsize count)
00311 {
00312     if (format() != BINARY)
00313     {
00314         FatalErrorIn("IPstream::read(char*, std::streamsize)")
00315             << "stream format not binary"
00316             << Foam::abort(FatalError);
00317     }
00318 
00319     readFromBuffer(data, count, 8);
00320     return *this;
00321 }
00322 
00323 
00324 Foam::Istream& Foam::IPstream::rewind()
00325 {
00326     bufPosition_ = 0;
00327     return *this;
00328 }
00329 
00330 
00331 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines