![]() |
Eclipse SUMO - Simulation of Urban MObility
|
Output formatter for CSV output. More...
#include <CSVFormatter.h>
Public Member Functions | |
| bool | closeTag (std::ostream &into, const std::string &comment="") override |
| Closes the most recently opened tag. | |
| CSVFormatter (const std::string &columnNames, const char separator=';') | |
| Constructor. | |
| OutputFormatterType | getType () const |
| Returns the type of formatter being used. | |
| void | openTag (std::ostream &into, const std::string &xmlElement) override |
| Keeps track of an open XML tag by adding a new element to the stack. | |
| void | openTag (std::ostream &into, const SumoXMLTag &xmlElement) override |
| Keeps track of an open XML tag by adding a new element to the stack. | |
| void | setExpectedAttributes (const SumoXMLAttrMask &expected, const int depth) override |
| Which elements are expected and which maximum depth the XML tree has. | |
| void | writeAttr (std::ostream &, const std::string &attr, const std::string &val, const bool isNull, const bool escape) |
| void | writeAttr (std::ostream &, const SumoXMLAttr attr, const std::string &val, const bool isNull, const bool escape) |
| typed overloads (non-template) – picked by overload resolution over the template | |
| template<class T > | |
| void | writeAttr (std::ostream &into, const std::string &attr, const T &val, const bool isNull, const bool escape) |
| Writes a named attribute. | |
| template<class T > | |
| void | writeAttr (std::ostream &into, const SumoXMLAttr attr, const T &val, const bool isNull, const bool escape) |
| Writes a named attribute. | |
| 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) override |
| Writes a time value using time2string. | |
| bool | writeXMLHeader (std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool, bool) override |
| Writes an "XML header". | |
| bool | wroteHeader () const override |
| Whether a complete row has been encountered and triggered header writing. | |
| virtual | ~CSVFormatter () override |
| 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. | |
| template<class ATTR_TYPE > | |
| void | checkHeader (const ATTR_TYPE &attr) |
Private Attributes | |
| std::vector< std::string > | myBufferedRows |
| partial rows buffered before the schema is known (depth < myMaxDepth) | |
| bool | myCheckColumns = false |
| whether the columns should be checked for completeness | |
| SumoXMLAttrMask | myExpectedAttrs |
| which CSV columns are expected (just for checking completeness) | |
| std::vector< std::string > | myFullHeader |
| the CSV header if we write the full name | |
| bool | myHaveRootAttrs = false |
| whether any root attribute have been encountered | |
| std::vector< std::string > | myHeader |
| the CSV header | |
| const std::string | myHeaderFormat |
| the format to use for the column names | |
| int | myMaxDepth = 1000 |
| the maximum depth of the XML hierarchy (excluding the root element) | |
| bool | myNeedsWrite = false |
| whether any attribute has been written since the last row was emitted | |
| 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.) | |
| std::vector< std::string > | myValues |
| the current attribute / column values | |
| bool | myWroteHeader = false |
| whether the CSV header line has been written | |
| std::vector< std::pair< const std::string, int > > | myXMLStack |
| The name and number of attributes in the currently open XML elements. | |
Output formatter for CSV output.
Definition at line 37 of file CSVFormatter.h.
| CSVFormatter::CSVFormatter | ( | const std::string & | columnNames, |
| const char | separator = ';' |
||
| ) |
Constructor.
Definition at line 34 of file CSVFormatter.cpp.
References myHeaderFormat, and myWroteHeader.
|
inlineoverridevirtual |
Destructor.
Definition at line 43 of file CSVFormatter.h.
|
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.
| [in] | attr | The attribute (name as enum value) |
Definition at line 174 of file CSVFormatter.h.
References checkHeader(), myCheckColumns, myExpectedAttrs, myMaxDepth, mySeenAttrs, myXMLStack, TLF, and toString().
Referenced by writeAttr(), writeAttr(), and writeTime().
|
inlineprivate |
Definition at line 185 of file CSVFormatter.h.
References myBufferedRows, myFullHeader, myHeader, myHeaderFormat, myNeedsWrite, mySeparator, myValues, myWroteHeader, myXMLStack, and toString().
Referenced by checkAttr(), writeAttr(), and writeAttr().
|
overridevirtual |
Closes the most recently opened tag.
This is where the main action starts. This function determines whether a row is completed and can be written.
| [in] | into | The output stream to use |
| [in] | comment | A comment to write after the tag (ignored for CSV) |
Implements OutputFormatter.
Definition at line 73 of file CSVFormatter.cpp.
References joinToString(), myBufferedRows, myCheckColumns, myFullHeader, myHaveRootAttrs, myHeader, myHeaderFormat, myMaxDepth, myNeedsWrite, mySeenAttrs, mySeparator, myValues, myWroteHeader, myXMLStack, and WRITE_WARNING.
|
inlineinherited |
Returns the type of formatter being used.
Definition at line 152 of file OutputFormatter.h.
References OutputFormatter::myType.
Referenced by OutputDevice::isXML(), OutputDevice::lf(), OutputDevice::writeAttr(), OutputDevice::writeFuncAttr(), and OutputDevice::writeOptionalAttr().
|
overridevirtual |
Keeps track of an open XML tag by adding a new element to the stack.
| [in] | into | The output stream to use (unused) |
| [in] | xmlElement | Name of the element to open |
Implements OutputFormatter.
Definition at line 61 of file CSVFormatter.cpp.
References myValues, and myXMLStack.
Referenced by writeXMLHeader().
|
overridevirtual |
Keeps track of an open XML tag by adding a new element to the stack.
| [in] | into | The output stream to use (unused) |
| [in] | xmlElement | Enum identifier of the element to open |
Implements OutputFormatter.
Definition at line 67 of file CSVFormatter.cpp.
References myValues, myXMLStack, and toString().
|
inlineoverridevirtual |
Which elements are expected and which maximum depth the XML tree has.
This is not necessary for the functionality but very useful for debugging and tracking whether expected attributes are still missing (triggers an error in checkAttr). If expected is empty, no tracking takes place.
The depth parameter is only for performance. If a tag at this depth is closed for the first time, the header is being written and buffered rows may be flushed. Setting it to a large value (which is also the default) means you are on the safe side if more attributes or tags show up later but it may result in buffering the complete output before writing the first line. Setting it to 0 triggers auto detection which means the first time a tag is closed the maximum depth will be determined.
| [in] | expected | The enum values of the attrs which should be present before a row can be written. |
| [in] | depth | The maximum expected depth of nested XML elements. |
Reimplemented from OutputFormatter.
Definition at line 161 of file CSVFormatter.h.
References myCheckColumns, myExpectedAttrs, and myMaxDepth.
|
inline |
Definition at line 122 of file CSVFormatter.h.
References checkHeader(), StringUtils::escapeCSV(), myCheckColumns, mySeparator, and myValues.
|
inline |
typed overloads (non-template) – picked by overload resolution over the template
Definition at line 118 of file CSVFormatter.h.
References checkAttr(), StringUtils::escapeCSV(), mySeparator, and myValues.
|
inline |
Writes a named attribute.
This calls checkHeader to check/add this to the known attributes and then adds the string conversion to myValues.
| [in] | into | The output stream to use (used only to determine precision) |
| [in] | attr | The attribute (name as string) |
| [in] | val | The attribute value |
| [in] | isNull | whether this is actually a null value (writes the empty string) |
| [in] | escape | Whether the value should be processed by StringUtils::escapeCSV (only used in the string variant below) |
Definition at line 110 of file CSVFormatter.h.
References checkHeader(), myCheckColumns, myValues, toString(), and UNUSED_PARAMETER.
|
inline |
Writes a named attribute.
This calls checkAttr to check/add this to the known attributes and then adds the string conversion to myValues.
| [in] | into | The output stream to use (used only to determine precision) |
| [in] | attr | The attribute (name as enum value) |
| [in] | val | The attribute value |
| [in] | isNull | whether this is actually a null value (writes the empty string) |
| [in] | escape | Whether the value should be processed by StringUtils::escapeCSV (only used in the string variant below) |
Definition at line 93 of file CSVFormatter.h.
References checkAttr(), myValues, toString(), and UNUSED_PARAMETER.
Referenced by writeXMLHeader().
|
inlinevirtualinherited |
Writes some whitespace to format the output. This method is only implemented for XML output.
| [in] | into | The output stream to use |
| [in] | val | The whitespace |
Reimplemented in PlainXMLFormatter.
Definition at line 138 of file OutputFormatter.h.
References UNUSED_PARAMETER.
Referenced by OutputDevice::writePadding().
|
inlinevirtualinherited |
Writes a preformatted tag to the device but ensures that any pending tags are closed. This method is only implemented for XML output.
| [in] | into | The output stream to use |
| [in] | val | The preformatted data |
Reimplemented in PlainXMLFormatter.
Definition at line 127 of file OutputFormatter.h.
References UNUSED_PARAMETER.
Referenced by OutputDevice::writePreformattedTag().
|
inlineoverridevirtual |
Writes a time value using time2string.
| [in] | into | The output stream to use (unused) |
| [in] | attr | The attribute (name as enum value) |
| [in] | val | The attribute value |
Implements OutputFormatter.
Definition at line 134 of file CSVFormatter.h.
References checkAttr(), myValues, and time2string().
|
overridevirtual |
Writes an "XML header".
For CSV output the header is only relevant if it contains additional attributes.
| [in] | into | The output stream to use |
| [in] | rootElement | The root element to use |
| [in] | attrs | Additional attributes to save within the rootElement |
Reimplemented from OutputFormatter.
Definition at line 43 of file CSVFormatter.cpp.
References myHaveRootAttrs, openTag(), SUMO_ATTR_SCHEMA_LOCATION, SUMO_ATTR_XMLNS, and writeAttr().
|
inlineoverridevirtual |
Whether a complete row has been encountered and triggered header writing.
Implements OutputFormatter.
Definition at line 143 of file CSVFormatter.h.
References myWroteHeader.
|
private |
partial rows buffered before the schema is known (depth < myMaxDepth)
Definition at line 243 of file CSVFormatter.h.
Referenced by checkHeader(), and closeTag().
|
private |
whether the columns should be checked for completeness
Definition at line 246 of file CSVFormatter.h.
Referenced by checkAttr(), closeTag(), setExpectedAttributes(), writeAttr(), and writeAttr().
|
private |
which CSV columns are expected (just for checking completeness)
Definition at line 249 of file CSVFormatter.h.
Referenced by checkAttr(), and setExpectedAttributes().
|
private |
the CSV header if we write the full name
Definition at line 222 of file CSVFormatter.h.
Referenced by checkHeader(), and closeTag().
|
private |
whether any root attribute have been encountered
Definition at line 240 of file CSVFormatter.h.
Referenced by closeTag(), and writeXMLHeader().
|
private |
the CSV header
Definition at line 219 of file CSVFormatter.h.
Referenced by checkHeader(), and closeTag().
|
private |
the format to use for the column names
Definition at line 213 of file CSVFormatter.h.
Referenced by checkHeader(), closeTag(), and CSVFormatter().
|
private |
the maximum depth of the XML hierarchy (excluding the root element)
Definition at line 231 of file CSVFormatter.h.
Referenced by checkAttr(), closeTag(), and setExpectedAttributes().
|
private |
whether any attribute has been written since the last row was emitted
Definition at line 237 of file CSVFormatter.h.
Referenced by checkHeader(), and closeTag().
|
private |
which CSV columns have been set (just for checking completeness)
Definition at line 252 of file CSVFormatter.h.
Referenced by checkAttr(), and closeTag().
|
private |
The value separator.
Definition at line 216 of file CSVFormatter.h.
Referenced by checkHeader(), closeTag(), writeAttr(), and writeAttr().
|
privateinherited |
the type of formatter being used (XML, CSV, Parquet, etc.)
Definition at line 170 of file OutputFormatter.h.
Referenced by OutputFormatter::getType().
|
private |
the current attribute / column values
Definition at line 228 of file CSVFormatter.h.
Referenced by checkHeader(), closeTag(), openTag(), openTag(), writeAttr(), writeAttr(), writeAttr(), writeAttr(), and writeTime().
|
private |
whether the CSV header line has been written
Definition at line 234 of file CSVFormatter.h.
Referenced by checkHeader(), closeTag(), CSVFormatter(), and wroteHeader().
|
private |
The name and number of attributes in the currently open XML elements.
Definition at line 225 of file CSVFormatter.h.
Referenced by checkAttr(), checkHeader(), closeTag(), openTag(), and openTag().