Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
CSVFormatter Class Reference

Output formatter for CSV output. More...

#include <CSVFormatter.h>

Inheritance diagram for CSVFormatter:
[legend]
Collaboration diagram for CSVFormatter:
[legend]

Public Member Functions

bool closeTag (std::ostream &into, const std::string &comment="")
 Closes the most recently opened tag.
 
 CSVFormatter (const std::string &columnNames, const char separator=';')
 Constructor.
 
OutputFormatterType getType ()
 Returns the type of formatter being used.
 
void openTag (std::ostream &into, const std::string &xmlElement)
 Keeps track of an open XML tag by adding a new element to the stack.
 
void openTag (std::ostream &into, const SumoXMLTag &xmlElement)
 Keeps track of an open XML tag by adding a new element to the stack.
 
void setExpectedAttributes (const SumoXMLAttrMask &expected, const int depth=2)
 Set the expected attributes to write. This is used for tracking which attributes are expected in table like outputs. This should be not necessary but at least in the initial phase of implementing CSV and Parquet it helps a lot to track errors.
 
template<>
void writeAttr (std::ostream &, const SumoXMLAttr attr, const std::string &val)
 
template<class T >
void writeAttr (std::ostream &into, const std::string &attr, const T &val)
 
template<>
void writeAttr (std::ostream &into, const SumoXMLAttr attr, const double &val)
 
template<class T >
void writeAttr (std::ostream &into, const SumoXMLAttr attr, const T &val)
 writes a named attribute
 
void writeNull (std::ostream &, const SumoXMLAttr attr)
 
virtual void writePadding (std::ostream &into, const std::string &val)
 Writes some whitespace to format the output. This method is only implemented for XML output.
 
virtual void writePreformattedTag (std::ostream &into, const std::string &val)
 Writes a preformatted tag to the device but ensures that any pending tags are closed. This method is only implemented for XML output.
 
void writeTime (std::ostream &, const SumoXMLAttr attr, const SUMOTime val)
 
virtual bool writeXMLHeader (std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool includeConfig=true)
 Writes an XML header with optional configuration.
 
bool wroteHeader () const
 Returns whether a header has been written. Useful to detect whether a file is being used by multiple sources.
 
virtual ~CSVFormatter ()
 Destructor.
 

Private Member Functions

void checkAttr (const SumoXMLAttr attr)
 Helper function to keep track of the written attributes and accumulate the header. It checks whether the written attribute is expected in the column based format. The check does only apply to the deepest level of the XML hierarchy and not to the order of the columns just to the presence.
 

Private Attributes

bool myCheckColumns = false
 whether the columns should be checked for completeness
 
int myCurrentDepth = 0
 the current depth of the XML hierarchy (excluding the root element)
 
std::string myCurrentTag
 the currently read tag (only valid when generating the header)
 
SumoXMLAttrMask myExpectedAttrs
 which CSV columns are expected (just for checking completeness)
 
std::vector< std::string > myHeader
 the CSV header
 
const std::string myHeaderFormat
 the format to use for the column names
 
int myMaxDepth = 0
 the maximum depth of the XML hierarchy (excluding the root element)
 
SumoXMLAttrMask mySeenAttrs
 which CSV columns have been set (just for checking completeness)
 
const char mySeparator
 The value separator.
 
const OutputFormatterType myType
 the type of formatter being used (XML, CSV, Parquet, etc.)
 
bool myWroteHeader = false
 whether the CSV header line has been written
 
std::vector< std::unique_ptr< std::ostringstream > > myXMLStack
 The attributes to write for each begun xml element (excluding the root element)
 

Detailed Description

Output formatter for CSV output.

Definition at line 38 of file CSVFormatter.h.

Constructor & Destructor Documentation

◆ CSVFormatter()

CSVFormatter::CSVFormatter ( const std::string &  columnNames,
const char  separator = ';' 
)

Constructor.

Definition at line 30 of file CSVFormatter.cpp.

References myHeaderFormat, and myWroteHeader.

◆ ~CSVFormatter()

virtual CSVFormatter::~CSVFormatter ( )
inlinevirtual

Destructor.

Definition at line 44 of file CSVFormatter.h.

Member Function Documentation

◆ checkAttr()

void CSVFormatter::checkAttr ( const SumoXMLAttr  attr)
inlineprivate

Helper function to keep track of the written attributes and accumulate the header. It checks whether the written attribute is expected in the column based format. The check does only apply to the deepest level of the XML hierarchy and not to the order of the columns just to the presence.

Parameters
[in]attrThe attribute (name)

Definition at line 121 of file CSVFormatter.h.

References myCheckColumns, myCurrentDepth, myCurrentTag, myExpectedAttrs, myHeader, myHeaderFormat, myMaxDepth, mySeenAttrs, myWroteHeader, TLF, and toString().

Referenced by writeAttr(), writeAttr(), writeAttr(), writeNull(), and writeTime().

Here is the caller graph for this function:

◆ closeTag()

