unaligned access
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOstreams / Pstreams / OPwrite.C
blob41f239c9adf7bf201ebc257a8412875524b9e232
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Write primitive and binary block from OPstream
28 \*---------------------------------------------------------------------------*/
30 #include "error.H"
32 #include "OPstream.H"
33 #include "int.H"
34 #include "token.H"
36 #include <cctype>
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 namespace Foam
43 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
45 OPstream::OPstream
47     const commsTypes commsType,
48     const int toProcNo,
49     const label bufSize,
50     streamFormat format,
51     versionNumber version
54     Pstream(commsType, bufSize),
55     Ostream(format, version),
56     toProcNo_(toProcNo)
58     setOpened();
59     setGood();
61     if (!bufSize)
62     {
63         buf_.setSize(1000);
64     }
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 Ostream& OPstream::write(const token&)
72     notImplemented("Ostream& OPstream::write(const token&)");
73     setBad();
74     return *this;
78 Ostream& OPstream::write(const char c)
80     if (!isspace(c))
81     {
82         writeToBuffer(c);
83     }
85     return *this;
89 Ostream& OPstream::write(const char* s)
91     word nonWhiteChars(string::validate<word>(s));
93     if (nonWhiteChars.size() == 0)
94     {
95         return *this;
96     }
97     else if (nonWhiteChars.size() == 1)
98     {
99         return write(nonWhiteChars.c_str()[1]);
100     }
101     else
102     {
103         return write(nonWhiteChars);
104     }
108 Ostream& OPstream::write(const word& w)
110     write(char(token::WORD));
112     size_t ws = w.size();
113     writeToBuffer(ws);
114     writeToBuffer(w.c_str(), ws + 1, 1);
116     return *this;
120 Ostream& OPstream::write(const string& s)
122     write(char(token::STRING));
124     size_t ss = s.size();
125     writeToBuffer(ss);
126     writeToBuffer(s.c_str(), ss + 1, 1);
128     return *this;
132 Ostream& OPstream::write(const label l)
134     write(char(token::LABEL));
136     writeToBuffer(l);
138     return *this;
142 Ostream& OPstream::write(const floatScalar s)
144     write(char(token::FLOAT_SCALAR));
146     writeToBuffer(s);
148     return *this;
152 Ostream& OPstream::write(const doubleScalar s)
154     write(char(token::DOUBLE_SCALAR));
156     writeToBuffer(s);
158     return *this;
162 Ostream& OPstream::write(const char* data, std::streamsize count)
164     if (format() != BINARY)
165     {
166         FatalErrorIn("Ostream::write(const char*, std::streamsize)")
167             << "stream format not binary"
168             << Foam::abort(FatalError);
169     }
171     writeToBuffer(data, count, 8);
173     return *this;
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 } // End namespace Foam
181 // ************************************************************************* //