Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
CSVFormatter.cpp
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// An output formatter for CSV files
19/****************************************************************************/
20#include <config.h>
21
22#ifdef HAVE_FMT
23#include <fmt/ostream.h>
24#include <fmt/ranges.h>
25#endif
28#include "CSVFormatter.h"
29
30
31// ===========================================================================
32// member method definitions
33// ===========================================================================
34CSVFormatter::CSVFormatter(const std::string& columnNames, const char separator)
35 : OutputFormatter(OutputFormatterType::CSV), myHeaderFormat(columnNames), mySeparator(separator) {
36 if (myHeaderFormat == "none") {
37 myWroteHeader = true;
38 }
39}
40
41
42bool
43CSVFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
44 const std::map<SumoXMLAttr, std::string>& attrs, bool /* writeMetadata */,
45 bool /* includeConfig */) {
46 if (attrs.size() > 2) {
47 myHaveRootAttrs = true;
48 openTag(into, rootElement);
49 for (const auto& a : attrs) {
50 if (a.first != SUMO_ATTR_XMLNS && a.first != SUMO_ATTR_SCHEMA_LOCATION) {
51 writeAttr(into, a.first, a.second, false, false);
52 }
53 }
54 return true;
55 }
56 return false;
57}
58
59
60void
61CSVFormatter::openTag(std::ostream& /* into */, const std::string& xmlElement) {
62 myXMLStack.push_back({xmlElement, (int)myValues.size()});
63}
64
65
66void
67CSVFormatter::openTag(std::ostream& /* into */, const SumoXMLTag& xmlElement) {
68 myXMLStack.push_back({toString(xmlElement), (int)myValues.size()});
69}
70
71
72bool
73CSVFormatter::closeTag(std::ostream& into, const std::string& /* comment */) {
74 if (myMaxDepth == 0) {
75 // the auto detection case: the first closed tag determines the depth
76 myMaxDepth = (int)myXMLStack.size();
77 }
78 const bool eof = myXMLStack.empty() || (myHaveRootAttrs && myXMLStack.size() == 1);
79 if ((myMaxDepth == (int)myXMLStack.size() || eof) && !myWroteHeader) {
80 // First complete row or EOF: write the header
81 if (!myCheckColumns) {
82 WRITE_WARNING("Column based formats are still experimental. Autodetection only works for homogeneous output.");
83 }
84 bool full = myHeaderFormat == "full";
85 if (myHeaderFormat == "auto") {
86 const std::set<std::string> uniq(myHeader.begin(), myHeader.end());
87 full = uniq.size() < myHeader.size();
88 }
89#ifdef HAVE_FMT
90 fmt::print(into, "{}\n", fmt::join(full ? myFullHeader : myHeader, std::string_view(&mySeparator, 1)));
91#else
92 into << joinToString(full ? myFullHeader : myHeader, mySeparator) << "\n";
93#endif
94 myWroteHeader = true;
95 }
96 if (myNeedsWrite) {
97 myValues.resize(myHeader.size());
98#ifdef HAVE_FMT
99 const std::string row = fmt::format("{}", fmt::join(myValues, std::string_view(&mySeparator, 1)));
100#else
101 const std::string row = joinToString(myValues, mySeparator);
102#endif
103 myBufferedRows.emplace_back(row);
104 mySeenAttrs.reset();
105 myNeedsWrite = false;
106 }
107 if (myWroteHeader && !myBufferedRows.empty()) {
108 for (const std::string& row : myBufferedRows) {
109 into << row << '\n';
110 }
111 myBufferedRows.clear();
112 }
113 if (!eof) {
114 if ((int)myValues.size() > myXMLStack.back().second) {
115 myValues.resize(myXMLStack.back().second);
116 }
117 myXMLStack.pop_back();
118 return true;
119 }
120 return false;
121}
122
123
124/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
OutputFormatterType
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_ATTR_XMLNS
@ SUMO_ATTR_SCHEMA_LOCATION
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:314
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
const std::string myHeaderFormat
the format to use for the column names
bool myHaveRootAttrs
whether any root attribute have been encountered
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 myNeedsWrite
whether any attribute has been written since the last row was emitted
void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val, const bool isNull, const bool escape)
Writes a named attribute.
bool closeTag(std::ostream &into, const std::string &comment="") override
Closes the most recently opened tag.
SumoXMLAttrMask mySeenAttrs
which CSV columns have been set (just for checking completeness)
std::vector< std::string > myHeader
the CSV header
int myMaxDepth
the maximum depth of the XML hierarchy (excluding the root element)
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool, bool) override
Writes an "XML header".
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.
CSVFormatter(const std::string &columnNames, const char separator=';')
Constructor.
std::vector< std::string > myFullHeader
the CSV header if we write the full name
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.