Detailed Description

A TokenWriter writes sequential tokens to a file, a string or stdout.

It is the counterpart of the TokenReader. Since wrtiting data is comparatively straight foreward, there is no explicit support of sections etc. It is left to the calling function to organise the output appropriately.

There are two twists required to get alogn with faudes-tokens within an XML context

  • The faudes comment mark % is convenient, but not XML compliant. The TokenWriter therefore must escape special XML characters when writing a faudes-comment.
  • Faudes style token streams are meant to formated to some extend, e.g., insertion of linefeeds after reaching a certain number of columns or befor the beginning of an element. While this is convenient for faudes-type serialisation, it is annoying for the processing of HTML style documentation.

Definition at line 51 of file cfl_tokenwriter.h.

#include <cfl_tokenwriter.h>

Public Types

enum  Mode {
  File , XmlFile , Stdout , String ,
  Stream , XmlStream
}
 Mode of operation: write to file, string or stdout. More...
 

Public Member Functions

 TokenWriter (Mode mode)
 Console or String TokenWriter constructor. More...
 
 TokenWriter (const std::string &rFilename, std::ios::openmode openmode=std::ios::out|std::ios::trunc)
 File TokenWriter constructor. More...
 
 TokenWriter (const std::string &rFilename, const std::string &doctype)
 Xml File TokenWriter constructor. More...
 
 TokenWriter (std::ostream &rStream, const std::string &doctype="")
 Stream TokenWriter constructor. More...
 
 ~TokenWriter (void)
 Destructor. More...
 
std::string FileName (void) const
 Get the filename. More...
 
void Flush (void)
 Flush any buffers. More...
 
