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