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/api.h>
33 #include <cxxtools/textbuffer.h>
34 #include <iostream>
35 
36 namespace cxxtools
37 {
38 
61 template <typename CharT, typename ByteT>
62 class BasicTextIStream : public std::basic_istream<CharT>
63 {
64  public:
65  typedef ByteT extern_type;
66  typedef CharT intern_type;
67  typedef CharT char_type;
68  typedef typename std::char_traits<CharT> traits_type;
69  typedef typename traits_type::int_type int_type;
70  typedef typename traits_type::pos_type pos_type;
71  typedef typename traits_type::off_type off_type;
72  typedef std::basic_istream<extern_type> StreamType;
74 
75  public:
84  : std::basic_istream<intern_type>(0)
85  , _buffer( &is, codec )
86  {
87  std::basic_istream<CharT>::init(&_buffer);
88  std::basic_istream<CharT>::exceptions(is.exceptions());
89  }
90 
91  explicit BasicTextIStream(CodecType* codec)
92  : std::basic_istream<intern_type>(0)
93  , _buffer( 0, codec )
94  {
95  std::basic_istream<CharT>::init(&_buffer);
96  }
97 
100  { }
101 
102  void attach(StreamType& is)
103  {
104  _buffer.attach( is );
105  this->clear();
106  std::basic_istream<CharT>::exceptions(is.exceptions());
107  }
108 
109  void detach()
110  {
111  _buffer.detach();
112  this->clear();
113  }
114 
115  void terminate()
116  {
117  _buffer.terminate();
118  }
119 
121  { return _buffer; }
122 
123  private:
125 };
126 
127 
150 template <typename CharT, typename ByteT>
151 class BasicTextOStream : public std::basic_ostream<CharT>
152 {
153  public:
154  typedef ByteT extern_type;
155  typedef CharT intern_type;
156  typedef CharT char_type;
157  typedef typename std::char_traits<CharT> traits_type;
158  typedef typename traits_type::int_type int_type;
159  typedef typename traits_type::pos_type pos_type;
160  typedef typename traits_type::off_type off_type;
161  typedef std::basic_ostream<extern_type> StreamType;
163 
164  public:
173  : std::basic_ostream<intern_type>(0)
174  , _buffer( &os , codec )
175  {
176  std::basic_ostream<CharT>::init(&_buffer);
177  std::basic_ostream<CharT>::exceptions(os.exceptions());
178  }
179 
180  explicit BasicTextOStream(CodecType* codec)
181  : std::basic_ostream<intern_type>(0)
182  , _buffer( 0 , codec )
183  { std::basic_ostream<CharT>::init(&_buffer); }
184 
187  { }
188 
189  void attach(StreamType& os)
190  {
191  _buffer.attach( os );
192  std::basic_ostream<CharT>::exceptions(os.exceptions());
193  this->clear();
194  }
195 
196  void detach()
197  {
198  _buffer.detach();
199  this->clear();
200  }
201 
202  void terminate()
203  {
204  _buffer.terminate();
205  }
206 
208  { return _buffer; }
209 
210  private:
212 };
213 
236 template <typename CharT, typename ByteT>
237 class BasicTextStream : public std::basic_iostream<CharT>
238 {
239  public:
240  typedef ByteT extern_type;
241  typedef CharT intern_type;
242  typedef CharT char_type;
243  typedef typename std::char_traits<CharT> traits_type;
244  typedef typename traits_type::int_type int_type;
245  typedef typename traits_type::pos_type pos_type;
246  typedef typename traits_type::off_type off_type;
247  typedef std::basic_iostream<extern_type> StreamType;
249 
250  public:
260  : std::basic_iostream<intern_type>(0)
261  , _buffer( &ios, codec)
262  {
263  std::basic_iostream<CharT>::init(&_buffer);
264  std::basic_iostream<CharT>::exceptions(ios.exceptions());
265  }
266 
267  explicit BasicTextStream(CodecType* codec)
268  : std::basic_iostream<intern_type>(0)
269  , _buffer(0, codec)
270  { std::basic_iostream<CharT>::init(&_buffer); }
271 
274  { }
275 
276  void attach(StreamType& ios)
277  {
278  _buffer.attach( ios );
279  this->clear();
280  std::basic_iostream<CharT>::exceptions(ios.exceptions());
281  }
282 
283  void detach()
284  {
285  _buffer.detach();
286  this->clear();
287  }
288 
289  void terminate()
290  {
291  _buffer.terminate();
292  }
293 
295  { return _buffer; }
296 
297  private:
299 };
300 
301 
304 class CXXTOOLS_API TextIStream : public BasicTextIStream<Char, char>
305 {
306  public:
308 
309  public:
317  TextIStream(std::istream& is, Codec* codec);
318 
319  explicit TextIStream(Codec* codec);
320 
321  ~TextIStream();
322 };
323 
324 
327 class CXXTOOLS_API TextOStream : public BasicTextOStream<Char, char>
328 {
329  public:
331 
332  public:
340  TextOStream(std::ostream& os, Codec* codec);
341 
342  explicit TextOStream(Codec* codec);
343 
344  ~TextOStream();
345 };
346 
347 
350 class CXXTOOLS_API TextStream : public BasicTextStream<Char, char>
351 {
352  public:
354 
355  public:
363  TextStream(std::iostream& ios, Codec* codec);
364 
365  explicit TextStream(Codec* codec);
366 
367  ~TextStream();
368 };
369 
370 inline std::basic_ostream<Char>& operator<< (std::basic_ostream<Char>& out, wchar_t ch)
371 {
372  return out << Char(ch);
373 }
374 
375 inline std::basic_ostream<Char>& operator<< (std::basic_ostream<Char>& out, const wchar_t* str)
376 {
377  while (*str)
378  out << Char(*str++);
379  return out;
380 }
381 
382 } // namespace cxxtools
383 
384 #endif
385