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