IOStream.h
1/*
2 * Copyright (C) 2012 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_IOStream_h
30#define Pt_IOStream_h
31
32#include <Pt/Api.h>
33#include <Pt/StreamBuffer.h>
34#include <iostream>
35#include <algorithm>
36
37#if defined(_MSC_VER) && defined(_WIN32_WCE)
38 // alternatively compile with /FORCE:multiple
39 template class PT_API std::basic_ios<char>;
40 template class PT_API std::basic_istream<char>;
41 template class PT_API std::basic_ostream<char>;
42 template class PT_API std::basic_iostream<char>;
43#endif
44
45#if defined(_MSC_VER)
46 template class PT_API std::basic_ios<Pt::Char>;
47 template class PT_API std::basic_istream<Pt::Char>;
48 template class PT_API std::basic_ostream<Pt::Char>;
49 template class PT_API std::basic_iostream<Pt::Char>;
50#endif
51
52namespace Pt {
53
69template <typename CharT, typename TraitsT = std::char_traits<CharT> >
70class BasicIStream : public std::basic_istream<CharT, TraitsT>
71{
72 public:
73 typedef CharT char_type;
74 typedef TraitsT traits_type;
75 typedef typename TraitsT::int_type int_type;
76 typedef typename TraitsT::pos_type pos_type;
77 typedef typename TraitsT::off_type off_type;
78
79 public:
82
86
93 std::streamsize peeksome(CharT* buffer, std::streamsize n);
94
97 { return _buffer; }
98
101 {
102 _buffer = sb;
103 this->rdbuf(sb);
104 }
105
106 private:
108};
109
110
125template <typename CharT, typename TraitsT = std::char_traits<CharT> >
126class BasicOStream : public std::basic_ostream<CharT, TraitsT>
127{
128 public:
129 typedef CharT char_type;
130 typedef TraitsT traits_type;
131 typedef typename TraitsT::int_type int_type;
132 typedef typename TraitsT::pos_type pos_type;
133 typedef typename TraitsT::off_type off_type;
134
135 public:
138
142
145 std::streamsize writesome(CharT* buffer, std::streamsize n);
146
149 { return _buffer; }
150
153 {
154 _buffer = sb;
155 this->rdbuf(sb);
156 }
157
158 private:
160};
161
162
178template <typename CharT, typename TraitsT = std::char_traits<CharT> >
179class BasicIOStream : public std::basic_iostream<CharT, TraitsT>
180{
181 public:
182 typedef CharT char_type;
183 typedef TraitsT traits_type;
184 typedef typename TraitsT::int_type int_type;
185 typedef typename TraitsT::pos_type pos_type;
186 typedef typename TraitsT::off_type off_type;
187
188 public:
191
195
202 std::streamsize peeksome(CharT* buffer, std::streamsize n);
203
206 std::streamsize writesome(CharT* buffer, std::streamsize n);
207
210 { return _buffer; }
211
214 {
215 _buffer = sb;
216 this->rdbuf(sb);
217 }
218
219 private:
221};
222
223
224template <typename CharT, typename TraitsT>
226: std::basic_istream<CharT>(sb)
227, _buffer(sb)
228{
229}
230
231
232template <typename CharT, typename TraitsT>
233inline std::streamsize BasicIStream<CharT, TraitsT>::peeksome(CharT* buffer, std::streamsize n)
234{
235 if(_buffer && this->rdbuf() == _buffer)
236 return _buffer->speekn(buffer, n);
237
238 if(n > 0)
239 {
240 buffer[0] = this->peek();
241 return 1;
242 }
243
244 return 0;
245}
246
247
248template <typename CharT, typename TraitsT>
250: std::basic_ostream<CharT>(sb)
251, _buffer(sb)
252{
253}
254
255
256template <typename CharT, typename TraitsT>
257inline std::streamsize BasicOStream<CharT, TraitsT>::writesome(CharT* buffer, std::streamsize n)
258{
259 if( ! _buffer || this->rdbuf() != _buffer )
260 return 0;
261
262 std::streamsize avail = _buffer->out_avail();
263 if(avail == 0)
264 {
265 return 0;
266 }
267
268 n = std::min(avail, n);
269 return _buffer->sputn(buffer, n);
270}
271
272
273template <typename CharT, typename TraitsT>
275: std::basic_iostream<CharT>(sb)
276, _buffer(sb)
277{
278}
279
280
281template <typename CharT, typename TraitsT>
282inline std::streamsize BasicIOStream<CharT, TraitsT>::peeksome(CharT* buffer, std::streamsize n)
283{
284 if(_buffer && this->rdbuf() == _buffer)
285 return _buffer->speekn(buffer, n);
286
287 if(n > 0)
288 {
289 buffer[0] = this->peek();
290 return 1;
291 }
292
293 return 0;
294}
295
296
297template <typename CharT, typename TraitsT>
298inline std::streamsize BasicIOStream<CharT, TraitsT>::writesome(CharT* buffer, std::streamsize n)
299{
300 if( ! _buffer || this->rdbuf() != _buffer )
301 return 0;
302
303 std::streamsize avail = _buffer->out_avail();
304 if(avail == 0)
305 {
306 return 0;
307 }
308
309 n = std::min(avail, n);
310 return _buffer->sputn(buffer, n);
311}
312
313} // namespace Pt
314
315#endif
BasicStreamBuffer< char > * buffer()
Definition IOStream.h:209
std::streamsize writesome(CharT *buffer, std::streamsize n)
Write as much data as fits in buffer.
Definition IOStream.h:298
BasicIOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:274
~BasicIOStream()
Destructor.
Definition IOStream.h:193
std::streamsize peeksome(CharT *buffer, std::streamsize n)
Peeks bytes in the stream buffer.
Definition IOStream.h:282
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition IOStream.h:213
BasicStreamBuffer< char > * buffer()
Definition IOStream.h:96
std::streamsize peeksome(CharT *buffer, std::streamsize n)
Peeks bytes in the stream buffer.
Definition IOStream.h:233
~BasicIStream()
Destructor.
Definition IOStream.h:84
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition IOStream.h:100
BasicIStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:225
BasicStreamBuffer< char > * buffer()
Definition IOStream.h:148
std::streamsize writesome(CharT *buffer, std::streamsize n)
Write as much data as fits in buffer.
Definition IOStream.h:257
BasicOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:249
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition IOStream.h:152
~BasicOStream()
Destructor.
Definition IOStream.h:140
Stream buffer with peek and available output.
Definition StreamBuffer.h:75
Core module.
Definition Allocator.h:33