bool CSVFormatter::closeTag ( std::ostream &  into,
const std::string &  comment = "" 
)
virtual

Closes the most recently opened tag.

Parameters
[in]intoThe output stream to use
Returns
Whether a further element existed in the stack and could be closed
Todo:
it is not verified that the topmost element was closed

Implements OutputFormatter.

Definition at line 69 of file CSVFormatter.cpp.

References joinToString(), myCheckColumns, myCurrentDepth, myExpectedAttrs, myHeader, myMaxDepth, mySeenAttrs, mySeparator, myWroteHeader, myXMLStack, toString(), WRITE_ERRORF, and WRITE_WARNING.

◆ getType()

OutputFormatterType OutputFormatter::getType ( )
inlineinherited

Returns the type of formatter being used.

Returns
the formatter type

Definition at line 149 of file OutputFormatter.h.

References OutputFormatter::myType.

Referenced by OutputDevice::writeAttr(), OutputDevice::writeAttr(), OutputDevice::writeFuncAttr(), and OutputDevice::writeOptionalAttr().

Here is the caller graph for this function:

◆ openTag() [1/2]

void CSVFormatter::openTag ( std::ostream &  into,
const std::string &  xmlElement 
)
virtual

Keeps track of an open XML tag by adding a new element to the stack.

Parameters
[in]intoThe output stream to use (unused)
[in]xmlElementName of element to open (unused)
Returns
The OutputDevice for further processing

Implements OutputFormatter.

Definition at line 39 of file CSVFormatter.cpp.

References myCurrentDepth, myCurrentTag, myMaxDepth, myWroteHeader, myXMLStack, and WRITE_WARNINGF.

◆ openTag() [2/2]

void CSVFormatter::openTag ( std::ostream &  into,
const SumoXMLTag xmlElement 
)
virtual

Keeps track of an open XML tag by adding a new element to the stack.

Parameters
[in]intoThe output stream to use (unused)
[in]xmlElementName of element to open (unused)

Implements OutputFormatter.

Definition at line 54 of file CSVFormatter.cpp.

References myCurrentDepth, myCurrentTag, myMaxDepth, myWroteHeader, myXMLStack, toString(), and WRITE_WARNINGF.

◆ setExpectedAttributes()

void CSVFormatter::setExpectedAttributes ( const SumoXMLAttrMask expected,
const int  depth = 2 
)
inlinevirtual

Set the expected attributes to write. This is used for tracking which attributes are expected in table like outputs. This should be not necessary but at least in the initial phase of implementing CSV and Parquet it helps a lot to track errors.

Parameters
[in]expectedwhich attributes are to be written (at the deepest XML level)
[in]depththe maximum XML hierarchy depth (excluding the root)

Reimplemented from OutputFormatter.

Definition at line 108 of file CSVFormatter.h.

References myCheckColumns, myExpectedAttrs, and myMaxDepth.

◆ writeAttr() [1/4]

template<>
void CSVFormatter::writeAttr ( std::ostream &  ,
const SumoXMLAttr  attr,
const std::string &  val 
)
inline

Definition at line 188 of file CSVFormatter.h.

References checkAttr(), myCurrentDepth, mySeparator, and myXMLStack.

◆ writeAttr() [2/4]

template<class T >
void CSVFormatter::writeAttr ( std::ostream &  into,
const std::string &  attr,
const T &  val 
)
inline

◆ writeAttr() [3/4]

template<>
void CSVFormatter::writeAttr ( std::ostream &  into,
const SumoXMLAttr  attr,
const double &  val 
)
inline

Definition at line 177 of file CSVFormatter.h.

References checkAttr(), myCurrentDepth, mySeparator, myXMLStack, and toString().

◆ writeAttr() [4/4]

template<class T >
void CSVFormatter::writeAttr ( std::ostream &  into,
const SumoXMLAttr  attr,
const T &  val 
)
inline

writes a named attribute

Parameters
[in]intoThe output stream to use
[in]attrThe attribute (name)
[in]valThe attribute value

Definition at line 76 of file CSVFormatter.h.

References checkAttr(), myCurrentDepth, mySeparator, myXMLStack, and toString().

◆ writeNull()

void CSVFormatter::writeNull ( std::ostream &  ,
const SumoXMLAttr  attr 
)
inline

Definition at line 94 of file CSVFormatter.h.

References checkAttr(), myCurrentDepth, mySeparator, and myXMLStack.

◆ writePadding()

virtual void OutputFormatter::writePadding ( std::ostream &  into,
const std::string &  val 
)
inlinevirtualinherited

Writes some whitespace to format the output. This method is only implemented for XML output.

Parameters
[in]intoThe output stream to use
[in]valThe whitespace

Reimplemented in PlainXMLFormatter.

Definition at line 135 of file OutputFormatter.h.

References UNUSED_PARAMETER.

Referenced by OutputDevice::writePadding().

Here is the caller graph for this function:

◆ writePreformattedTag()

virtual void OutputFormatter::writePreformattedTag ( std::ostream &  into,
const std::string &  val 
)
inlinevirtualinherited

