libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
TextStream Class Reference

Formatted text I/O with encoding awareness. More...

#include <textstream.h>

Public Types

enum  Status { Ok , ReadPastEnd , WriteFailed }
 Stream status codes. More...
 
enum  FieldAlignment { Left , Right , Center }
 Field alignment for padded output. More...
 
enum  RealNumberNotation { SmartNotation , Fixed , Scientific }
 Floating-point notation styles. More...
 

Public Member Functions

 TextStream (IODevice *device)
 Constructs a TextStream on an IODevice.
 
 TextStream (Buffer *buffer)
 Constructs a TextStream backed by a Buffer.
 
 TextStream (String *string)
 Constructs a TextStream backed by a String.
 
 TextStream (FILE *file)
 Constructs a TextStream wrapping a C stdio FILE.
 
 ~TextStream ()
 Destructor.
 
Status status () const
 Returns the current stream status.
 
void resetStatus ()
 Resets the stream status to Ok.
 
bool atEnd () const
 Returns true if at the end of the stream.
 
void flush ()
 Flushes any buffered output to the underlying device.
 
IODevicedevice () const
 Returns the underlying IODevice.
 
void setEncoding (const String &encoding)
 Sets the text encoding for read/write operations.
 
String encoding () const
 Returns the current encoding name.
 
void setFieldWidth (int width)
 Sets the minimum field width for formatted output.
 
int fieldWidth () const
 Returns the current field width.
 
void setFieldAlignment (FieldAlignment align)
 Sets the field alignment.
 
FieldAlignment fieldAlignment () const
 Returns the current field alignment.
 
void setPadChar (char c)
 Sets the padding character used for field width.
 
char padChar () const
 Returns the current pad character.
 
void setIntegerBase (int base)
 Sets the integer base for formatted output.
 
int integerBase () const
 Returns the current integer base.
 
void setRealNumberPrecision (int precision)
 Sets the number of decimal places for float/double output.
 
int realNumberPrecision () const
 Returns the current real number precision.
 
void setRealNumberNotation (RealNumberNotation notation)
 Sets the floating-point notation style.
 
RealNumberNotation realNumberNotation () const
 Returns the current real number notation.
 
TextStreamoperator<< (const String &val)
 Writes a String.
 
TextStreamoperator<< (const char *val)
 Writes a C string.
 
TextStreamoperator<< (char val)
 Writes a single character.
 
TextStreamoperator<< (int val)
 Writes an int as formatted decimal text.
 
TextStreamoperator<< (unsigned int val)
 Writes an unsigned int as formatted decimal text.
 
TextStreamoperator<< (int64_t val)
 Writes an int64_t as formatted text.
 
TextStreamoperator<< (uint64_t val)
 Writes a uint64_t as formatted text.
 
TextStreamoperator<< (float val)
 Writes a float as formatted text.
 
TextStreamoperator<< (double val)
 Writes a double as formatted text.
 
TextStreamoperator<< (bool val)
 Writes "true" or "false".
 
TextStreamoperator<< (const Variant &val)
 Writes a Variant using its toString() representation.
 
TextStreamoperator<< (TextStream &(*manip)(TextStream &))
 Writes a manipulator function.
 
TextStreamoperator>> (String &val)
 Reads a whitespace-delimited token into a String.
 
TextStreamoperator>> (char &val)
 Reads a single character.
 
TextStreamoperator>> (int &val)
 Reads an int from text.
 
TextStreamoperator>> (int64_t &val)
 Reads an int64_t from text.
 
TextStreamoperator>> (double &val)
 Reads a double from text.
 
String readLine ()
 Reads one line of text, consuming the trailing newline.
 
String readAll ()
 Reads all remaining text.
 
String read (size_t maxLength)
 Reads up to maxLength characters.
 

Detailed Description

Formatted text I/O with encoding awareness.

TextStream provides a Qt-style interface for reading and writing formatted text. It is used for human-readable output, config files, log formatting, and structured text parsing.

TextStream does not inherit from std::ostream. All operator<< and operator>> overloads are provided explicitly. This separation allows encoding-aware text handling and promeki-native formatting controls without the complexity of std::locale.

TextStream can be constructed from:

All constructors funnel through a single IODevice code path internally.

Encoding
The default encoding is UTF-8. When a different encoding is set, text is converted on read/write. Currently supported: "UTF-8" and "Latin-1".
Formatting
Integer base, field width, alignment, pad character, and floating-point precision/notation are all configurable. Manipulator functions (endl, flush, hex, dec, etc.) provide a convenient chaining syntax.

Member Enumeration Documentation

◆ FieldAlignment

Field alignment for padded output.

Enumerator
Left 

Left-aligned (pad on right).

Right 

Right-aligned (pad on left, default).

Center 

Center-aligned (pad both sides).

◆ RealNumberNotation

Floating-point notation styles.

Enumerator
SmartNotation 

Use fixed or scientific as appropriate (default).

