TSDuck v3.46-4806
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::TextFormatter Class Reference

Format and print a text document using various output types and indentation. More...

#include <tsTextFormatter.h>

Inheritance diagram for ts::TextFormatter:
Collaboration diagram for ts::TextFormatter:

Public Types

enum class  EndOfLineMode {
  NATIVE ,
  CR ,
  LF ,
  CRLF ,
  SPACING ,
  NONE
}
 End-of-line mode. More...
 
using SuperClass = std::basic_ostream< char >
 Explicit reference to the public superclass.
 

Public Member Functions

 TextFormatter (Report &report=(ts::NullReport::Instance()))
 Constructor.
 
virtual ~TextFormatter () override
 Destructor.
 
void close ()
 Close the current output.
 
TextFormatter & column (size_t col)
 Insert all necessary new-lines and spaces to move to a given column.
 
size_t currentColumn ()
 Get the current column.
 
size_t currentMargin () const
 Get the current margin, at current level of indentation.
 
TextFormatter & endl ()
 Insert an end-of-line, according to the current end-of-line mode.
 
EndOfLineMode endOfLineMode () const
 Get the end-of-line mode.
 
bool formatting () const
 Check if formatting (margin, indentation) is in effect.
 
bool getString (UString &str)
 Retrieve the current content of the internal string buffer.
 
TextFormatter & indent ()
 Push one indentation level, typically when formatting child items.
 
size_t indentSize () const
 Get the indent size for inner elements.
 
bool isOpen () const
 Check if the Output is open to some output.
 
TextFormatter & margin ()
 Insert all necessary new-lines and spaces to move to the current margin.
 
size_t marginSize () const
 Get the margin size for outer-most elements.
 
Report & report () const
 Get the current report for log and error messages.
 
TextFormatter & setEndOfLineMode (EndOfLineMode mode)
 Set the end-of-line mode.
 
bool setFile (const fs::path &file_name)
 Set output to a text file.
 
TextFormatter & setIndentSize (size_t indent)
 Set the indent size for inner elements.
 
TextFormatter & setMarginSize (size_t margin)
 Set the margin size for outer-most elements.
 
TextFormatter & setStream (std::ostream &strm)
 Set output to an open text stream.
 
TextFormatter & setString ()
 Set output to an internal string buffer.
 
TextFormatter & spaces (size_t count)
 Output spaces on the stream.
 
UString toString ()
 Return the current content of the internal string buffer.
 
TextFormatter & unindent ()
 Pop one indentation level, typically when formatting back to parent.
 

Static Public Attributes

static constexpr size_t DEFAULT_STREAM_BUFFER_SIZE = 1024
 Default stream buffer size in bytes.
 

Protected Member Functions

virtual bool writeStreamBuffer (const void *addr, size_t size) override
 Write buffered data to underlying output devicen whatever it is.
 

Detailed Description

Format and print a text document using various output types and indentation.

This class is used to format XML documents or other types of structured text output. It is a subclass of std::ostream and can be used as any output stream. It also defines additional I/O manipulators to handle indentation.

Member Enumeration Documentation

◆ EndOfLineMode

End-of-line mode.

See also
setEndOfLineMode()
I/O manipulator ts::endl()
Enumerator
NATIVE 

Native end of line (std::endl). This is the default.

CR 

One carriage-return character.

LF 

One line-feed character.

CRLF 

One carriage-return and one line-feed character.

SPACING 

One space character.

NONE 

Nothing as end of line.

Constructor & Destructor Documentation

◆ TextFormatter()

ts::TextFormatter::TextFormatter ( Report &  report = (ts::NullReport::Instance()))
explicit

Constructor.

Parameters
[in,out]reportWhere to report errors.

Member Function Documentation

◆ report()

Report & ts::TextFormatter::report ( ) const
inline

Get the current report for log and error messages.

Returns
A reference to the current output report.

◆ marginSize()

size_t ts::TextFormatter::marginSize ( ) const
inline

Get the margin size for outer-most elements.

Returns
The margin size for outer-most elements.

◆ setMarginSize()

TextFormatter & ts::TextFormatter::setMarginSize ( size_t  margin)

Set the margin size for outer-most elements.

Parameters
[in]marginThe margin size for outer-most elements.
Returns
A reference to this object.
See also
I/O manipulator ts::margin(size_t)

◆ indentSize()

size_t ts::TextFormatter::indentSize ( ) const
inline

Get the indent size for inner elements.

Returns
The indent size for inner elements.

◆ setIndentSize()

