FreeFOAM The Cross-Platform CFD Toolkit
OPstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25  Write primitive and binary block from OPstream
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <OpenFOAM/error.H>
30 
31 #include "OPstream.H"
32 #include <OpenFOAM/int.H>
33 #include <OpenFOAM/token.H>
34 
35 #include <cctype>
36 
37 // * * * * * * * * * * * * * Private member functions * * * * * * * * * * * //
38 
39 template<class T>
40 inline void Foam::OPstream::writeToBuffer(const T& t)
41 {
42  writeToBuffer(&t, sizeof(T), sizeof(T));
43 }
44 
45 
46 inline void Foam::OPstream::writeToBuffer(const char& c)
47 {
48  if (size_t(buf_.size()) < bufPosition_ + 1U)
49  {
50  enlargeBuffer(1);
51  }
52 
53  buf_[bufPosition_] = c;
54  bufPosition_ ++;
55 }
56 
57 
58 inline void Foam::OPstream::writeToBuffer
59 (
60  const void* data,
61  size_t count,
62  size_t align
63 )
64 {
65  label oldPos = bufPosition_;
66 
67  if (align > 1)
68  {
69  // Align bufPosition. Pads bufPosition_ - oldPos characters.
70  bufPosition_ = align + ((bufPosition_ - 1) & ~(align - 1));
71  }
72 
73  if (size_t(buf_.size()) < bufPosition_ + count)
74  {
75  enlargeBuffer(bufPosition_ - oldPos + count);
76  }
77 
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++;
82 
83  bufPosition_ += count;
84 }
85 
86 
87 
88 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
89 
90 Foam::autoPtr<Foam::OPstreamImpl> Foam::OPstream::impl_;
91 
92 // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * //
93 
95 (
96  const commsTypes commsType,
97  const int toProcNo,
98  const label bufSize,
100  versionNumber version
101 )
102 :
103  Pstream(commsType, bufSize),
104  Ostream(format, version),
105  toProcNo_(toProcNo)
106 {
107  setOpened();
108  setGood();
109 
110  if (!bufSize)
111  {
112  buf_.setSize(1000);
113  }
114 }
115 
116 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
117 
119 {
120  impl()->flush(commsType_, toProcNo_, buf_.begin(), bufPosition_);
121 }
122 
123 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
124 
126 {
127  notImplemented("Ostream& OPstream::write(const token&)");
128  setBad();
129  return *this;
130 }
131 
132 
134 {
135  if (!isspace(c))
136  {
137  writeToBuffer(c);
138  }
139 
140  return *this;
141 }
142 
143 
145 {
146  word nonWhiteChars(string::validate<word>(str));
147 
148  if (nonWhiteChars.size() == 1)
149  {
150  return write(nonWhiteChars.c_str()[1]);
151  }
152  else if (nonWhiteChars.size())
153  {
154  return write(nonWhiteChars);
155  }
156  else
157  {
158  return *this;
159  }
160 }
161 
162 
164 {
165  write(char(token::WORD));
166 
167  size_t len = str.size();
168  writeToBuffer(len);
169  writeToBuffer(str.c_str(), len + 1, 1);
170 
171  return *this;
172 }
173 
174 
176 {
177  write(char(token::STRING));
178 
179  size_t len = str.size();
180  writeToBuffer(len);
181  writeToBuffer(str.c_str(), len + 1, 1);
182 
183  return *this;
184 }
185 
186 
187 Foam::Ostream& Foam::OPstream::writeQuoted(const std::string& str, const bool)
188 {
189  write(char(token::STRING));
190 
191  size_t len = str.size();
192  writeToBuffer(len);
193  writeToBuffer(str.c_str(), len + 1, 1);
194 
195  return *this;
196 }
197 
198 
200 {
201  write(char(token::LABEL));
202  writeToBuffer(val);
203  return *this;
204 }
205 
206 
208 {
209  write(char(token::FLOAT_SCALAR));
210  writeToBuffer(val);
211  return *this;
212 }
213 
214 
216 {
217  write(char(token::DOUBLE_SCALAR));
218  writeToBuffer(val);
219  return *this;
220 }
221 
222 
223 Foam::Ostream& Foam::OPstream::write(const char* data, std::streamsize count)
224 {
225  if (format() != BINARY)
226  {
227  FatalErrorIn("Ostream::write(const char*, std::streamsize)")
228  << "stream format not binary"
230  }
231 
232  writeToBuffer(data, count, 8);
233 
234  return *this;
235 }
236 
237 
238 // ************************ vim: set sw=4 sts=4 et: ************************ //