TextStream.h
1 /*
2  * Copyright (C) 2004-2013 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_TextStream_h
30 #define Pt_TextStream_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/String.h>
34 #include <Pt/TextBuffer.h>
35 #include <Pt/IOStream.h>
36 
37 namespace Pt {
38 
46 template <typename CharT, typename ByteT>
47 class BasicTextIStream : public BasicIStream<CharT>
48 {
49  public:
51  typedef ByteT extern_type;
52 
54  typedef CharT intern_type;
55 
57  typedef CharT char_type;
58 
60  typedef typename std::char_traits<CharT> traits_type;
61 
63  typedef typename traits_type::int_type int_type;
64 
66  typedef typename traits_type::pos_type pos_type;
67 
69  typedef typename traits_type::off_type off_type;
70 
72  typedef std::basic_istream<extern_type> StreamType;
73 
76 
77  public:
87  , _tbuffer( is, codec )
88  {
89  this->setBuffer(&_tbuffer);
90  }
91 
99  , _tbuffer(codec )
100  {
101  this->setBuffer(&_tbuffer);
102  }
103 
107  { }
108 
112  {
113  return _tbuffer.codec();
114  }
115 
122  {
123  _tbuffer.setCodec(codec);
124  }
125 
128  void attach(StreamType& is)
129  {
130  _tbuffer.attach(is);
131  }
132 
135  void detach()
136  {
137  _tbuffer.detach();
138  }
139 
142  void discard()
143  {
144  _tbuffer.discard();
145  }
146 
152  void reset()
153  {
154  _tbuffer.reset();
155  }
156 
162  void reset(StreamType& is)
163  {
164  _tbuffer.reset(is);
165  }
166 
170  { return _tbuffer; }
171 
172  private:
174 };
175 
176 
185 template <typename CharT, typename ByteT>
186 class BasicTextOStream : public BasicOStream<CharT>
187 {
188  public:
190  typedef ByteT extern_type;
191 
193  typedef CharT intern_type;
194 
196  typedef CharT char_type;
197 
199  typedef typename std::char_traits<CharT> traits_type;
200 
202  typedef typename traits_type::int_type int_type;
203 
205  typedef typename traits_type::pos_type pos_type;
206 
208  typedef typename traits_type::off_type off_type;
209 
211  typedef std::basic_ostream<extern_type> StreamType;
212 
215 
216  public:
226  , _tbuffer( os , codec )
227  {
228  this->setBuffer(&_tbuffer);
229  }
230 
238  , _tbuffer( codec )
239  {
240  this->setBuffer(&_tbuffer);
241  }
242 
246  { }
247 
251  {
252  return _tbuffer.codec();
253  }
254 
261  {
262  _tbuffer.setCodec(codec);
263  }
264 
267  void attach(StreamType& os)
268  {
269  _tbuffer.attach(os);
270  }
271 
274  void detach()
275  {
276  _tbuffer.detach();
277  }
278 
281  void discard()
282  {
283  _tbuffer.discard();
284  }
285 
291  void reset()
292  {
293  _tbuffer.reset();
294  }
295 
301  void reset(StreamType& os)
302  {
303  _tbuffer.reset(os);
304  }
305 
309  { return _tbuffer; }
310 
311  private:
313 };
314 
324 template <typename CharT, typename ByteT>
325 class BasicTextStream : public BasicIOStream<CharT>
326 {
327  public:
329  typedef ByteT extern_type;
330 
332  typedef CharT intern_type;
333 
335  typedef CharT char_type;
336 
338  typedef typename std::char_traits<CharT> traits_type;
339 
341  typedef typename traits_type::int_type int_type;
342 
344  typedef typename traits_type::pos_type pos_type;
345 
347  typedef typename traits_type::off_type off_type;
348 
350  typedef std::basic_iostream<extern_type> StreamType;
351 
354 
355  public:
365  , _tbuffer( ios, codec)
366  {
367  this->setBuffer(&_tbuffer);
368  }
369 
377  , _tbuffer(codec)
378  {
379  this->setBuffer(&_tbuffer);
380  }
381 
385  { }
386 
390  {
391  return _tbuffer.codec();
392  }
393 
400  {
401  _tbuffer.setCodec(codec);
402  }
403 
406  void attach(StreamType& ios)
407  {
408  _tbuffer.attach(ios);
409  }
410 
413  void detach()
414  {
415  _tbuffer.detach();
416  }
417 
420  void discard()
421  {
422  _tbuffer.discard();
423  }
424 
430  void reset()
431  {
432  _tbuffer.reset();
433  }
434 
440  void reset(StreamType& ios)
441  {
442  _tbuffer.reset(ios);
443  }
444 
448  { return _tbuffer; }
449 
450  private:
452 };
453 
466 typedef BasicTextIStream<Char, char> TextIStream;
467 
480 typedef BasicTextOStream<Char, char> TextOStream;
481 
494 typedef BasicTextStream<Char, char> TextStream;
495 
496 
498 //
499 // @ingroup Unicode
500 //*/
501 //class PT_API TextIStream : public BasicTextIStream<Char, char>
502 //{
503 // public:
504 // typedef TextCodec<Pt::Char, char> Codec;
505 //
506 // public:
507 // /** @brief Constructor
508 //
509 // The stream will read bytes from \a is and use the codec \a codec
510 // for character conversion. The codec will be destroyed by the
511 // buffer of this stream if the codec was constructed with a
512 // refcount of 0.
513 // */
514 // TextIStream(std::istream& is, Codec* codec);
515 //
516 // explicit TextIStream(Codec* codec);
517 //
518 // ~TextIStream();
519 //};
520 //
521 //
523 //
524 // @ingroup Unicode
525 //*/
526 //class PT_API TextOStream : public BasicTextOStream<Char, char>
527 //{
528 // public:
529 // typedef TextCodec<Pt::Char, char> Codec;
530 //
531 // public:
532 // /** @brief Constructor
533 //
534 // The stream will write bytes to \a os and use the codec \a codec
535 // for character conversion. The codec will be destroyed by the
536 // buffer of this stream if the codec was constructed with a
537 // refcount of 0.
538 // */
539 // TextOStream(std::ostream& os, Codec* codec);
540 //
541 // explicit TextOStream(Codec* codec);
542 //
543 // ~TextOStream();
544 //};
545 //
546 //
548 //
549 // @ingroup Unicode
550 //*/
551 //class PT_API TextStream : public BasicTextStream<Char, char>
552 //{
553 // public:
554 // typedef TextCodec<Pt::Char, char> Codec;
555 //
556 // public:
557 // /** @brief Constructor
558 //
559 // The stream will write or write bytes to \a ios and use the codec
560 // \a codec for character conversion. The codec will be destroyed
561 // by the buffer of this stream if the codec was constructed with a
562 // refcount of 0.
563 // */
564 // TextStream(std::iostream& ios, Codec* codec);
565 //
566 // explicit TextStream(Codec* codec);
567 //
568 // ~TextStream();
569 //};
570 
571 } // namespace Pt
572 
573 #endif // Pt_TextStream_h
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:127
ByteT extern_type
External character type.
Definition: TextStream.h:329
void reset()
Resets the buffer and target.
Definition: TextStream.h:152
void detach()
Detach from external target.
Definition: TextStream.h:135
void setCodec(CodecType *codec)
Sets the text codec.
Definition: TextStream.h:399
~BasicTextStream()
Destructor.
Definition: TextStream.h:384
void discard()
Discards the buffer.
Definition: TextBuffer.h:189
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:175
traits_type::pos_type pos_type
Stream position type.
Definition: TextStream.h:344
Converts character sequences using a codec.
Definition: TextStream.h:325
BasicTextIStream(CodecType *codec)
Construct with codec.
Definition: TextStream.h:97
CharT char_type
Internal character type.
Definition: TextStream.h:196
BasicTextIStream(StreamType &is, CodecType *codec)
Construct with input stream and codec.
Definition: TextStream.h:85
std::char_traits< CharT > traits_type
Internal character traits.
Definition: TextStream.h:199
CharT char_type
Internal character type.
Definition: TextStream.h:335
void discard()
Discards the buffer.
Definition: TextStream.h:420
void discard()
Discards the buffer.
Definition: TextStream.h:142
void reset(StreamType &ios)
Resets the buffer and target.
Definition: TextStream.h:440
traits_type::pos_type pos_type
Stream position type.
Definition: TextStream.h:205
void reset(StreamType &os)
Resets the buffer and target.
Definition: TextStream.h:301
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition: TextStream.h:353
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition: TextStream.h:308
CodecType * codec()
Returns the used codec or a nullptr.
Definition: TextBuffer.h:132
void attach(StreamType &is)
Attach to external target.
Definition: TextStream.h:128
void detach()
Detach from external target.
Definition: TextStream.h:274
Input stream.
Definition: IOStream.h:57
Converts between character encodings.
Definition: TextCodec.h:38
void discard()
Discards the buffer.
Definition: TextStream.h:281
traits_type::int_type int_type
Integer type.
Definition: TextStream.h:341
~BasicTextIStream()
Destructor.
Definition: TextStream.h:106
Input/Output stream.
Definition: IOStream.h:141
void reset(StreamType &is)
Resets the buffer and target.
Definition: TextStream.h:162
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition: TextStream.h:75
BasicTextOStream(StreamType &os, CodecType *codec)
Construct with output stream and codec.
Definition: TextStream.h:224
Converts character sequences using a codec.
Definition: TextStream.h:186
void attach(StreamType &ios)
Attach to external target.
Definition: TextStream.h:406
Unicode character type.
Definition: String.h:66
CharT char_type
Internal character type.
Definition: TextStream.h:57
void reset()
Resets the buffer and target.
Definition: TextStream.h:291
CharT intern_type
Internal character type.
Definition: TextStream.h:193
std::basic_ostream< extern_type > StreamType
External stream type.
Definition: TextStream.h:211
traits_type::off_type off_type
Stream offset type.
Definition: TextStream.h:69
CodecType * codec()
Returns the used code or a nullptr.
Definition: TextStream.h:111
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition: TextStream.h:214
traits_type::off_type off_type
Stream offset type.
Definition: TextStream.h:347
void setCodec(CodecType *codec)
Sets the text codec.
Definition: TextStream.h:260
BasicTextStream(StreamType &ios, CodecType *codec)
Construct by stream and codec.
Definition: TextStream.h:363
std::char_traits< CharT > traits_type
Internal character traits.
Definition: TextStream.h:338
void reset()
Resets the buffer and target.
Definition: TextBuffer.h:170
void reset()
Resets the buffer and target.
Definition: TextStream.h:430
traits_type::int_type int_type
Integer type.
Definition: TextStream.h:63
void detach()
Detach from external target.
Definition: TextStream.h:413
Output stream.
Definition: IOStream.h:101
CodecType * codec()
Returns the used code or a nullptr.
Definition: TextStream.h:250
ByteT extern_type
External character type.
Definition: TextStream.h:51
Converts character sequences using a codec.
Definition: TextStream.h:47
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition: TextStream.h:447
std::basic_istream< extern_type > StreamType
External stream type.
Definition: TextStream.h:72
traits_type::int_type int_type
Integer type.
Definition: TextStream.h:202
void setCodec(CodecType *codec)
Sets the text codec.
Definition: TextStream.h:121
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition: TextStream.h:169
void attach(StreamType &os)
Attach to external target.
Definition: TextStream.h:267
CharT intern_type
Internal character type.
Definition: TextStream.h:332
void setCodec(CodecType *codec)
Sets the text codec.
Definition: TextBuffer.h:140
BasicTextOStream(CodecType *codec)
Construct with codec.
Definition: TextStream.h:236
ByteT extern_type
External character type.
Definition: TextStream.h:190
void detach()
Detach from external target.
Definition: TextBuffer.h:160
std::basic_iostream< extern_type > StreamType
External stream type.
Definition: TextStream.h:350
BasicTextStream(CodecType *codec)
Construct with codec.
Definition: TextStream.h:375
traits_type::pos_type pos_type
Stream position type.
Definition: TextStream.h:66
void setBuffer(BasicStreamBuffer< CharT > *sb)
Sets the buffer.
Definition: IOStream.h:87
CharT intern_type
Internal character type.
Definition: TextStream.h:54
std::char_traits< CharT > traits_type
Internal character traits.
Definition: TextStream.h:60
traits_type::off_type off_type
Stream offset type.
Definition: TextStream.h:208
~BasicTextOStream()
Destructor.
Definition: TextStream.h:245
CodecType * codec()
Returns the used code or a nullptr.
Definition: TextStream.h:389
void attach(std::basic_ios< extern_type > &target)
Attach to external target.
Definition: TextBuffer.h:153