Writes a preformatted tag to the device but ensures that any pending tags are closed. This method is only implemented for XML output.

Parameters
[in]intoThe output stream to use
[in]valThe preformatted data

Reimplemented in PlainXMLFormatter.

Definition at line 124 of file OutputFormatter.h.

References UNUSED_PARAMETER.

Referenced by OutputDevice::writePreformattedTag().

Here is the caller graph for this function:

◆ writeTime()

void CSVFormatter::writeTime ( std::ostream &  ,
const SumoXMLAttr  attr,
const SUMOTime  val 
)
inlinevirtual

Implements OutputFormatter.

Definition at line 99 of file CSVFormatter.h.

References checkAttr(), myCurrentDepth, mySeparator, myXMLStack, and time2string().

◆ writeXMLHeader()

virtual bool OutputFormatter::writeXMLHeader ( std::ostream &  into,
const std::string &  rootElement,
const std::map< SumoXMLAttr, std::string > &  attrs,
bool  includeConfig = true 
)
inlinevirtualinherited

Writes an XML header with optional configuration.

If something has been written (myXMLStack is not empty), nothing is written and false returned. The default implementation does nothing and returns false.

Parameters
[in]intoThe output stream to use
[in]rootElementThe root element to use
[in]attrsAdditional attributes to save within the rootElement
[in]includeConfigwhether the current config should be included as XML comment
Returns
whether something has been written

Reimplemented in PlainXMLFormatter.

Definition at line 77 of file OutputFormatter.h.

References UNUSED_PARAMETER.

Referenced by OutputDevice::writeXMLHeader().

Here is the caller graph for this function:

◆ wroteHeader()

bool CSVFormatter::wroteHeader ( ) const
inlinevirtual

Returns whether a header has been written. Useful to detect whether a file is being used by multiple sources.

Returns
Whether a header has been written

Implements OutputFormatter.

Definition at line 104 of file CSVFormatter.h.

References myWroteHeader.

Field Documentation

◆ myCheckColumns

bool CSVFormatter::myCheckColumns = false
private

whether the columns should be checked for completeness

Definition at line 163 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), setExpectedAttributes(), and writeAttr().

◆ myCurrentDepth

int CSVFormatter::myCurrentDepth = 0
private

the current depth of the XML hierarchy (excluding the root element)

Definition at line 157 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), openTag(), openTag(), writeAttr(), writeAttr(), writeAttr(), writeAttr(), writeNull(), and writeTime().

◆ myCurrentTag

std::string CSVFormatter::myCurrentTag
private

the currently read tag (only valid when generating the header)

Definition at line 148 of file CSVFormatter.h.

Referenced by checkAttr(), openTag(), openTag(), and writeAttr().

◆ myExpectedAttrs

SumoXMLAttrMask CSVFormatter::myExpectedAttrs
private

which CSV columns are expected (just for checking completeness)

Definition at line 166 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), and setExpectedAttributes().

◆ myHeader

std::vector<std::string> CSVFormatter::myHeader
private

the CSV header

Definition at line 145 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), and writeAttr().

◆ myHeaderFormat

const std::string CSVFormatter::myHeaderFormat
private

the format to use for the column names

Definition at line 139 of file CSVFormatter.h.

Referenced by checkAttr(), and CSVFormatter().

◆ myMaxDepth

int CSVFormatter::myMaxDepth = 0
private

the maximum depth of the XML hierarchy (excluding the root element)

Definition at line 154 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), openTag(), openTag(), and setExpectedAttributes().

◆ mySeenAttrs

SumoXMLAttrMask CSVFormatter::mySeenAttrs
private

which CSV columns have been set (just for checking completeness)

Definition at line 169 of file CSVFormatter.h.

Referenced by checkAttr(), and closeTag().

◆ mySeparator

const char CSVFormatter::mySeparator
private

The value separator.

Definition at line 142 of file CSVFormatter.h.

Referenced by closeTag(), writeAttr(), writeAttr(), writeAttr(), writeAttr(), writeNull(), and writeTime().

◆ myType

const OutputFormatterType OutputFormatter::myType
privateinherited

the type of formatter being used (XML, CSV, Parquet, etc.)

Definition at line 167 of file OutputFormatter.h.

Referenced by OutputFormatter::getType().

◆ myWroteHeader

bool CSVFormatter::myWroteHeader = false
private

whether the CSV header line has been written

Definition at line 160 of file CSVFormatter.h.

Referenced by checkAttr(), closeTag(), CSVFormatter(), openTag(), openTag(), writeAttr(), and wroteHeader().

◆ myXMLStack

std::vector<std::unique_ptr<std::ostringstream> > CSVFormatter::myXMLStack
private

The attributes to write for each begun xml element (excluding the root element)

Definition at line 151 of file CSVFormatter.h.

Referenced by closeTag(), openTag(), openTag(), writeAttr(), writeAttr(), writeAttr(), writeAttr(), writeNull(), and writeTime().


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