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-2025 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
24#include "CSVFormatter.h"
25
26
27// ===========================================================================
28// member method definitions
29// ===========================================================================
30CSVFormatter::CSVFormatter(const std::string& columnNames, const char separator)
31 : OutputFormatter(OutputFormatterType::CSV), myHeaderFormat(columnNames), mySeparator(separator) {
32 if (myHeaderFormat == "none") {
33 myWroteHeader = true;
34 }
35}
36
37
38void
39CSVFormatter::openTag(std::ostream& /* into */, const std::string& xmlElement) {
41 if (myCurrentDepth > (int)myXMLStack.size()) {
42 myXMLStack.emplace_back(std::unique_ptr<std::ostringstream>(new std::ostringstream()));
43 }
44 if (!myWroteHeader) {
45 myCurrentTag = xmlElement;
46 }
47 if (myMaxDepth == myCurrentDepth && myWroteHeader && myCurrentTag != xmlElement) {
48 WRITE_WARNINGF("Encountered mismatch in XML tags (expected % but got %). Column names may be incorrect.", myCurrentTag, xmlElement);
49 }
50}
51
52
53void
54CSVFormatter::openTag(std::ostream& /* into */, const SumoXMLTag& xmlElement) {
56 if (myCurrentDepth > (int)myXMLStack.size()) {
57 myXMLStack.emplace_back(std::unique_ptr<std::ostringstream>(new std::ostringstream()));
58 }
59 if (!myWroteHeader) {
60 myCurrentTag = toString(xmlElement);
61 }
62 if (myMaxDepth == myCurrentDepth && myWroteHeader && myCurrentTag != toString(xmlElement)) {
63 WRITE_WARNINGF("Encountered mismatch in XML tags (expected % but got %). Column names may be incorrect.", myCurrentTag, toString(xmlElement));
64 }
65}
66
67
68bool
69CSVFormatter::closeTag(std::ostream& into, const std::string& /* comment */) {
70 if (myMaxDepth == 0) {
72 }
74 if (!myCheckColumns) {
75 WRITE_WARNING("Column based formats are still experimental. Autodetection only works for homogeneous output.");
76 }
77 into << joinToString(myHeader, mySeparator) << "\n";
78 myWroteHeader = true;
79 }
82 WRITE_ERRORF("Incomplete attribute set '%', this file format does not support CSV output yet.", toString(mySeenAttrs));
83 }
84 for (auto it = myXMLStack.begin(); it != myXMLStack.end() - 1; ++it) {
85 into << (*it)->str();
86 }
87 // remove the final separator
88 std::string final = myXMLStack[myCurrentDepth - 1]->str();
89 final[final.size() - 1] = '\n';
90 into << final;
91 mySeenAttrs.reset();
92 }
93 if (myCurrentDepth > 0) {
94 if (!myWroteHeader) {
95 const std::string text = myXMLStack[myCurrentDepth - 1]->str();
96 const int count = (int)std::count(text.begin(), text.end(), mySeparator);
97 myHeader.resize(myHeader.size() - count);
98 }
99 myXMLStack[myCurrentDepth - 1]->str("");
100 myXMLStack[myCurrentDepth - 1]->clear();
102 }
103 return false;
104}
105
106
107/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
OutputFormatterType
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition ToString.h:283
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
const std::string myHeaderFormat
the format to use for the column names
int myCurrentDepth
the current depth of the XML hierarchy (excluding the root element)
std::string myCurrentTag
the currently read tag (only valid when generating the header)
SumoXMLAttrMask myExpectedAttrs
which CSV columns are expected (just for checking completeness)
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.
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.
CSVFormatter(const std::string &columnNames, const char separator=';')
Constructor.
std::vector< std::unique_ptr< std::ostringstream > > myXMLStack
The attributes to write for each begun xml element (excluding the root element)
bool myWroteHeader
whether the CSV header line has been written
Abstract base class for output formatters.