Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/importio/NamedColumnsParser.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-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
/****************************************************************************/
14
/// @file NamedColumnsParser.h
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @date Fri, 19 Jul 2002
18
///
19
// A parser to retrieve information from a table with known columns
20
/****************************************************************************/
21
#pragma once
22
#include <config.h>
23
24
#include <map>
25
#include <string>
26
#include <utils/common/StringTokenizer.h>
27
28
29
// ===========================================================================
30
// class definitions
31
// ===========================================================================
32
/**
33
* @class NamedColumnsParser
34
* @brief A parser to retrieve information from a table with known columns
35
*
36
* When initialised, this parser stores the given information about the
37
* order of the named elements and allows the retrieval of lines using the
38
* names of these elements.
39
* Use it like this:
40
* @arg Initialise with "Name;PositionX;PositionY"
41
* (defDelim=default=";")
42
* (lineDelim=default=";")
43
* @arg Parse each line of a table using "parseLine" (parseLine("Dummy;0;0"))
44
* @arg get values using operations like: string posX = get("PositionX");
45
*
46
* @todo What happens if an uninitialised NamedColumnsParser is used? exceptions?
47
*/
48
class NamedColumnsParser {
49
public:
50
/** @brief Constructor
51
*
52
* Does nothing, a later call to reinit is necessary
53
*/
54
NamedColumnsParser();
55
56
57
/** @brief Constructor
58
*
59
* Initialises the parser (mainly using "reinitMap", only "ignoreCase" and
60
* "lineDelim" are saved directly into member variables - "reinit"
61
* does the same).
62
*
63
* @param[in] def The line that describes (names) the entries
64
* @param[in] defDelim Delimiter for the entry names
65
* @param[in] lineDelim Delimiter used within data lines
66
* @param[in] chomp Whether the lines shall be trimmed (white spaces shall be removed)
67
* @param[in] ignoreCase Whether the case shall be ignored when parsing the definitions
68
*/
69
NamedColumnsParser(const std::string& def, const std::string& defDelim = ";",
70
const std::string& lineDelim = ";", bool chomp = false,
71
bool ignoreCase = true);
72
73
74
/// @brief Destructor
75
~NamedColumnsParser();
76
77
78
/** @brief Reinitialises the parser
79
*
80
* Initialises the parser (mainly using "reinitMap", only "ignoreCase" and
81
* "lineDelim" are saved directly into member variables
82
*
83
* @param[in] def The line that describes (names) the entries
84
* @param[in] defDelim Delimiter for the entry names
85
* @param[in] lineDelim Delimiter used within data lines
86
* @param[in] chomp Whether the lines shall be trimmed (white spaces shall be removed)
87
* @param[in] ignoreCase Whether the case shall be ignored when parsing the definitions
88
*/
89
void reinit(const std::string& def, const std::string& defDelim = ";",
90
const std::string& lineDelim = ";", bool chomp = false,
91
bool ignoreCase = true);
92
93
94
/** @brief Parses the contents of the line
95
*
96
* Parses the line by tokenizing it using a StringTokenizer and the set
97
* line delimiter ("myLineDelimiter"). Stores the tokenized line into
98
* "myLineParser"
99
*
100
* @param[in] line The line to parse
101
*/
102
void parseLine(const std::string& line);
103
104
105
/** @brief Returns the named information
106
*
107
* Tries to find the given variable name within the parsed definition line
108
* ("myDefinitionsMap"). If the value was not within the definition, an
109
* UnknownElement exception is thrown. Otherwise, the method tries to return
110
* the element from the parsed value line that is at the obtained position.
111
* If the value line had less tokens than the position assumes, an OutOfBoundsException
112
* is thrown, otherwise the value at the position returned (optionally prunned).
113
*
114
* @param[in] name The name of the value to retrieve
115
* @param[in] prune Whether the returned value shall be trimmed (leading/trainling spaces removed)
116
* @return The obtained value
117
* @exception UnknownElement when the element was not named during the initialisation
118
* @exception OutOfBoundsException when the line was too short and did not contain the item */
119
std::string get(const std::string& name,
120
bool prune = false) const;
121
122
123
/** @brief Returns the information whether the named column is known
124
*
125
* @param[in] name The name of the value to check
126
* @return Whether the named value is stored in the parsed line
127
*/
128
bool know(const std::string& name) const;
129
130
131
/** @brief Returns whether the number of named columns matches the actual number
132
*
133
* @return Whether the number of named columns matches the actual number
134
*/
135
bool hasFullDefinition() const;
136
137
138
private:
139
/** @brief Rebuilds the map of attribute names to their positions in a table
140
*
141
* The given definition string is split using the given delimiter. The obtained
142
* tokens are stired in "" together with their positions within the tokenized
143
* string.
144
* If wished (myAmCaseInsensitive==true), the definition string is converted
145
* into lower case, first. Also, if chomp==true, each token ist prunned.
146
*
147
* @param[in] def The definition string
148
* @param[in] delim The delimiter string
149
* @param[in] chomp Whether the tokens shall be prunned
150
*/
151
void reinitMap(std::string def, const std::string& delim = ";",
152
bool chomp = false);
153
154
155
/** @brief Prunes the given string if it shall be done
156
*
157
* If prune==true, the given string is prunned (all leading/trailing spaces
158
* are removed).
159
*
160
* @param[in, out] str The string to prune (optionally)
161
* @param[in] prune Whether the string shall be prunned
162
*/
163
void checkPrune(std::string& str, bool prune) const;
164
165
166
private:
167
/** @brief The map's definition of column item names to their positions within the table */
168
typedef std::map<std::string, int> PosMap;
169
170
/// @brief The map of column item names to their positions within the table
171
PosMap myDefinitionsMap;
172
173
/// @brief The delimiter to split the column items on
174
std::string myLineDelimiter;
175
176
/// @brief The contents of the current line
177
StringTokenizer myLineParser;
178
179
/// @brief Information whether case insensitive match shall be done
180
bool myAmCaseInsensitive;
181
182
};
183
184