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

OPstream.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 Description
00026     Write primitive and binary block from OPstream
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "error.H"
00031 
00032 #include "OPstream.H"
00033 #include "int.H"
00034 #include "token.H"
00035 
00036 #include <cctype>
00037 
00038 // * * * * * * * * * * * * * Private member functions  * * * * * * * * * * * //
00039 
00040 template<class T>
00041 inline void Foam::OPstream::writeToBuffer(const T& t)
00042 {
00043     writeToBuffer(&t, sizeof(T), sizeof(T));
00044 }
00045 
00046 
00047 inline void Foam::OPstream::writeToBuffer(const char& c)
00048 {
00049     if (size_t(buf_.size()) < bufPosition_ + 1U)
00050     {
00051         enlargeBuffer(1);
00052     }
00053 
00054     buf_[bufPosition_] = c;
00055     bufPosition_ ++;
00056 }
00057 
00058 
00059 inline void Foam::OPstream::writeToBuffer
00060 (
00061     const void* data,
00062     size_t count,
00063     size_t align
00064 )
00065 {
00066     label oldPos = bufPosition_;
00067 
00068     if (align > 1)
00069     {
00070         // Align bufPosition. Pads bufPosition_ - oldPos characters.
00071         bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
00072     }
00073 
00074     if (size_t(buf_.size()) < bufPosition_ + count)
00075     {
00076         enlargeBuffer(bufPosition_ - oldPos + count);
00077     }
00078 
00079     register char* bufPtr = &buf_[bufPosition_];
00080     register const char* dataPtr = reinterpret_cast<const char*>(data);
00081     register size_t i = count;
00082     while (i--) *bufPtr++ = *dataPtr++;
00083 
00084     bufPosition_ += count;
00085 }
00086 
00087 
00088 
00089 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
00090 
00091 Foam::OPstream::OPstream
00092 (
00093     const commsTypes commsType,
00094     const int toProcNo,
00095     const label bufSize,
00096     streamFormat format,
00097     versionNumber version
00098 )
00099 :
00100     Pstream(commsType, bufSize),
00101     Ostream(format, version),
00102     toProcNo_(toProcNo)
00103 {
00104     setOpened();
00105     setGood();
00106 
00107     if (!bufSize)
00108     {
00109         buf_.setSize(1000);
00110     }
00111 }
00112 
00113 
00114 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00115 
00116 Foam::Ostream& Foam::OPstream::write(const token&)
00117 {
00118     notImplemented("Ostream& OPstream::write(const token&)");
00119     setBad();
00120     return *this;
00121 }
00122 
00123 
00124 Foam::Ostream& Foam::OPstream::write(const char c)
00125 {
00126     if (!isspace(c))
00127     {
00128         writeToBuffer(c);
00129     }
00130 
00131     return *this;
00132 }
00133 
00134 
00135 Foam::Ostream& Foam::OPstream::write(const char* str)
00136 {
00137     word nonWhiteChars(string::validate<word>(str));
00138 
00139     if (nonWhiteChars.size() == 1)
00140     {
00141         return write(nonWhiteChars.c_str()[1]);
00142     }
00143     else if (nonWhiteChars.size())
00144     {
00145         return write(nonWhiteChars);
00146     }
00147     else
00148     {
00149         return *this;
00150     }
00151 }
00152 
00153 
00154 Foam::Ostream& Foam::OPstream::write(const word& str)
00155 {
00156     write(char(token::WORD));
00157 
00158     size_t len = str.size();
00159     writeToBuffer(len);
00160     writeToBuffer(str.c_str(), len + 1, 1);
00161 
00162     return *this;
00163 }
00164 
00165 
00166 Foam::Ostream& Foam::OPstream::write(const string& str)
00167 {
00168     write(char(token::STRING));
00169 
00170     size_t len = str.size();
00171     writeToBuffer(len);
00172     writeToBuffer(str.c_str(), len + 1, 1);
00173 
00174     return *this;
00175 }
00176 
00177 
00178 Foam::Ostream& Foam::OPstream::writeQuoted(const std::string& str, const bool)
00179 {
00180     write(char(token::STRING));
00181 
00182     size_t len = str.size();
00183     writeToBuffer(len);
00184     writeToBuffer(str.c_str(), len + 1, 1);
00185 
00186     return *this;
00187 }
00188 
00189 
00190 Foam::Ostream& Foam::OPstream::write(const label val)
00191 {
00192     write(char(token::LABEL));
00193     writeToBuffer(val);
00194     return *this;
00195 }
00196 
00197 
00198 Foam::Ostream& Foam::OPstream::write(const floatScalar val)
00199 {
00200     write(char(token::FLOAT_SCALAR));
00201     writeToBuffer(val);
00202     return *this;
00203 }
00204 
00205 
00206 Foam::Ostream& Foam::OPstream::write(const doubleScalar val)
00207 {
00208     write(char(token::DOUBLE_SCALAR));
00209     writeToBuffer(val);
00210     return *this;
00211 }
00212 
00213 
00214 Foam::Ostream& Foam::OPstream::write(const char* data, std::streamsize count)
00215 {
00216     if (format() != BINARY)
00217     {
00218         FatalErrorIn("Ostream::write(const char*, std::streamsize)")
00219             << "stream format not binary"
00220             << Foam::abort(FatalError);
00221     }
00222 
00223     writeToBuffer(data, count, 8);
00224 
00225     return *this;
00226 }
00227 
00228 
00229 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd