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_EXPORT std::basic_ios<char>;
40  template class PT_EXPORT std::basic_istream<char>;
41  template class PT_EXPORT std::basic_ostream<char>;
42  template class PT_EXPORT std::basic_iostream<char>;
43 #endif
44 
45 #if defined(_MSC_VER)
46  template class PT_EXPORT std::basic_ios<Pt::Char>;
47  template class PT_EXPORT std::basic_istream<Pt::Char>;
48  template class PT_EXPORT std::basic_ostream<Pt::Char>;
49  template class PT_EXPORT std::basic_iostream<Pt::Char>;
50 #endif
51 
52 namespace Pt {
53 
56 template <typename CharT, typename TraitsT = std::char_traits<CharT> >
57 class BasicIStream : public std::basic_istream<CharT, TraitsT>
58 {
59  public:
60  typedef CharT char_type;
61  typedef TraitsT traits_type;
62  typedef typename TraitsT::int_type int_type;
63  typedef typename TraitsT::pos_type pos_type;
64  typedef typename TraitsT::off_type off_type;
65 
66  public:
68  explicit BasicIStream(BasicStreamBuffer<CharT>* sb = 0);
69 
72  {}
73 
80  std::streamsize peeksome(CharT* buffer, std::streamsize n);
81 
84  { return _buffer; }
85 
88  {
89  _buffer = sb;
90  this->rdbuf(sb);
91  }
92 
93  private:
94  BasicStreamBuffer<CharT>* _buffer;
95 };
96 
97 
100 template <typename CharT, typename TraitsT = std::char_traits<CharT> >
101 class BasicOStream : public std::basic_ostream<CharT, TraitsT>
102 {
103  public:
104  typedef CharT char_type;
105  typedef TraitsT traits_type;
106  typedef typename TraitsT::int_type int_type;
107  typedef typename TraitsT::pos_type pos_type;
108  typedef typename TraitsT::off_type off_type;
109 
110  public:
112  explicit BasicOStream(BasicStreamBuffer<CharT>* sb = 0);
113 
116  {}
117 
120  std::streamsize writesome(CharT* buffer, std::streamsize n);
121 
124  { return _buffer; }
125 
128  {
129  _buffer = sb;
130  this->rdbuf(sb);
131  }
132 
133  private:
134  BasicStreamBuffer<CharT>* _buffer;
135 };
136 
137 
140 template <typename CharT, typename TraitsT = std::char_traits<CharT> >
141 class BasicIOStream : public std::basic_iostream<CharT, TraitsT>
142 {
143  public:
144  typedef CharT char_type;
145  typedef TraitsT traits_type;
146  typedef typename TraitsT::int_type int_type;
147  typedef typename TraitsT::pos_type pos_type;
148  typedef typename TraitsT::off_type off_type;
149 
150  public:
152  explicit BasicIOStream(BasicStreamBuffer<CharT>* sb = 0);
153 
156  {}
157 
164  std::streamsize peeksome(CharT* buffer, std::streamsize n);
165 
168  std::streamsize writesome(CharT* buffer, std::streamsize n);
169 
172  { return _buffer; }
173 
176  {
177  _buffer = sb;
178  this->rdbuf(sb);
179  }
180 
181  private:
182  BasicStreamBuffer<CharT>* _buffer;
183 };
184 
185 
186 template <typename CharT, typename TraitsT>
188 : std::basic_istream<CharT>(sb)
189 , _buffer(sb)
190 {
191 }
192 
193 
194 template <typename CharT, typename TraitsT>
195 inline std::streamsize BasicIStream<CharT, TraitsT>::peeksome(CharT* buffer, std::streamsize n)
196 {
197  if(_buffer && this->rdbuf() == _buffer)
198  return _buffer->speekn(buffer, n);
199 
200  if(n > 0)
201  {
202  buffer[0] = this->peek();
203  return 1;
204  }
205 
206  return 0;
207 }
208 
209 
210 template <typename CharT, typename TraitsT>
212 : std::basic_ostream<CharT>(sb)
213 , _buffer(sb)
214 {
215 }
216 
217 
218 template <typename CharT, typename TraitsT>
219 inline std::streamsize BasicOStream<CharT, TraitsT>::writesome(CharT* buffer, std::streamsize n)
220 {
221  if( ! _buffer || this->rdbuf() != _buffer )
222  return 0;
223 
224  std::streamsize avail = _buffer->out_avail();
225  if(avail == 0)
226  {
227  return 0;
228  }
229 
230  n = std::min(avail, n);
231  return _buffer->sputn(buffer, n);
232 }
233 
234 
235 template <typename CharT, typename TraitsT>
237 : std::basic_iostream<CharT>(sb)
238 , _buffer(sb)
239 {
240 }
241 
242 
243 template <typename CharT, typename TraitsT>
244 inline std::streamsize BasicIOStream<CharT, TraitsT>::peeksome(CharT* buffer, std::streamsize n)
245 {
246  if(_buffer && this->rdbuf() == _buffer)
247  return _buffer->speekn(buffer, n);
248 
249  if(n > 0)
250  {
251  buffer[0] = this->peek();
252  return 1;
253  }
254 
255  return 0;
256 }
257 
258 
259 template <typename CharT, typename TraitsT>
260 inline std::streamsize BasicIOStream<CharT, TraitsT>::writesome(CharT* buffer, std::streamsize n)
261 {
262  if( ! _buffer || this->rdbuf() != _buffer )
263  return 0;
264 
265  std::streamsize avail = _buffer->out_avail();
266  if(avail == 0)
267  {
268  return 0;
269  }
270 
271  n = std::min(avail, n);
272  return _buffer->sputn(buffer, n);
273 }
274 
275 } // namespace Pt
276 
277 #endif
~BasicOStream()
Destructor.
Definition: IOStream.h:115
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:127
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:175
BasicIOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition: IOStream.h:236
std::streamsize writesome(CharT *buffer, std::streamsize n)
Write as much data as fits in buffer.
Definition: IOStream.h:219
~BasicIStream()
Destructor.
Definition: IOStream.h:71
std::streamsize peeksome(CharT *buffer, std::streamsize n)
Peeks bytes in the stream buffer.
Definition: IOStream.h:195
Input stream.
Definition: IOStream.h:57
Input/Output stream.
Definition: IOStream.h:141
BasicOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition: IOStream.h:211
Output stream.
Definition: IOStream.h:101
BasicStreamBuffer< CharT > * buffer()
Returns the buffer.
Definition: IOStream.h:123
std::streamsize writesome(CharT *buffer, std::streamsize n)
Write as much data as fits in buffer.
Definition: IOStream.h:260
BasicStreamBuffer< CharT > * buffer()
Returns the buffer.
Definition: IOStream.h:83
BasicStreamBuffer< CharT > * buffer()
Returns the buffer.
Definition: IOStream.h:171
~BasicIOStream()
Destructor.
Definition: IOStream.h:155
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:87
std::streamsize peeksome(CharT *buffer, std::streamsize n)
Peeks bytes in the stream buffer.
Definition: IOStream.h:244