BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / IOstreams / Pstreams / UOPstream.C
blobc257826c0473d286c03b3a253a19ba2702570a2b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Write primitive and binary block from UOPstream
27 \*---------------------------------------------------------------------------*/
29 #include "error.H"
31 #include "UOPstream.H"
32 #include "int.H"
33 #include "token.H"
35 #include <cctype>
37 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
39 template<class T>
40 inline void Foam::UOPstream::writeToBuffer(const T& t)
42     writeToBuffer(&t, sizeof(T), sizeof(T));
46 inline void Foam::UOPstream::writeToBuffer(const char& c)
48     if (!sendBuf_.capacity())
49     {
50         sendBuf_.setCapacity(1000);
51     }
52     sendBuf_.append(c);
56 inline void Foam::UOPstream::writeToBuffer
58     const void* data,
59     size_t count,
60     size_t align
63     if (!sendBuf_.capacity())
64     {
65         sendBuf_.setCapacity(1000);
66     }
68     label alignedPos = sendBuf_.size();
70     if (align > 1)
71     {
72         // Align bufPosition. Pads sendBuf_.size() - oldPos characters.
73         alignedPos = align + ((sendBuf_.size() - 1) & ~(align - 1));
74     }
76     // Extend if necessary
77     sendBuf_.setSize(alignedPos + count);
79     register const char* dataPtr = reinterpret_cast<const char*>(data);
80     register size_t i = count;
81     while (i--) sendBuf_[alignedPos++] = *dataPtr++;
86 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
88 Foam::UOPstream::UOPstream
90     const commsTypes commsType,
91     const int toProcNo,
92     DynamicList<char>& sendBuf,
93     const int tag,
94     const bool sendAtDestruct,
95     streamFormat format,
96     versionNumber version
99     UPstream(commsType),
100     Ostream(format, version),
101     toProcNo_(toProcNo),
102     sendBuf_(sendBuf),
103     tag_(tag),
104     sendAtDestruct_(sendAtDestruct)
106     setOpened();
107     setGood();
111 Foam::UOPstream::UOPstream(const int toProcNo, PstreamBuffers& buffers)
113     UPstream(buffers.commsType_),
114     Ostream(buffers.format_, buffers.version_),
115     toProcNo_(toProcNo),
116     sendBuf_(buffers.sendBuf_[toProcNo]),
117     tag_(buffers.tag_),
118     sendAtDestruct_(buffers.commsType_ != UPstream::nonBlocking)
120     setOpened();
121     setGood();
125 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
127 Foam::UOPstream::~UOPstream()
129     if (sendAtDestruct_)
130     {
131         if
132         (
133            !UOPstream::write
134             (
135                 commsType_,
136                 toProcNo_,
137                 sendBuf_.begin(),
138                 sendBuf_.size(),
139                 tag_
140             )
141         )
142         {
143             FatalErrorIn("UOPstream::~UOPstream()")
144                 << "Failed sending outgoing message of size " << sendBuf_.size()
145                 << " to processor " << toProcNo_
146                 << Foam::abort(FatalError);
147         }
148     }
152 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
154 Foam::Ostream& Foam::UOPstream::write(const token&)
156     notImplemented("Ostream& UOPstream::write(const token&)");
157     setBad();
158     return *this;
162 Foam::Ostream& Foam::UOPstream::write(const char c)
164     if (!isspace(c))
165     {
166         writeToBuffer(c);
167     }
169     return *this;
173 Foam::Ostream& Foam::UOPstream::write(const char* str)
175     word nonWhiteChars(string::validate<word>(str));
177     if (nonWhiteChars.size() == 1)
178     {
179         return write(nonWhiteChars.c_str()[1]);
180     }
181     else if (nonWhiteChars.size())
182     {
183         return write(nonWhiteChars);
184     }
185     else
186     {
187         return *this;
188     }
192 Foam::Ostream& Foam::UOPstream::write(const word& str)
194     write(char(token::WORD));
196     size_t len = str.size();
197     writeToBuffer(len);
198     writeToBuffer(str.c_str(), len + 1, 1);
200     return *this;
204 Foam::Ostream& Foam::UOPstream::write(const string& str)
206     write(char(token::STRING));
208     size_t len = str.size();
209     writeToBuffer(len);
210     writeToBuffer(str.c_str(), len + 1, 1);
212     return *this;
216 Foam::Ostream& Foam::UOPstream::writeQuoted
218     const std::string& str,
219     const bool quoted
222     if (quoted)
223     {
224         write(char(token::STRING));
225     }
226     else
227     {
228         write(char(token::WORD));
229     }
231     size_t len = str.size();
232     writeToBuffer(len);
233     writeToBuffer(str.c_str(), len + 1, 1);
235     return *this;
239 Foam::Ostream& Foam::UOPstream::write(const label val)
241     write(char(token::LABEL));
242     writeToBuffer(val);
243     return *this;
247 Foam::Ostream& Foam::UOPstream::write(const floatScalar val)
249     write(char(token::FLOAT_SCALAR));
250     writeToBuffer(val);
251     return *this;
255 Foam::Ostream& Foam::UOPstream::write(const doubleScalar val)
257     write(char(token::DOUBLE_SCALAR));
258     writeToBuffer(val);
259     return *this;
263 Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
265     if (format() != BINARY)
266     {
267         FatalErrorIn("Ostream::write(const char*, std::streamsize)")
268             << "stream format not binary"
269             << Foam::abort(FatalError);
270     }
272     writeToBuffer(data, count, 8);
274     return *this;
278 void Foam::UOPstream::print(Ostream& os) const
280     os  << "Writing from processor " << toProcNo_
281         << " to processor " << myProcNo() << Foam::endl;
285 // ************************************************************************* //