TextFormatter & ts::TextFormatter::setIndentSize ( size_t  indent)
inline

Set the indent size for inner elements.

Parameters
[in]indentThe indent size for inner elements.
Returns
A reference to this object.

◆ endOfLineMode()

EndOfLineMode ts::TextFormatter::endOfLineMode ( ) const
inline

Get the end-of-line mode.

Returns
The current end-of-line mode.

◆ setEndOfLineMode()

TextFormatter & ts::TextFormatter::setEndOfLineMode ( EndOfLineMode  mode)

Set the end-of-line mode.

Parameters
[in]modeThe new end-of-line mode.
Returns
A reference to this object.

◆ formatting()

bool ts::TextFormatter::formatting ( ) const
inline

Check if formatting (margin, indentation) is in effect.

When end-of-line mode is SPACING or NONE, formatting is disabled, margin and indentation are ignored.

Returns
True if formatting is in effect.

◆ setStream()

TextFormatter & ts::TextFormatter::setStream ( std::ostream &  strm)

Set output to an open text stream.

Parameters
[in,out]strmThe output text stream. The referenced stream object must remain valid as long as this object.
Returns
A reference to this object.

◆ setFile()

bool ts::TextFormatter::setFile ( const fs::path &  file_name)

Set output to a text file.

Parameters
[in]file_nameOutput file name. If the specified file is empty or "-", use the standard output.
Returns
True on success, false on error.

◆ setString()

TextFormatter & ts::TextFormatter::setString ( )

Set output to an internal string buffer.

Returns
A reference to this object.
See also
getString()

◆ getString()

bool ts::TextFormatter::getString ( UString &  str)

Retrieve the current content of the internal string buffer.

Must be called after setString() and before close().

Parameters
[out]strReturned string containing the formatted document.
Returns
True on success, false if there is no internal string buffer.
See also
setString()

◆ toString()

UString ts::TextFormatter::toString ( )

Return the current content of the internal string buffer.

Must be called after setString() and before close().

Returns
The string containing the formatted document.
See also
getString()

◆ isOpen()

bool ts::TextFormatter::isOpen ( ) const

Check if the Output is open to some output.

Returns
True if the Output is open.

◆ close()

void ts::TextFormatter::close ( )

Close the current output.

Depending on the output mode:

  • The external stream is no longer referenced.
  • The external file is closed.
  • The internal string buffer is emptied.

◆ currentMargin()

size_t ts::TextFormatter::currentMargin ( ) const
inline

Get the current margin, at current level of indentation.

Returns
The current margin.

◆ currentColumn()

size_t ts::TextFormatter::currentColumn ( )

Get the current column.

A flush is forced to update the column.

Returns
The current column.

◆ margin()

TextFormatter & ts::TextFormatter::margin ( )

Insert all necessary new-lines and spaces to move to the current margin.

Returns
A reference to this object.
See also
I/O manipulator ts::margin()

◆ column()

TextFormatter & ts::TextFormatter::column ( size_t  col)

Insert all necessary new-lines and spaces to move to a given column.

Parameters
[in]colThe column position to move to. The first character of a line is at column 0.
Returns
A reference to this object.
See also
I/O manipulator ts::column(size_t)

◆ endl()

TextFormatter & ts::TextFormatter::endl ( )

Insert an end-of-line, according to the current end-of-line mode.

Returns
A reference to this object.
See also
I/O manipulator ts::endl()

◆ spaces()

TextFormatter & ts::TextFormatter::spaces ( size_t  count)

Output spaces on the stream.

Parameters
[in]countNumber of spaces to print.
Returns
A reference to this object.
See also
I/O manipulator ts::spaces(size_t)

◆ indent()

TextFormatter & ts::TextFormatter::indent ( )
inline

Push one indentation level, typically when formatting child items.

Returns
A reference to this object.
See also
I/O manipulator ts::ident()

◆ unindent()

TextFormatter & ts::TextFormatter::unindent ( )
inline

Pop one indentation level, typically when formatting back to parent.

Returns
A reference to this object.
See also
I/O manipulator ts::unident()

◆ writeStreamBuffer()

virtual bool ts::TextFormatter::writeStreamBuffer ( const void *  addr,
size_t  size 
)
overrideprotectedvirtual

Write buffered data to underlying output devicen whatever it is.

Must be implemented by subclasses.

Parameters
[in]addrBuffered data address.
[in]sizeBuffered data size in bytes.
Returns
True on success, false on error.

Implements ts::AbstractStandardOutputStream.


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