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 <algorithm>
25#include "OutputFormatter.h"
28
29
30// ===========================================================================
31// class definitions
32// ===========================================================================
38public:
40 CSVFormatter(const std::string& columnNames, const char separator = ';');
41
43 virtual ~CSVFormatter() override { }
44
54 bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
55 const std::map<SumoXMLAttr, std::string>& attrs, bool /* includeConfig */) override;
56
62 void openTag(std::ostream& into, const std::string& xmlElement) override;
63
69 void openTag(std::ostream& into, const SumoXMLTag& xmlElement) override;
70
79 bool closeTag(std::ostream& into, const std::string& comment = "") override;
80
91 template <class T>
92 void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val, const bool isNull, const bool escape) {
93 UNUSED_PARAMETER(escape);
94 checkAttr(attr);
95 myValues.emplace_back(isNull ? "" : toString(val, into.precision()));
96 }
97
108 template <class T>
109 void writeAttr(std::ostream& into, const std::string& attr, const T& val, const bool isNull, const bool escape) {
110 UNUSED_PARAMETER(escape);
111 assert(!myCheckColumns);
112 checkHeader(attr);
113 myValues.emplace_back(isNull ? "" : toString(val, into.precision()));
114 }
115
117 void writeAttr(std::ostream& /* into */, const SumoXMLAttr attr, const std::string& val, const bool isNull, const bool escape) {
118 checkAttr(attr);
119 myValues.emplace_back(isNull ? "" : (escape ? StringUtils::escapeCSV(val, mySeparator) : val));
120 }
121 void writeAttr(std::ostream& /* into */, const std::string& attr, const std::string& val, const bool isNull, const bool escape) {
122 assert(!myCheckColumns);
123 checkHeader(attr);
124 myValues.emplace_back(isNull ? "" : (escape ? StringUtils::escapeCSV(val, mySeparator) : val));
125 }
126
133 void writeTime(std::ostream& /* into */, const SumoXMLAttr attr, const SUMOTime val) override {
134 checkAttr(attr);
135 myValues.emplace_back(time2string(val));
136 }
137
142 bool wroteHeader() const override {
143 return myWroteHeader;
144 }
145
160 void setExpectedAttributes(const SumoXMLAttrMask& expected, const int depth) override {
161 myExpectedAttrs = expected;
162 myMaxDepth = depth;
163 myCheckColumns = expected.any();
164 }
165
166private:
173 inline void checkAttr(const SumoXMLAttr attr) {
174 if (myCheckColumns && myMaxDepth == (int)myXMLStack.size()) {
175 mySeenAttrs.set(attr);
176 if (!myExpectedAttrs.test(attr)) {
177 throw ProcessError(TLF("Unexpected attribute '%', this file format does not support CSV output yet.", toString(attr)));
178 }
179 }
180 checkHeader(attr);
181 }
182
183 template <class ATTR_TYPE>
184 inline void checkHeader(const ATTR_TYPE& attr) {
185 myNeedsWrite = true;
186 if (!myWroteHeader) {
187 std::string headerName = toString(attr);
188 std::string prefix;
189 for (const auto& entry : myXMLStack) {
190 prefix += entry.first + "_";
191 }
192 const std::string fullHeaderName = prefix + headerName;
193 if (myHeaderFormat != "plain") {
194 headerName = myXMLStack.back().first + "_" + headerName;
195 }
196 const auto colIt = std::find(myFullHeader.begin(), myFullHeader.end(), fullHeaderName);
197 if (colIt == myFullHeader.end()) {
198 for (std::string& row : myBufferedRows) {
199 row += mySeparator;
200 }
201 myValues.resize(myHeader.size());
202 myHeader.emplace_back(headerName);
203 myFullHeader.emplace_back(fullHeaderName);
204 } else {
205 // there might be missing attributes inbetween, so make sure header position and value size match
206 myValues.resize(std::distance(myFullHeader.begin(), colIt));
207 }
208 }
209 }
210
212 const std::string myHeaderFormat;
213
215 const char mySeparator;
216
218 std::vector<std::string> myHeader;
219
221 std::vector<std::string> myFullHeader;
222
224 std::vector<std::pair<const std::string, int> > myXMLStack;
225
227 std::vector<std::string> myValues;
228
230 int myMaxDepth = 1000;
231
233 bool myWroteHeader = false;
234
236 bool myNeedsWrite = false;
237
239 bool myHaveRootAttrs = false;
240
242 std::vector<std::string> myBufferedRows;
243
245 bool myCheckColumns = false;
246
249
252};
long long int SUMOTime
Definition GUI.h:36
#define TLF(string,...)
Definition MsgHandler.h:318
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
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:57
Output formatter for CSV output.
const std::string myHeaderFormat
the format to use for the column names
bool myHaveRootAttrs
whether any root attribute have been encountered
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
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.
bool wroteHeader() const override
Whether a complete row has been encountered and triggered header writing.
bool myNeedsWrite
whether any attribute has been written since the last row was emitted
void writeAttr(std::ostream &, const std::string &attr, const std::string &val, const bool isNull, const bool escape)
void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val, const bool isNull, const bool escape)
Writes a named attribute.
SumoXMLAttrMask myExpectedAttrs
which CSV columns are expected (just for checking completeness)
void writeTime(std::ostream &, const SumoXMLAttr attr, const SUMOTime val) override
Writes a time value using time2string.
bool closeTag(std::ostream &into, const std::string &comment="") override
Closes the most recently opened tag.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool) override
Writes an "XML header".
SumoXMLAttrMask mySeenAttrs
which CSV columns have been set (just for checking completeness)
std::vector< std::string > myHeader
the CSV header
virtual ~CSVFormatter() override
Destructor.
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth) override
Which elements are expected and which maximum depth the XML tree has.
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.
std::vector< std::pair< const std::string, int > > myXMLStack
The name and number of attributes in the currently open XML elements.
void writeAttr(std::ostream &into, const std::string &attr, const T &val, const bool isNull, const bool escape)
Writes a named attribute.
void checkHeader(const ATTR_TYPE &attr)
std::vector< std::string > myFullHeader
the CSV header if we write the full name
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.
static std::string escapeCSV(const std::string &orig, const char separator, const char quote='"')
Adds quotes to strings containing the separator or the quote char and escapes the quote char with a b...
#define UNUSED_PARAMETER(x)