1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 Write primitive and binary block from OPstream
27 \*---------------------------------------------------------------------------*/
37 // * * * * * * * * * * * * * Private member functions * * * * * * * * * * * //
40 inline void Foam::OPstream::writeToBuffer(const T& t)
42 writeToBuffer(&t, sizeof(T), sizeof(T));
46 inline void Foam::OPstream::writeToBuffer(const char& c)
48 if (size_t(buf_.size()) < bufPosition_ + 1U)
53 buf_[bufPosition_] = c;
58 inline void Foam::OPstream::writeToBuffer
65 label oldPos = bufPosition_;
69 // Align bufPosition. Pads bufPosition_ - oldPos characters.
70 bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
73 if (size_t(buf_.size()) < bufPosition_ + count)
75 enlargeBuffer(bufPosition_ - oldPos + count);
78 register char* bufPtr = &buf_[bufPosition_];
79 register const char* dataPtr = reinterpret_cast<const char*>(data);
80 register size_t i = count;
81 while (i--) *bufPtr++ = *dataPtr++;
83 bufPosition_ += count;
88 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
90 Foam::OPstream::OPstream
92 const commsTypes commsType,
99 Pstream(commsType, bufSize),
100 Ostream(format, version),
113 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
115 Foam::Ostream& Foam::OPstream::write(const token&)
117 notImplemented("Ostream& OPstream::write(const token&)");
123 Foam::Ostream& Foam::OPstream::write(const char c)
134 Foam::Ostream& Foam::OPstream::write(const char* str)
136 word nonWhiteChars(string::validate<word>(str));
138 if (nonWhiteChars.size() == 1)
140 return write(nonWhiteChars.c_str()[1]);
142 else if (nonWhiteChars.size())
144 return write(nonWhiteChars);
153 Foam::Ostream& Foam::OPstream::write(const word& str)
155 write(char(token::WORD));
157 size_t len = str.size();
159 writeToBuffer(str.c_str(), len + 1, 1);
165 Foam::Ostream& Foam::OPstream::write(const string& str)
167 write(char(token::STRING));
169 size_t len = str.size();
171 writeToBuffer(str.c_str(), len + 1, 1);
177 Foam::Ostream& Foam::OPstream::writeQuoted(const std::string& str, const bool)
179 write(char(token::STRING));
181 size_t len = str.size();
183 writeToBuffer(str.c_str(), len + 1, 1);
189 Foam::Ostream& Foam::OPstream::write(const label val)
191 write(char(token::LABEL));
197 Foam::Ostream& Foam::OPstream::write(const floatScalar val)
199 write(char(token::FLOAT_SCALAR));
205 Foam::Ostream& Foam::OPstream::write(const doubleScalar val)
207 write(char(token::DOUBLE_SCALAR));
213 Foam::Ostream& Foam::OPstream::write(const longDoubleScalar val)
215 write(char(token::LONG_DOUBLE_SCALAR));
221 Foam::Ostream& Foam::OPstream::write(const char* data, std::streamsize count)
223 if (format() != BINARY)
225 FatalErrorIn("Ostream::write(const char*, std::streamsize)")
226 << "stream format not binary"
227 << Foam::abort(FatalError);
230 writeToBuffer(data, count, 8);
236 // ************************************************************************* //