Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
CSVFormatter.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Output formatter for CSV output
19/****************************************************************************/
20#pragma once
21#include <config.h>
22
23#include <memory>
24#include "OutputFormatter.h"
25
26
27// ===========================================================================
28// class definitions
29// ===========================================================================
35public:
37 CSVFormatter(const std::string& columnNames, const char separator = ';');
38
40 virtual ~CSVFormatter() { }
41
48 void openTag(std::ostream& into, const std::string& xmlElement);
49
55 void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
56
63 bool closeTag(std::ostream& into, const std::string& comment = "");
64
71 template <class T>
72 void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val, const bool isNull) {
73 checkAttr(attr);
74 myValues.emplace_back(isNull ? "" : toString(val, into.precision()));
75 }
76
77 template <class T>
78 void writeAttr(std::ostream& into, const std::string& attr, const T& val, const bool isNull) {
79 assert(!myCheckColumns);
80 checkHeader(attr);
81 myValues.emplace_back(isNull ? "" : toString(val, into.precision()));
82 }
83
84 void writeTime(std::ostream& /* into */, const SumoXMLAttr attr, const SUMOTime val) {
85 checkAttr(attr);
86 myValues.emplace_back(time2string(val));
87 }
88
89 bool wroteHeader() const {
90 return myWroteHeader;
91 }
92
93 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth = 2) {
94 myExpectedAttrs = expected;
95 myMaxDepth = depth;
96 myCheckColumns = expected.any();
97 }
98
99private:
106 inline void checkAttr(const SumoXMLAttr attr) {
107 if (myCheckColumns && myMaxDepth == (int)myXMLStack.size()) {
108 mySeenAttrs.set(attr);
109 if (!myExpectedAttrs.test(attr)) {
110 throw ProcessError(TLF("Unexpected attribute '%', this file format does not support CSV output yet.", toString(attr)));
111 }
112 }
113 checkHeader(attr);
114 }
115
116 template <class ATTR_TYPE>
117 inline void checkHeader(const ATTR_TYPE& attr) {
118 myNeedsWrite = true;
119 if (!myWroteHeader) {
120 std::string headerName = toString(attr);
121 if (myHeaderFormat != "plain" && !(myHeaderFormat == "auto" && std::find(myHeader.begin(), myHeader.end(), headerName) == myHeader.end())) {
122 headerName = myCurrentTag + "_" + headerName;
123 }
124 if (std::find(myHeader.begin(), myHeader.end(), headerName) == myHeader.end()) {
125 for (std::string& row : myBufferedRows) {
126 row += mySeparator;
127 }
128 while (myValues.size() < myHeader.size()) {
129 myValues.emplace_back("");
130 }
131 myHeader.emplace_back(headerName);
132 }
133 }
134 }
135
137 const std::string myHeaderFormat;
138
140 const char mySeparator;
141
143 std::vector<std::string> myHeader;
144
146 std::string myCurrentTag;
147
149 std::vector<int> myXMLStack;
150
152 std::vector<std::string> myValues;
153
155 int myMaxDepth = 2;
156
158 bool myWroteHeader = false;
159
161 bool myNeedsWrite = false;
162
164 std::vector<std::string> myBufferedRows;
165
167 bool myCheckColumns = false;
168
171
174};
long long int SUMOTime
Definition GUI.h:36
#define TLF(string,...)
Definition MsgHandler.h:306
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::bitset< 96 > SumoXMLAttrMask
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
Output formatter for CSV output.
const std::string myHeaderFormat
the format to use for the column names
void writeAttr(std::ostream &into, const std::string &attr, const T &val, const bool isNull)
bool myNeedsWrite
whether any attribute has been written since the last row was emitted
std::string myCurrentTag
the currently read tag (only valid when generating the header)
bool wroteHeader() const
Returns whether a header has been written. Useful to detect whether a file is being used by multiple ...
void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val, const bool isNull)
writes a named attribute
SumoXMLAttrMask myExpectedAttrs
which CSV columns are expected (just for checking completeness)
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 tabl...
SumoXMLAttrMask mySeenAttrs
which CSV columns have been set (just for checking completeness)
std::vector< int > myXMLStack
The number of attributes in the currently open XML elements.
std::vector< std::string > myHeader
the CSV header
virtual ~CSVFormatter()
Destructor.
int myMaxDepth
the maximum depth of the XML hierarchy (excluding the root element)
bool myCheckColumns
whether the columns should be checked for completeness
const char mySeparator
The value separator.
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
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 checkHeader(const ATTR_TYPE &attr)
void writeTime(std::ostream &, const SumoXMLAttr attr, const SUMOTime val)
void checkAttr(const SumoXMLAttr attr)
Helper function to keep track of the written attributes and accumulate the header....
bool myWroteHeader
whether the CSV header line has been written
std::vector< std::string > myValues
the current attribute / column values
std::vector< std::string > myBufferedRows
partial rows buffered before the schema is known (depth < myMaxDepth)
Abstract base class for output formatters.