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#ifdef _MSC_VER
24#pragma warning(push)
25#pragma warning(disable: 4868) // mask warning about left-to-right evaluation order
26#endif
27#include <fmt/ostream.h>
28#include <fmt/ranges.h>
29#ifdef _MSC_VER
30#pragma warning(pop)
31#endif
32#endif
35#include "CSVFormatter.h"
36
37
38// ===========================================================================
39// member method definitions
40// ===========================================================================
41CSVFormatter::CSVFormatter(const std::string& columnNames, const char separator)
42 : OutputFormatter(OutputFormatterType::CSV), myHeaderFormat(columnNames), mySeparator(separator) {
43 if (myHeaderFormat == "none") {
44 myWroteHeader = true;
45 }
46}
47
48
49bool
50CSVFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
51 const std::map<SumoXMLAttr, std::string>& attrs, bool /* includeConfig */) {
52 if (attrs.size() > 2) {
53 myHaveRootAttrs = true;
54 openTag(into, rootElement);
55 for (const auto& a : attrs) {
56 if (a.first != SUMO_ATTR_XMLNS && a.first != SUMO_ATTR_SCHEMA_LOCATION) {
57 writeAttr(into, a.first, a.second, false, false);
58 }
59 }
60 return true;
61 }
62 return false;
63}
64
65
66void
67CSVFormatter::openTag(std::ostream& /* into */, const std::string& xmlElement) {
68 myXMLStack.push_back({xmlElement, (int)myValues.size()});
69}
70
71
72void
73CSVFormatter::openTag(std::ostream& /* into */, const SumoXMLTag& xmlElement) {
74 myXMLStack.push_back({toString(xmlElement), (int)myValues.size()});
75}
76
77
78bool
79CSVFormatter::closeTag(std::ostream& into, const std::string& /* comment */) {
80 if (myMaxDepth == 0) {
81 // the auto detection case: the first closed tag determines the depth
82 myMaxDepth = (int)myXMLStack.size();
83 }
84 const bool eof = myXMLStack.empty() || (myHaveRootAttrs && myXMLStack.size() == 1);
85 if ((myMaxDepth == (int)myXMLStack.size() || eof) && !myWroteHeader) {
86 // First complete row or EOF: write the header
87 if (!myCheckColumns) {
88 WRITE_WARNING("Column based formats are still experimental. Autodetection only works for homogeneous output.");
89 }
90 bool full = myHeaderFormat == "full";
91 if (myHeaderFormat == "auto") {
92 const std::set<std::string> uniq(myHeader.begin(), myHeader.end());
93 full = uniq.size() < myHeader.size();
94 }
95#ifdef HAVE_FMT
96 fmt::print(into, "{}\n", fmt::join(full ? myFullHeader : myHeader, std::string_view(&mySeparator, 1)));
97#else
98 into << joinToString(full ? myFullHeader : myHeader, mySeparator) << "\n";
99#endif
100 myWroteHeader = true;
101 }
102 if (myNeedsWrite) {
103 myValues.resize(myHeader.size());
104#ifdef HAVE_FMT
105 const std::string row = fmt::format("{}", fmt::join(myValues, std::string_view(&mySeparator, 1)));
106#else
107 const std::string row = joinToString(myValues, mySeparator);
108#endif
109 myBufferedRows.emplace_back(row);
110 mySeenAttrs.reset();
111 myNeedsWrite = false;
112 }
113 if (myWroteHeader && !myBufferedRows.empty()) {
114 for (const std::string& row : myBufferedRows) {
115 into << row << '\n';
116 }
117 myBufferedRows.clear();
118 }
119 if (!eof) {
120 if ((int)myValues.size() > myXMLStack.back().second) {
121 myValues.resize(myXMLStack.back().second);
122 }
123 myXMLStack.pop_back();
124 return true;
125 }
126 return false;
127}
128
129
130/****************************************************************************/
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
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:322
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
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.
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
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.
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.