bool FileMode (void) const
 Test for file mode (incl. More...
 
bool XmlMode (void) const
 Test for xml file mode. More...
 
bool StdoutMode (void) const
 Test for console mode. More...
 
std::string Str (void)
 Retrieve output as string (if in String mode) More...
 
std::ostream * Streamp (void)
 Access C++ stream. More...
 
int Columns (void) const
 Get number of columns in a line. More...
 
void Columns (int columns)
 Set number of columns in a line. More...
 
void Endl (void)
 Write endl separator. More...
 
void Endl (bool on)
 Turn endl separator on/off. More...
 
void Write (Token &rToken)
 Write next token. More...
 
void WriteString (const std::string &rString)
 Write string. More...
 
void WriteText (const std::string &rText)
 Write text. More...
 
void WriteText (Token &rBeginTag, const std::string &rText)
 Write text section. More...
 
void WriteText (const std::string &rLabel, const std::string &rText)
 Write text section. More...
 
void WriteVerbatim (Token &rBeginTag, const std::string &rText)
 Write verbatim text section. More...
 
void WriteVerbatim (const std::string &rLabel, const std::string &rText)
 Write verbatim text section. More...
 
void WriteCharacterData (const std::string &rCharData)
 Write character data. More...
 
void WriteInteger (Idx index)
 Write non negative integer. More...
 
void WriteFloat (const double &val)
 Write float. More...
 
void WriteInteger16 (long int val)
 Write integer as hex. More...
 
void WriteOption (const std::string &rOpt)
 Write option (may not contain any "+") More...
 
void WriteBegin (const std::string &rLabel)
 Write begin label. More...
 
void WriteEnd (const std::string &rLabel)
 Write end label. More...
 
void WriteEmpty (const std::string &rLabel)
 Write empty section label. More...
 
void WriteComment (const std::string &rComment)
 Write comment in faudes format. More...
 
void WriteXmlComment (const std::string &rComment)
 Write comment in Xml format. More...
 
void WriteBinary (const char *pData, long int len)
 Write comment. More...
 
TokenWriteroperator<< (Token &rToken)
 Operator for writing tokens. More...
 
TokenWriteroperator<< (const std::string &rString)
 Operator for writing std::strings to a stream. More...
 
TokenWriteroperator<< (const Idx index)
 Operator for writing Idxs to a stream. More...
 

Private Member Functions

void DoFlush (bool clf=1)
 Flush internal buffer. More...
 

Private Attributes

Mode mMode
 Output mode. More...
 
std::ostream * mpStream
 ostream object pointer More...
 
std::ofstream mFStream
 Actual stream object, file output. More...
 
std::ostringstream mSStream
 Actual stream object, string output. More...
 
std::ostream * pSStream
 Actual stream object, stream output. More...
 
Token mOutBuffer
 Outputbuffer. More...
 
bool mHasOutBuffer
 
std::string mFileName
 Filename. More...
 
int mColumns
 Number of columns. More...
 
int mColCount
 Column counter. More...
 
bool mEndl
 Endl seperator on/off. More...
 
std::string mDocType
 Xml doctype if in xml mode. More...
 

Member Enumeration Documentation

◆ Mode

Mode of operation: write to file, string or stdout.

Enumerator
File 
XmlFile 
Stdout 
String 
Stream 
XmlStream 

Definition at line 58 of file cfl_tokenwriter.h.

Constructor & Destructor Documentation

◆ TokenWriter() [1/4]

faudes::TokenWriter::TokenWriter ( Mode  mode)

Console or String TokenWriter constructor.

Technical detail: Stdout mode uses the global console object ConsoleOut::G() declared in cfl_helper.h.

Exceptions
Exception

Definition at line 73 of file cfl_tokenwriter.cpp.

◆ TokenWriter() [2/4]

faudes::TokenWriter::TokenWriter ( const std::string &  rFilename,
std::ios::openmode  openmode = std::ios::out|std::ios::trunc 
)

File TokenWriter constructor.

Parameters
rFilenameFile to write
openmodestd::ios::openmode
Exceptions
Exception

Definition at line 29 of file cfl_tokenwriter.cpp.

◆ TokenWriter() [3/4]

faudes::TokenWriter::TokenWriter ( const std::string &  rFilename,
const std::string &  doctype 
)

Xml File TokenWriter constructor.

Parameters
rFilenameFile to write
doctypeString to indicate XML doctype.
Exceptions
Exception

Definition at line 47 of file cfl_tokenwriter.cpp.

◆ TokenWriter() [4/4]

faudes::TokenWriter::TokenWriter ( std::ostream &  rStream,
const std::string &  doctype = "" 
)

Stream TokenWriter constructor.

Parameters
rStreamstream C++ stream to write to
doctypeString to indicate XML doctype.
Exceptions
Exception

Definition at line 118 of file cfl_tokenwriter.cpp.

◆ ~TokenWriter()

faudes::TokenWriter::~TokenWriter ( void  )

Destructor.

Calls close

Definition at line 141 of file cfl_tokenwriter.cpp.

Member Function Documentation

◆ Columns() [1/2]

void faudes::TokenWriter::Columns ( int  columns)

Set number of columns in a line.

Parameters
columns

of columns in a line

Definition at line 184 of file cfl_tokenwriter.cpp.

◆ Columns() [2/2]

int faudes::TokenWriter::Columns ( void  ) const

Get number of columns in a line.

Returns

of columns in a line

Definition at line 179 of file cfl_tokenwriter.cpp.

◆ DoFlush()

void faudes::TokenWriter::DoFlush ( bool  clf = 1)
private

Flush internal buffer.

Definition at line 209 of file cfl_tokenwriter.cpp.

◆ Endl() [1/2]

void faudes::TokenWriter::Endl ( bool  on)

Turn endl separator on/off.

Definition at line 203 of file cfl_tokenwriter.cpp.

◆ Endl() [2/2]

void faudes::TokenWriter::Endl ( void  )

Write endl separator.

Exceptions
Exception

Definition at line 189 of file cfl_tokenwriter.cpp.

◆ FileMode()

bool faudes::TokenWriter::FileMode ( void  ) const
inline

Test for file mode (incl.

XmlFile)

Returns
Mode

Definition at line 132 of file cfl_tokenwriter.h.

◆ FileName()

std::string faudes::TokenWriter::FileName ( void  ) const

Get the filename.

Return dummy values for console or string mode.

Returns
Filename

Definition at line 539 of file cfl_tokenwriter.cpp.

◆ Flush()

void faudes::TokenWriter::Flush ( void  )

Flush any buffers.

Definition at line 147 of file cfl_tokenwriter.cpp.

◆ operator<<() [1/3]

TokenWriter& faudes::TokenWriter::operator<< ( const Idx  index)
inline

Operator for writing Idxs to a stream.

Parameters
indexIndex to write
Returns
Reference to this TokenWriter
Exceptions
Exception

Definition at line 476 of file cfl_tokenwriter.h.

◆ operator<<() [2/3]

TokenWriter& faudes::TokenWriter::operator<< ( const std::string &  rString)
inline

Operator for writing std::strings to a stream.

Parameters
rStringString to write
Returns
Reference to this TokenWriter
Exceptions
Exception

Definition at line 461 of file cfl_tokenwriter.h.

◆ operator<<() [3/3]

TokenWriter& faudes::TokenWriter::operator<< ( Token rToken)
inline

Operator for writing tokens.

Parameters
rTokenToken to write
Returns
Reference to this TokenWriter
Exceptions
Exception

Definition at line 446 of file cfl_tokenwriter.h.

◆ StdoutMode()

bool faudes::TokenWriter::StdoutMode ( void  ) const
inline

Test for console mode.

Returns
Mode

Definition at line 148 of file cfl_tokenwriter.h.

◆ Str()

std::string faudes::TokenWriter::Str ( void  )

Retrieve output as string (if in String mode)

Exceptions
Exception

Definition at line 160 of file cfl_tokenwriter.cpp.

◆ Streamp()

std::ostream * faudes::TokenWriter::Streamp ( void  )

Access C++ stream.

Definition at line 171 of file cfl_tokenwriter.cpp.

◆ Write()

void faudes::TokenWriter::Write ( Token rToken)

Write next token.

Parameters
rTokenToken to write
Exceptions
Exception

Definition at line 246 of file cfl_tokenwriter.cpp.

◆ WriteBegin()

void faudes::TokenWriter::WriteBegin ( const std::string &  rLabel)

Write begin label.

Parameters
rLabelEnd label, e.g. "Alphabet"
Exceptions
Exception

Definition at line 439 of file cfl_tokenwriter.cpp.

◆ WriteBinary()

void faudes::TokenWriter::WriteBinary ( const char *  pData,
long int  len 
)

Write comment.

Parameters
lenNumber of bytes to write
pDataData to write
Exceptions
Exception

Definition at line 533 of file cfl_tokenwriter.cpp.

◆ WriteCharacterData()

void faudes::TokenWriter::WriteCharacterData ( const std::string &  rCharData)

Write character data.

Writes a preproccessed string to the token stream. The string may or may not contain any markup. Formating like linefeed is maintained. This method is meant to support 1:1 copying from one stream to another. See also ReadCharacterData() from TokenReader.

Parameters
rCharDataString to write
Exceptions
Exception

Definition at line 388 of file cfl_tokenwriter.cpp.

◆ WriteComment()

void faudes::TokenWriter::WriteComment ( const std::string &  rComment)

Write comment in faudes format.

Parameters
rCommentComment to write
Exceptions
Exception

Definition at line 460 of file cfl_tokenwriter.cpp.

◆ WriteEmpty()

void faudes::TokenWriter::WriteEmpty ( const std::string &  rLabel)

Write empty section label.

Parameters
rLabelEnd label, e.g. "Alphabet"
Exceptions
Exception

Definition at line 453 of file cfl_tokenwriter.cpp.

◆ WriteEnd()

void faudes::TokenWriter::WriteEnd ( const std::string &  rLabel)

Write end label.

Parameters
rLabelEnd label, e.g. "Alphabet"
Exceptions
Exception

Definition at line 446 of file cfl_tokenwriter.cpp.

◆ WriteFloat()

void faudes::TokenWriter::WriteFloat ( const double &  val)

Write float.

Parameters
valfloat to write
Exceptions
Exception

Definition at line 423 of file cfl_tokenwriter.cpp.

◆ WriteInteger()

void faudes::TokenWriter::WriteInteger ( Idx  index)

Write non negative integer.

Parameters
indexInteger to write
Exceptions
Exception

Definition at line 409 of file cfl_tokenwriter.cpp.

◆ WriteInteger16()

void faudes::TokenWriter::WriteInteger16 ( long int  val)

Write integer as hex.

Parameters
valInteger to write
Exceptions
Exception

Definition at line 416 of file cfl_tokenwriter.cpp.

◆ WriteOption()

void faudes::TokenWriter::WriteOption ( const std::string &  rOpt)

Write option (may not contain any "+")

Parameters
rOptoption to write
Exceptions
Exception

Definition at line 431 of file cfl_tokenwriter.cpp.

◆ WriteString()

void faudes::TokenWriter::WriteString ( const std::string &  rString)

Write string.

Writes a faudes string token, i.e. enclosed in double quotes if required and any special markup characters substitited by entity references.

The string must not include any formating controls like linefeed.

Parameters
rStringString to write
Exceptions
Exception

Definition at line 300 of file cfl_tokenwriter.cpp.

◆ WriteText() [1/3]

void faudes::TokenWriter::WriteText ( const std::string &  rLabel,
const std::string &  rText 
)

Write text section.

Convenience wrapper for WriteText(Token&, const std::string&).

Parameters
rLabelSection label
rTextString to write
Exceptions
Exception

Definition at line 348 of file cfl_tokenwriter.cpp.

◆ WriteText() [2/3]

void faudes::TokenWriter::WriteText ( const std::string &  rText)

Write text.

Converts the specified string to character data by substituting markup signal characters with the resp. entities and writes the result to the token stream.

In contrast to WriteString(const std::string&), no quotes are applied and the output will in general be interpretable by multiple faudes string tokens.

This function is depreciated as of libFAUDES 2.16. For a consistent readback, it is required to wrap the output between a begin-end-elemant pair. This is organized conveniently organised by WriteText(Token&, const std::string&).

Parameters
rTextString to write
Exceptions
Exception

Definition at line 313 of file cfl_tokenwriter.cpp.

◆ WriteText() [3/3]

void faudes::TokenWriter::WriteText ( Token rBeginTag,
const std::string &  rText 
)

Write text section.

Converts the specified string to character data by substituting markup signal characters with the resp. entities and writes the result to a section in the token stream. Formating by linefeeds etc is maintained.

There is a matching ReadText() function for the Tokenreader to retrieve the original text.

See also WriteText(const std::string&, const std::string&).

Parameters
rBeginTagBegin tag.
rTextString to write
Exceptions
Exception

Definition at line 326 of file cfl_tokenwriter.cpp.

◆ WriteVerbatim() [1/2]

void faudes::TokenWriter::WriteVerbatim ( const std::string &  rLabel,
const std::string &  rText 
)

Write verbatim text section.

Convenience wrapper for WriteVerbatim(Token&, const std::string&).

Parameters
rLabelSection label
rTextString to write
Exceptions
Exception

Definition at line 380 of file cfl_tokenwriter.cpp.

◆ WriteVerbatim() [2/2]

void faudes::TokenWriter::WriteVerbatim ( Token rBeginTag,
const std::string &  rText 
)

Write verbatim text section.

Writes a string verbatim as a sequence of CDATA markup in a deticated section. The string may contain any characters.

In the libFAUDES context, the usecase are long fragments of text such as Lua code. For cosmetic reasons, the specified string in wrapped in linefeeds. Use the corresponding ReadVerbatim() for a consistent readback.

See also WriteVerbatim(const std::string&, const std::string&).

Parameters
rBeginTagprepared begin token
rTextString to write
Exceptions
Exception

Definition at line 356 of file cfl_tokenwriter.cpp.

◆ WriteXmlComment()

void faudes::TokenWriter::WriteXmlComment ( const std::string &  rComment)

Write comment in Xml format.

Parameters
rCommentComment to write
Exceptions
Exception

Definition at line 501 of file cfl_tokenwriter.cpp.

◆ XmlMode()

bool faudes::TokenWriter::XmlMode ( void  ) const
inline

Test for xml file mode.

Returns
Mode

Definition at line 140 of file cfl_tokenwriter.h.

Member Data Documentation

◆ mColCount

int faudes::TokenWriter::mColCount
private

Column counter.

Definition at line 509 of file cfl_tokenwriter.h.

◆ mColumns

int faudes::TokenWriter::mColumns
private

Number of columns.

Definition at line 506 of file cfl_tokenwriter.h.

◆ mDocType

std::string faudes::TokenWriter::mDocType
private

Xml doctype if in xml mode.

Definition at line 515 of file cfl_tokenwriter.h.

◆ mEndl

bool faudes::TokenWriter::mEndl
private

Endl seperator on/off.

Definition at line 512 of file cfl_tokenwriter.h.

◆ mFileName

std::string faudes::TokenWriter::mFileName
private

Filename.

Definition at line 503 of file cfl_tokenwriter.h.

◆ mFStream

std::ofstream faudes::TokenWriter::mFStream
private

Actual stream object, file output.

Definition at line 490 of file cfl_tokenwriter.h.

◆ mHasOutBuffer

bool faudes::TokenWriter::mHasOutBuffer
private

Definition at line 500 of file cfl_tokenwriter.h.

◆ mMode

Mode faudes::TokenWriter::mMode
private

Output mode.

Definition at line 484 of file cfl_tokenwriter.h.

◆ mOutBuffer

Token faudes::TokenWriter::mOutBuffer
private

Outputbuffer.

Definition at line 499 of file cfl_tokenwriter.h.

◆ mpStream

std::ostream* faudes::TokenWriter::mpStream
private

ostream object pointer

Definition at line 487 of file cfl_tokenwriter.h.

◆ mSStream

std::ostringstream faudes::TokenWriter::mSStream
private

Actual stream object, string output.

Definition at line 493 of file cfl_tokenwriter.h.

◆ pSStream

std::ostream* faudes::TokenWriter::pSStream
private

Actual stream object, stream output.

Definition at line 496 of file cfl_tokenwriter.h.


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

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen