BasicTextIStream< CharT, ByteT > Class Template Reference

#include <Pt/TextStream.h>

Converts character sequences using a codec. More...

Inherits BasicIStream< CharT, TraitsT >.

Public Types

typedef ByteT extern_type
 External character type.
typedef CharT intern_type
 Internal character type.
typedef CharT char_type
 Internal character type.
typedef std::char_traits< CharT > traits_type
 Internal character traits.
typedef traits_type::int_type int_type
 Integer type.
typedef traits_type::pos_type pos_type
 Stream position type.
typedef traits_type::off_type off_type
 Stream offset type.
typedef std::basic_istream< extern_typeStreamType
 External stream type.
typedef TextCodec< char_type, extern_typeCodecType
 Codec type.

Public Member Functions

 BasicTextIStream (StreamType &is, CodecType *codec)
 Construct with input stream and codec.
 BasicTextIStream (CodecType *codec)
 Construct with codec.
 ~BasicTextIStream ()
 Destructor.
CodecTypecodec ()
 Returns the used code or a nullptr.
void setCodec (CodecType *codec)
 Sets the text codec.
void attach (StreamType &is)
 Attach to external target.
void detach ()
 Detach from external target.
void discard ()
 Discards the buffer.
void reset ()
 Resets the buffer and target.
void reset (StreamType &is)
 Resets the buffer and target.
BasicTextBuffer< intern_type, extern_type > & textBuffer ()
 Returns the stream buffer.
std::streamsize peeksome (CharT *buffer, std::streamsize n)
 Peeks bytes in the stream buffer.
BasicStreamBuffer< CharT > * buffer ()
 Returns the buffer.
void setBuffer (BasicStreamBuffer< CharT > *sb)
 Sets the buffer.

Detailed Description

template<typename CharT, typename ByteT>
class Pt::BasicTextIStream< CharT, ByteT >

This stream decodes an external character sequence using a codec. Reading from the stream will convert from the the encoding of external characters.

Text streams do not only convert between text encodings, but also between character types of different size. The first template parameter CharT is the character type of the decoded text and the second one ByteT is the character type of the encoded text. They are also called the internal and external character types and may be of the same type. The internal character type is used as the character type of the standard C++ stream base class.

A text stream always works with another stream as input or output. This stream works with another std::basic_istream to read the encoded input, using the external character type. A text stream can be constructed with an underlying stream and a codec, but both can also be set or reset later. If no codec is set, the stream will directly assign characters, instead of converting them. If no target stream is set, the text stream will always be EOF.

The following example demonstrates how a string stream is used as the input for a text stream, which uses a Pt::Utf8Codec to decode UTF-8 encoded text:

std::istringstream iss("UTF-8 encoded text");
Pt::TextIStream tis(iss, new Pt::Utf8Codec());
std::getline(tis, s);
Unicode capable basic_string.
Definition Api-String.h:67
Convert between unicode and UTF-8.
Definition Utf8Codec.h:44

The std::getline() function reads all input into a Pt::String. The extraction operator can also be used, for example to directly read numbers from the stream.

Constructor & Destructor Documentation

◆ BasicTextIStream() [1/2]

template<typename CharT, typename ByteT>
BasicTextIStream ( StreamType & is,
CodecType * codec )

The input stream is is used to read a character sequence and convert it using the codec codec. The codec object which is passed as pointer will be managed by this class and deleted if its reference count reaches 0.

◆ BasicTextIStream() [2/2]

template<typename CharT, typename ByteT>
BasicTextIStream ( CodecType * codec)
explicit

The codec object which is passed as pointer will be managed by this class and deleted if its reference count reaches 0.

Member Function Documentation

◆ setCodec()

template<typename CharT, typename ByteT>
void setCodec ( CodecType * codec)

The codec object which is passed as pointer will be managed by this class and deleted if its reference count reaches 0.

◆ reset() [1/2]

template<typename CharT, typename ByteT>
void reset ( )

The target is detached and the buffer content is discarded. The codec is kept, if one was set previously.

◆ reset() [2/2]

template<typename CharT, typename ByteT>
void reset ( StreamType & is)

Attaches to the new target and discards the buffer. The codec is kept, if one was set previously.

◆ peeksome()

template<typename CharT, typename TraitsT>
std::streamsize peeksome ( CharT * buffer,
std::streamsize n )
inherited

The number of bytes that can be peeked depends on the current stream buffer get area and maybe less than requested, similar to istream::readsome().