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
37namespace Pt {
38
78template <typename CharT, typename ByteT>
79class BasicTextIStream : public BasicIStream<CharT>
80{
81 public:
83 typedef ByteT extern_type;
84
86 typedef CharT intern_type;
87
89 typedef CharT char_type;
90
92 typedef typename std::char_traits<CharT> traits_type;
93
95 typedef typename traits_type::int_type int_type;
96
98 typedef typename traits_type::pos_type pos_type;
99
101 typedef typename traits_type::off_type off_type;
102
104 typedef std::basic_istream<extern_type> StreamType;
105
108
109 public:
119 , _tbuffer( is, codec )
120 {
121 this->setBuffer(&_tbuffer);
122 }
123
131 , _tbuffer(codec )
132 {
133 this->setBuffer(&_tbuffer);
134 }
135
140
144 {
145 return _tbuffer.codec();
146 }
147
154 {
155 _tbuffer.setCodec(codec);
156 }
157
161 {
162 _tbuffer.attach(is);
163 }
164
167 void detach()
168 {
169 _tbuffer.detach();
170 }
171
174 void discard()
175 {
176 _tbuffer.discard();
177 }
178
184 void reset()
185 {
186 _tbuffer.reset();
187 }
188
195 {
196 _tbuffer.reset(is);
197 }
198
203
204 private:
206};
207
208
234template <typename CharT, typename ByteT>
235class BasicTextOStream : public BasicOStream<CharT>
236{
237 public:
239 typedef ByteT extern_type;
240
242 typedef CharT intern_type;
243
245 typedef CharT char_type;
246
248 typedef typename std::char_traits<CharT> traits_type;
249
251 typedef typename traits_type::int_type int_type;
252
254 typedef typename traits_type::pos_type pos_type;
255
257 typedef typename traits_type::off_type off_type;
258
260 typedef std::basic_ostream<extern_type> StreamType;
261
264
265 public:
275 , _tbuffer( os , codec )
276 {
277 this->setBuffer(&_tbuffer);
278 }
279
287 , _tbuffer( codec )
288 {
289 this->setBuffer(&_tbuffer);
290 }
291
296
300 {
301 return _tbuffer.codec();
302 }
303
310 {
311 _tbuffer.setCodec(codec);
312 }
313
317 {
318 _tbuffer.attach(os);
319 }
320
323 void detach()
324 {
325 _tbuffer.detach();
326 }
327
330 void discard()
331 {
332 _tbuffer.discard();
333 }
334
340 void reset()
341 {
342 _tbuffer.reset();
343 }
344
351 {
352 _tbuffer.reset(os);
353 }
354
359
360 private:
362};
363
373template <typename CharT, typename ByteT>
374class BasicTextStream : public BasicIOStream<CharT>
375{
376 public:
378 typedef ByteT extern_type;
379
381 typedef CharT intern_type;
382
384 typedef CharT char_type;
385
387 typedef typename std::char_traits<CharT> traits_type;
388
390 typedef typename traits_type::int_type int_type;
391
393 typedef typename traits_type::pos_type pos_type;
394
396 typedef typename traits_type::off_type off_type;
397
399 typedef std::basic_iostream<extern_type> StreamType;
400
403
404 public:
414 , _tbuffer( ios, codec)
415 {
416 this->setBuffer(&_tbuffer);
417 }
418
426 , _tbuffer(codec)
427 {
428 this->setBuffer(&_tbuffer);
429 }
430
435
439 {
440 return _tbuffer.codec();
441 }
442
449 {
450 _tbuffer.setCodec(codec);
451 }
452
456 {
457 _tbuffer.attach(ios);
458 }
459
462 void detach()
463 {
464 _tbuffer.detach();
465 }
466
469 void discard()
470 {
471 _tbuffer.discard();
472 }
473
479 void reset()
480 {
481 _tbuffer.reset();
482 }
483
489 void reset(StreamType& ios)
490 {
491 _tbuffer.reset(ios);
492 }
493
498
499 private:
501};
502
515typedef BasicTextIStream<Char, char> TextIStream;
516
529typedef BasicTextOStream<Char, char> TextOStream;
530
543typedef BasicTextStream<Char, char> TextStream;
544
545
547//
548// @ingroup Pt-Text
549//*/
550//class PT_API TextIStream : public BasicTextIStream<Char, char>
551//{
552// public:
553// typedef TextCodec<Pt::Char, char> Codec;
554//
555// public:
556// /** @brief Constructor
557//
558// The stream will read bytes from \a is and use the codec \a codec
559// for character conversion. The codec will be destroyed by the
560// buffer of this stream if the codec was constructed with a
561// refcount of 0.
562// */
563// TextIStream(std::istream& is, Codec* codec);
564//
565// explicit TextIStream(Codec* codec);
566//
567// ~TextIStream();
568//};
569//
570//
572//
573// @ingroup Pt-Text
574//*/
575//class PT_API TextOStream : public BasicTextOStream<Char, char>
576//{
577// public:
578// typedef TextCodec<Pt::Char, char> Codec;
579//
580// public:
581// /** @brief Constructor
582//
583// The stream will write bytes to \a os and use the codec \a codec
584// for character conversion. The codec will be destroyed by the
585// buffer of this stream if the codec was constructed with a
586// refcount of 0.
587// */
588// TextOStream(std::ostream& os, Codec* codec);
589//
590// explicit TextOStream(Codec* codec);
591//
592// ~TextOStream();
593//};
594//
595//
597//
598// @ingroup Pt-Text
599//*/
600//class PT_API TextStream : public BasicTextStream<Char, char>
601//{
602// public:
603// typedef TextCodec<Pt::Char, char> Codec;
604//
605// public:
606// /** @brief Constructor
607//
608// The stream will write or write bytes to \a ios and use the codec
609// \a codec for character conversion. The codec will be destroyed
610// by the buffer of this stream if the codec was constructed with a
611// refcount of 0.
612// */
613// TextStream(std::iostream& ios, Codec* codec);
614//
615// explicit TextStream(Codec* codec);
616//
617// ~TextStream();
618//};
619
620} // namespace Pt
621
622#endif // Pt_TextStream_h
BasicIOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:274
void setBuffer(BasicStreamBuffer< Char > *sb)
Definition IOStream.h:213
void setBuffer(BasicStreamBuffer< Char > *sb)
Definition IOStream.h:100
BasicIStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:225
BasicOStream(BasicStreamBuffer< CharT > *sb=0)
Constructor.
Definition IOStream.h:249
void setBuffer(BasicStreamBuffer< Char > *sb)
Definition IOStream.h:152
Converts character sequences with different encodings.
Definition TextBuffer.h:52
traits_type::off_type off_type
Stream offset type.
Definition TextStream.h:101
void setCodec(CodecType *codec)
Sets the text codec.
Definition TextStream.h:153
~BasicTextIStream()
Destructor.
Definition TextStream.h:138
BasicTextIStream(CodecType *codec)
Construct with codec.
Definition TextStream.h:129
std::basic_istream< extern_type > StreamType
External stream type.
Definition TextStream.h:104
void discard()
Discards the buffer.
Definition TextStream.h:174
BasicTextIStream(StreamType &is, CodecType *codec)
Construct with input stream and codec.
Definition TextStream.h:117
CharT intern_type
Internal character type.
Definition TextStream.h:86
traits_type::pos_type pos_type
Stream position type.
Definition TextStream.h:98
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition TextStream.h:107
traits_type::int_type int_type
Integer type.
Definition TextStream.h:95
void attach(StreamType &is)
Attach to external target.
Definition TextStream.h:160
std::char_traits< CharT > traits_type
Internal character traits.
Definition TextStream.h:92
CodecType * codec()
Definition TextStream.h:143
void detach()
Detach from external target.
Definition TextStream.h:167
CharT char_type
Internal character type.
Definition TextStream.h:89
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition TextStream.h:201
void reset()
Resets the buffer and target.
Definition TextStream.h:184
void reset(StreamType &is)
Resets the buffer and target.
Definition TextStream.h:194
ByteT extern_type
External character type.
Definition TextStream.h:83
traits_type::off_type off_type
Stream offset type.
Definition TextStream.h:257
std::basic_ostream< extern_type > StreamType
External stream type.
Definition TextStream.h:260
void setCodec(CodecType *codec)
Sets the text codec.
Definition TextStream.h:309
~BasicTextOStream()
Destructor.
Definition TextStream.h:294
void discard()
Discards the buffer.
Definition TextStream.h:330
CharT intern_type
Internal character type.
Definition TextStream.h:242
traits_type::pos_type pos_type
Stream position type.
Definition TextStream.h:254
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition TextStream.h:263
traits_type::int_type int_type
Integer type.
Definition TextStream.h:251
void reset(StreamType &os)
Resets the buffer and target.
Definition TextStream.h:350
std::char_traits< CharT > traits_type
Internal character traits.
Definition TextStream.h:248
void attach(StreamType &os)
Attach to external target.
Definition TextStream.h:316
CodecType * codec()
Definition TextStream.h:299
void detach()
Detach from external target.
Definition TextStream.h:323
CharT char_type
Internal character type.
Definition TextStream.h:245
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition TextStream.h:357
void reset()
Resets the buffer and target.
Definition TextStream.h:340
BasicTextOStream(CodecType *codec)
Construct with codec.
Definition TextStream.h:285
BasicTextOStream(StreamType &os, CodecType *codec)
Construct with output stream and codec.
Definition TextStream.h:273
ByteT extern_type
External character type.
Definition TextStream.h:239
traits_type::off_type off_type
Stream offset type.
Definition TextStream.h:396
void setCodec(CodecType *codec)
Sets the text codec.
Definition TextStream.h:448
void discard()
Discards the buffer.
Definition TextStream.h:469
CharT intern_type
Internal character type.
Definition TextStream.h:381
void attach(StreamType &ios)
Attach to external target.
Definition TextStream.h:455
traits_type::pos_type pos_type
Stream position type.
Definition TextStream.h:393
TextCodec< char_type, extern_type > CodecType
Codec type.
Definition TextStream.h:402
traits_type::int_type int_type
Integer type.
Definition TextStream.h:390
BasicTextStream(StreamType &ios, CodecType *codec)
Construct by stream and codec.
Definition TextStream.h:412
void reset(StreamType &ios)
Resets the buffer and target.
Definition TextStream.h:489
std::char_traits< CharT > traits_type
Internal character traits.
Definition TextStream.h:387
BasicTextStream(CodecType *codec)
Construct with codec.
Definition TextStream.h:424
CodecType * codec()
Definition TextStream.h:438
void detach()
Detach from external target.
Definition TextStream.h:462
CharT char_type
Internal character type.
Definition TextStream.h:384
BasicTextBuffer< intern_type, extern_type > & textBuffer()
Returns the stream buffer.
Definition TextStream.h:496
void reset()
Resets the buffer and target.
Definition TextStream.h:479
~BasicTextStream()
Destructor.
Definition TextStream.h:433
ByteT extern_type
External character type.
Definition TextStream.h:378
std::basic_iostream< extern_type > StreamType
External stream type.
Definition TextStream.h:399
Converts between character encodings.
Definition Api-TextCodec.h:44
Text input stream for unicode character conversion.
Text output stream for unicode character conversion.
Text stream for unicode character conversion.
Core module.
Definition Allocator.h:33