Fixed 

Always use fixed-point notation.

Scientific 

Always use scientific notation.

◆ Status

Stream status codes.

Enumerator
Ok 

No error.

ReadPastEnd 

Attempted to read beyond available data.

WriteFailed 

A write operation failed.

Constructor & Destructor Documentation

◆ TextStream() [1/4]

TextStream::TextStream ( IODevice device)
explicit

Constructs a TextStream on an IODevice.

The device must already be open. The TextStream does not take ownership of the device.

Parameters
deviceThe IODevice to operate on.

◆ TextStream() [2/4]

TextStream::TextStream ( Buffer buffer)
explicit

Constructs a TextStream backed by a Buffer.

An internal BufferIODevice is created and opened for ReadWrite. The Buffer must outlive the TextStream.

Parameters
bufferThe Buffer to read from / write to.

◆ TextStream() [3/4]

TextStream::TextStream ( String string)
explicit

Constructs a TextStream backed by a String.

An internal StringIODevice is created and opened for ReadWrite. The String must outlive the TextStream.

Parameters
stringThe String to read from / write to.

◆ TextStream() [4/4]

TextStream::TextStream ( FILE file)
explicit

Constructs a TextStream wrapping a C stdio FILE.

An internal FileIODevice is created with ReadWrite mode. The FILE must outlive the TextStream. The TextStream does not take ownership of the FILE (it will not be fclose'd).

Parameters
fileThe FILE pointer to wrap.

Member Function Documentation

◆ atEnd()

bool TextStream::atEnd ( ) const

Returns true if at the end of the stream.

Returns
True if no more data can be read.

◆ device()

IODevice * TextStream::device ( ) const
inline

Returns the underlying IODevice.

Returns
The device pointer.

◆ encoding()

String TextStream::encoding ( ) const
inline

Returns the current encoding name.

Returns
The encoding string.

◆ fieldAlignment()

FieldAlignment TextStream::fieldAlignment ( ) const
inline

Returns the current field alignment.

Returns
The alignment mode.

◆ fieldWidth()

int TextStream::fieldWidth ( ) const
inline

Returns the current field width.

Returns
The field width.

◆ flush()

void TextStream::flush ( )

Flushes any buffered output to the underlying device.

Calls IODevice::flush() on the underlying device. This allows devices that intercept writes (such as StreamStringIODevice) to act on incomplete data.

◆ integerBase()

int TextStream::integerBase ( ) const
inline

Returns the current integer base.

Returns
The integer base.

◆ padChar()

char TextStream::padChar ( ) const
inline

Returns the current pad character.

Returns
The pad character.

◆ read()

String TextStream::read ( size_t  maxLength)

Reads up to maxLength characters.

Parameters
maxLengthMaximum number of characters to read.
Returns
The text read.

◆ readAll()

String TextStream::readAll ( )

Reads all remaining text.

Returns
The remaining text content.

◆ readLine()

String TextStream::readLine ( )

Reads one line of text, consuming the trailing newline.

The returned string does not include the trailing newline.

Returns
The line, or an empty string at end-of-stream.

◆ realNumberNotation()

RealNumberNotation TextStream::realNumberNotation ( ) const
inline

Returns the current real number notation.

Returns
The notation style.

◆ realNumberPrecision()

int TextStream::realNumberPrecision ( ) const
inline

Returns the current real number precision.

Returns
The precision.

◆ setEncoding()

void TextStream::setEncoding ( const String encoding)

Sets the text encoding for read/write operations.

Supported values: "UTF-8" (default), "Latin-1".

Parameters
encodingThe encoding name.

◆ setFieldAlignment()

void TextStream::setFieldAlignment ( FieldAlignment  align)
inline

Sets the field alignment.

Parameters
alignThe alignment mode.

◆ setFieldWidth()

void TextStream::setFieldWidth ( int  width)
inline

Sets the minimum field width for formatted output.

When a value's text representation is shorter than the field width, it is padded according to the current alignment and pad character.

Parameters
widthThe minimum field width.

◆ setIntegerBase()

void TextStream::setIntegerBase ( int  base)
inline

Sets the integer base for formatted output.

Supported bases: 2, 8, 10 (default), 16.

Parameters
baseThe integer base.

◆ setPadChar()

void TextStream::setPadChar ( char  c)
inline

Sets the padding character used for field width.

Parameters
cThe pad character (default: space).

◆ setRealNumberNotation()

void TextStream::setRealNumberNotation ( RealNumberNotation  notation)
inline

Sets the floating-point notation style.

Parameters
notationThe notation style.

◆ setRealNumberPrecision()

void TextStream::setRealNumberPrecision ( int  precision)
inline

Sets the number of decimal places for float/double output.

Parameters
precisionThe precision (default: 6).

◆ status()

Status TextStream::status ( ) const
inline

Returns the current stream status.

Returns
The status code.

The documentation for this class was generated from the following file: