textstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 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 cxxtools_BasicTextStream_h
30 #define cxxtools_BasicTextStream_h
31 
32 #include <cxxtools/textbuffer.h>
33 #include <iostream>
34 
35 namespace cxxtools
36 {
37 
60 template <typename CharT, typename ByteT>
61 class BasicTextIStream : public std::basic_istream<CharT>
62 {
63  public:
64  typedef ByteT extern_type;
65  typedef CharT intern_type;
66  typedef CharT char_type;
67  typedef typename std::char_traits<CharT> traits_type;
68  typedef typename traits_type::int_type int_type;
69  typedef typename traits_type::pos_type pos_type;
70  typedef typename traits_type::off_type off_type;
71  typedef std::basic_istream<extern_type> StreamType;
73 
74  public:
83  : std::basic_istream<intern_type>(0)
84  , _buffer( &is, codec )
85  {
86  std::basic_istream<CharT>::init(&_buffer);
87  std::basic_istream<CharT>::exceptions(is.exceptions());
88  }
89 
90  explicit BasicTextIStream(CodecType* codec)
91  : std::basic_istream<intern_type>(0)
92  , _buffer( 0, codec )
93  {
94  std::basic_istream<CharT>::init(&_buffer);
95  }
96 
99  { }
100 
101  void attach(StreamType& is)
102  {
103  _buffer.attach( is );
104  this->clear();
105  std::basic_istream<CharT>::exceptions(is.exceptions());
106  }
107 
108  void detach()
109  {
110  _buffer.detach();
111  this->clear();
112  }
113 
114  void terminate()
115  {
116  _buffer.terminate();
117  }
118 
120  { return _buffer; }
121 
122  private:
124 };
125 
126 
149 template <typename CharT, typename ByteT>
150 class BasicTextOStream : public std::basic_ostream<CharT>
151 {
152  public:
153  typedef ByteT extern_type;
154  typedef CharT intern_type;
155  typedef CharT char_type;
156  typedef typename std::char_traits<CharT> traits_type;
157  typedef typename traits_type::int_type int_type;
158  typedef typename traits_type::pos_type pos_type;
159  typedef typename traits_type::off_type off_type;
160  typedef std::basic_ostream<extern_type> StreamType;
162 
163  public:
172  : std::basic_ostream<intern_type>(0)
173  , _buffer( &os , codec )
174  {
175  std::basic_ostream<CharT>::init(&_buffer);
176  std::basic_ostream<CharT>::exceptions(os.exceptions());
177  }
178 
179  explicit BasicTextOStream(CodecType* codec)
180  : std::basic_ostream<intern_type>(0)
181  , _buffer( 0 , codec )
182  { std::basic_ostream<CharT>::init(&_buffer); }
183 
186  { }
187 
188  void attach(StreamType& os)
189  {
190  _buffer.attach( os );
191  std::basic_ostream<CharT>::exceptions(os.exceptions());
192  this->clear();
193  }
194 
195  void detach()
196  {
197  _buffer.detach();
198  this->clear();
199  }
200 
201  void terminate()
202  {
203  _buffer.terminate();
204  }
205 
207  { return _buffer; }
208 
209  private:
211 };
212 
235 template <typename CharT, typename ByteT>
236 class BasicTextStream : public std::basic_iostream<CharT>
237 {
238  public:
239  typedef ByteT extern_type;
240  typedef CharT intern_type;
241  typedef CharT char_type;
242  typedef typename std::char_traits<CharT> traits_type;
243  typedef typename traits_type::int_type int_type;
244  typedef typename traits_type::pos_type pos_type;
245  typedef typename traits_type::off_type off_type;
246  typedef std::basic_iostream<extern_type> StreamType;
248 
249  public:
259  : std::basic_iostream<intern_type>(0)
260  , _buffer( &ios, codec)
261  {
262  std::basic_iostream<CharT>::init(&_buffer);
263  std::basic_iostream<CharT>::exceptions(ios.exceptions());
264  }
265 
266  explicit BasicTextStream(CodecType* codec)
267  : std::basic_iostream<intern_type>(0)
268  , _buffer(0, codec)
269  { std::basic_iostream<CharT>::init(&_buffer); }
270 
273  { }
274 
275  void attach(StreamType& ios)
276  {
277  _buffer.attach( ios );
278  this->clear();
279  std::basic_iostream<CharT>::exceptions(ios.exceptions());
280  }
281 
282  void detach()
283  {
284  _buffer.detach();
285  this->clear();
286  }
287 
288  void terminate()
289  {
290  _buffer.terminate();
291  }
292 
294  { return _buffer; }
295 
296  private:
298 };
299 
300 
303 class TextIStream : public BasicTextIStream<Char, char>
304 {
305  public:
307 
308  public:
316  TextIStream(std::istream& is, Codec* codec);
317 
318  explicit TextIStream(Codec* codec);
319 
320  ~TextIStream();
321 };
322 
323 
326 class TextOStream : public BasicTextOStream<Char, char>
327 {
328  public:
330 
331  public:
339  TextOStream(std::ostream& os, Codec* codec);
340 
341  explicit TextOStream(Codec* codec);
342 
343  ~TextOStream();
344 };
345 
346 
349 class TextStream : public BasicTextStream<Char, char>
350 {
351  public:
353 
354  public:
362  TextStream(std::iostream& ios, Codec* codec);
363 
364  explicit TextStream(Codec* codec);
365 
366  ~TextStream();
367 };
368 
369 inline std::basic_ostream<Char>& operator<< (std::basic_ostream<Char>& out, wchar_t ch)
370 {
371  return out << Char(ch);
372 }
373 
374 inline std::basic_ostream<Char>& operator<< (std::basic_ostream<Char>& out, const wchar_t* str)
375 {
376  while (*str)
377  out << Char(*str++);
378  return out;
379 }
380 
381 } // namespace cxxtools
382 
383 #endif
384