Eclipse SUMO - Simulation of Urban MObility
NamedColumnsParser.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A parser to retrieve information from a table with known column
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <map>
26 #include <string>
29 #include "NamedColumnsParser.h"
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
36 
37 
39  const std::string& defDelim,
40  const std::string& lineDelim,
41  bool prune, bool ignoreCase)
42  : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
43  reinitMap(def, defDelim, prune);
44 }
45 
46 
48 
49 
50 void
51 NamedColumnsParser::reinit(const std::string& def,
52  const std::string& defDelim,
53  const std::string& lineDelim,
54  bool prune, bool ignoreCase) {
55  myAmCaseInsensitive = ignoreCase;
56  reinitMap(def, defDelim, prune);
57  myLineDelimiter = lineDelim;
58 }
59 
60 
61 void
62 NamedColumnsParser::parseLine(const std::string& line) {
64 }
65 
66 
67 std::string
68 NamedColumnsParser::get(const std::string& name, bool prune) const {
69  PosMap::const_iterator i = myDefinitionsMap.find(name);
70  if (i == myDefinitionsMap.end()) {
71  if (myAmCaseInsensitive) {
73  }
74  if (i == myDefinitionsMap.end()) {
75  throw UnknownElement(name);
76  }
77  }
78  int pos = (*i).second;
79  if (myLineParser.size() <= pos) {
80  throw OutOfBoundsException();
81  }
82  std::string ret = myLineParser.get(pos);
83  checkPrune(ret, prune);
84  return ret;
85 }
86 
87 
88 bool
89 NamedColumnsParser::know(const std::string& name) const {
90  PosMap::const_iterator i = myDefinitionsMap.find(name);
91  if (i == myDefinitionsMap.end()) {
92  if (myAmCaseInsensitive) {
94  }
95  }
96  if (i == myDefinitionsMap.end()) {
97  return false;
98  }
99  int pos = (*i).second;
100  return myLineParser.size() > pos;
101 }
102 
103 
104 bool
106  return (int)myDefinitionsMap.size() == myLineParser.size();
107 }
108 
109 
110 void
112  const std::string& delim,
113  bool prune) {
114  if (myAmCaseInsensitive) {
116  }
117  myDefinitionsMap.clear();
118  int pos = 0;
119  StringTokenizer st(s, delim);
120  while (st.hasNext()) {
121  std::string next = st.next();
122  checkPrune(next, prune);
123  myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
124  }
125 }
126 
127 
128 void
129 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
130  if (!prune) {
131  return;
132  }
133  std::string::size_type idx = str.find_first_not_of(" ");
134  if (idx != std::string::npos) {
135  str = str.substr(idx);
136  }
137  idx = str.find_last_not_of(" ");
138  if (idx != std::string::npos && idx != str.length() - 1) {
139  str = str.substr(0, idx + 1);
140  }
141 }
142 
143 
144 
145 /****************************************************************************/
146 
NamedColumnsParser::hasFullDefinition
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
Definition: NamedColumnsParser.cpp:105
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:95
StringUtils::to_lower_case
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:58
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:100
NamedColumnsParser::parseLine
void parseLine(const std::string &line)
Parses the contents of the line.
Definition: NamedColumnsParser.cpp:62
NamedColumnsParser::myLineDelimiter
std::string myLineDelimiter
The delimiter to split the column items on.
Definition: NamedColumnsParser.h:177
StringTokenizer::get
std::string get(int pos) const
returns the item at the given position
Definition: StringTokenizer.cpp:125
NamedColumnsParser::get
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
Definition: NamedColumnsParser.cpp:68
NamedColumnsParser.h
StringTokenizer
Definition: StringTokenizer.h:62
NamedColumnsParser::reinitMap
void reinitMap(std::string def, const std::string &delim=";", bool chomp=false)
Rebuilds the map of attribute names to their positions in a table.
Definition: NamedColumnsParser.cpp:111
OutOfBoundsException
Definition: UtilExceptions.h:122
UtilExceptions.h
NamedColumnsParser::reinit
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
Definition: NamedColumnsParser.cpp:51
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:138
NamedColumnsParser::know
bool know(const std::string &name) const
Returns the information whether the named column is known.
Definition: NamedColumnsParser.cpp:89
NamedColumnsParser::myAmCaseInsensitive
bool myAmCaseInsensitive
Information whether case insensitive match shall be done.
Definition: NamedColumnsParser.h:183
StringUtils.h
NamedColumnsParser::NamedColumnsParser
NamedColumnsParser()
Constructor.
Definition: NamedColumnsParser.cpp:35
UnknownElement
Definition: UtilExceptions.h:135
NamedColumnsParser::~NamedColumnsParser
~NamedColumnsParser()
Destructor.
Definition: NamedColumnsParser.cpp:47
NamedColumnsParser::myDefinitionsMap
PosMap myDefinitionsMap
The map of column item names to their positions within the table.
Definition: NamedColumnsParser.h:174
NamedColumnsParser::myLineParser
StringTokenizer myLineParser
The contents of the current line.
Definition: NamedColumnsParser.h:180
config.h
NamedColumnsParser::checkPrune
void checkPrune(std::string &str, bool prune) const
Prunes the given string if it shall be done.
Definition: NamedColumnsParser.cpp:129