iostream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2008 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 #ifndef cxxtools_System_IOStream_h
29 #define cxxtools_System_IOStream_h
30 
31 #include <cxxtools/api.h>
32 #include <cxxtools/streambuffer.h>
33 #include <iostream>
34 #include <algorithm>
35 
36 namespace cxxtools {
37 
39 template <typename CharT>
40 class BasicIStream : public std::basic_istream<CharT>
41 {
42  public:
43  explicit BasicIStream(BasicStreamBuffer<CharT>* buffer = 0)
44  : std::basic_istream<CharT>( buffer ),
45  _buffer(buffer)
46  { }
47 
49  { }
50 
53  { return _buffer; }
54 
56  {
57  BasicStreamBuffer<CharT>* tmp = _buffer;
58  _buffer = buffer;
59  this->rdbuf(buffer);
60  return tmp;
61  }
62 
64 
69  std::streamsize peeksome(CharT* buffer, std::streamsize n)
70  {
71  if(this->rdbuf() == _buffer)
72  return _buffer->speekn(buffer, n);
73 
74  if(n > 0)
75  {
76  buffer[0] = this->peek();
77  return 1;
78  }
79 
80  return 0;
81  }
82 
83  private:
84  BasicStreamBuffer<CharT>* _buffer;
85 };
86 
87 
89 template <typename CharT>
90 class BasicOStream : public std::basic_ostream<CharT>
91 {
92  public:
93  explicit BasicOStream(BasicStreamBuffer<CharT>* buffer = 0)
94  : std::basic_ostream<CharT>( buffer ),
95  _buffer(buffer)
96  { }
97 
99  {}
100 
103  { return _buffer; }
104 
106  {
107  BasicStreamBuffer<CharT>* tmp = _buffer;
108  _buffer = buffer;
109  this->rdbuf(buffer);
110  return tmp;
111  }
112 
113  std::streamsize writesome(CharT* buffer, std::streamsize n)
114  {
115  std::basic_streambuf<CharT>* current = std::basic_ios<CharT>::rdbuf();
116  if(current != _buffer)
117  return 0;
118 
119  std::streamsize avail = _buffer->out_avail();
120  if(avail == 0)
121  {
122  return 0;
123  }
124 
125  n = std::min(avail, n);
126  return _buffer->sputn(buffer, n);
127  }
128 
129  private:
130  BasicStreamBuffer<CharT>* _buffer;
131 };
132 
133 
135 template <typename CharT>
136 class BasicIOStream : public std::basic_iostream<CharT>
137 {
138  public:
140  : std::basic_iostream<CharT>( buffer ),
141  _buffer(buffer)
142  { }
143 
145  { }
146 
149  { return _buffer; }
150 
152  {
153  BasicStreamBuffer<CharT>* tmp = _buffer;
154  _buffer = buffer;
155  this->rdbuf(buffer);
156  return tmp;
157  }
158 
160 
165  std::streamsize peeksome(CharT* buffer, std::streamsize n)
166  {
167  if(this->rdbuf() == _buffer)
168  return _buffer->speekn(buffer, n);
169 
170  if(n > 0)
171  {
172  buffer[0] = this->peek();
173  return 1;
174  }
175 
176  return 0;
177  }
178 
179  std::streamsize writesome(CharT* buffer, std::streamsize n)
180  {
181  std::basic_streambuf<CharT>* current = std::basic_ios<CharT>::rdbuf();
182  if(current != _buffer)
183  return 0;
184 
185  std::streamsize avail = _buffer->out_avail();
186  if(avail == 0)
187  {
188  return 0;
189  }
190 
191  n = std::min(avail, n);
192  return _buffer->sputn(buffer, n);
193  }
194 
195  private:
196  BasicStreamBuffer<CharT>* _buffer;
197 };
198 
199 
200 class CXXTOOLS_API IStream : public BasicIStream<char>
201 {
202  public:
203  explicit IStream(size_t bufferSize = 8192, bool extend = false);
204 
205  ~IStream();
206 
207  explicit IStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
208 
209  StreamBuffer& buffer();
210 
211  IODevice* attachDevice(IODevice& device);
212 
213  IODevice* attachedDevice();
214 
215  private:
216  StreamBuffer _buffer;
217 };
218 
219 
220 class CXXTOOLS_API OStream : public BasicOStream<char>
221 {
222  public:
223  explicit OStream(size_t bufferSize = 8192, bool extend = false);
224 
225  explicit OStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
226 
227  ~OStream();
228 
229  StreamBuffer& buffer();
230 
231  IODevice* attachDevice(IODevice& device);
232 
233  IODevice* attachedDevice();
234 
235  private:
236  StreamBuffer _buffer;
237 };
238 
239 
240 class CXXTOOLS_API IOStream : public BasicIOStream<char>
241 {
242  public:
243  explicit IOStream(size_t bufferSize = 8192, bool extend = false);
244 
245  explicit IOStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
246 
247  ~IOStream();
248 
249  StreamBuffer& buffer();
250 
251  IODevice* attachDevice(IODevice& device);
252 
253  IODevice* attachedDevice();
254 
255  private:
256  StreamBuffer _buffer;
257 };
258 
259 } // !namespace cxxtools
260 
261 #endif
262