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 /****************************************************************************/
15 // A parser to retrieve information from a table with known column
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <map>
25 #include <string>
28 #include "NamedColumnsParser.h"
29 
30 
31 // ===========================================================================
32 // method definitions
33 // ===========================================================================
35 
36 
38  const std::string& defDelim,
39  const std::string& lineDelim,
40  bool prune, bool ignoreCase)
41  : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
42  reinitMap(def, defDelim, prune);
43 }
44 
45 
47 
48 
49 void
50 NamedColumnsParser::reinit(const std::string& def,
51  const std::string& defDelim,
52  const std::string& lineDelim,
53  bool prune, bool ignoreCase) {
54  myAmCaseInsensitive = ignoreCase;
55  reinitMap(def, defDelim, prune);
56  myLineDelimiter = lineDelim;
57 }
58 
59 
60 void
61 NamedColumnsParser::parseLine(const std::string& line) {
63 }
64 
65 
66 std::string
67 NamedColumnsParser::get(const std::string& name, bool prune) const {
68  PosMap::const_iterator i = myDefinitionsMap.find(name);
69  if (i == myDefinitionsMap.end()) {
70  if (myAmCaseInsensitive) {
72  }
73  if (i == myDefinitionsMap.end()) {
74  throw UnknownElement(name);
75  }
76  }
77  int pos = (*i).second;
78  if (myLineParser.size() <= pos) {
79  throw OutOfBoundsException();
80  }
81  std::string ret = myLineParser.get(pos);
82  checkPrune(ret, prune);
83  return ret;
84 }
85 
86 
87 bool
88 NamedColumnsParser::know(const std::string& name) const {
89  PosMap::const_iterator i = myDefinitionsMap.find(name);
90  if (i == myDefinitionsMap.end()) {
91  if (myAmCaseInsensitive) {
93  }
94  }
95  if (i == myDefinitionsMap.end()) {
96  return false;
97  }
98  int pos = (*i).second;
99  return myLineParser.size() > pos;
100 }
101 
102 
103 bool
105  return (int)myDefinitionsMap.size() == myLineParser.size();
106 }
107 
108 
109 void
111  const std::string& delim,
112  bool prune) {
113  if (myAmCaseInsensitive) {
115  }
116  myDefinitionsMap.clear();
117  int pos = 0;
118  StringTokenizer st(s, delim);
119  while (st.hasNext()) {
120  std::string next = st.next();
121  checkPrune(next, prune);
122  myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
123  }
124 }
125 
126 
127 void
128 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
129  if (!prune) {
130  return;
131  }
132  std::string::size_type idx = str.find_first_not_of(" ");
133  if (idx != std::string::npos) {
134  str = str.substr(idx);
135  }
136  idx = str.find_last_not_of(" ");
137  if (idx != std::string::npos && idx != str.length() - 1) {
138  str = str.substr(0, idx + 1);
139  }
140 }
141 
142 
143 
144 /****************************************************************************/
145 
NamedColumnsParser::hasFullDefinition
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
Definition: NamedColumnsParser.cpp:104
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:94
StringUtils::to_lower_case
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:59
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:99
NamedColumnsParser::parseLine
void parseLine(const std::string &line)
Parses the contents of the line.
Definition: NamedColumnsParser.cpp:61
NamedColumnsParser::myLineDelimiter
std::string myLineDelimiter
The delimiter to split the column items on.
Definition: NamedColumnsParser.h:176
StringTokenizer::get
std::string get(int pos) const
returns the item at the given position
Definition: StringTokenizer.cpp:124
NamedColumnsParser::get
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
Definition: NamedColumnsParser.cpp:67
NamedColumnsParser.h
StringTokenizer
Definition: StringTokenizer.h:61
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:110
OutOfBoundsException
Definition: UtilExceptions.h:134
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:50
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:137
NamedColumnsParser::know
bool know(const std::string &name) const
Returns the information whether the named column is known.
Definition: NamedColumnsParser.cpp:88
NamedColumnsParser::myAmCaseInsensitive
bool myAmCaseInsensitive
Information whether case insensitive match shall be done.
Definition: NamedColumnsParser.h:182
StringUtils.h
NamedColumnsParser::NamedColumnsParser
NamedColumnsParser()
Constructor.
Definition: NamedColumnsParser.cpp:34
UnknownElement
Definition: UtilExceptions.h:147
NamedColumnsParser::~NamedColumnsParser
~NamedColumnsParser()
Destructor.
Definition: NamedColumnsParser.cpp:46
NamedColumnsParser::myDefinitionsMap
PosMap myDefinitionsMap
The map of column item names to their positions within the table.
Definition: NamedColumnsParser.h:173
NamedColumnsParser::myLineParser
StringTokenizer myLineParser
The contents of the current line.
Definition: NamedColumnsParser.h:179
config.h
NamedColumnsParser::checkPrune
void checkPrune(std::string &str, bool prune) const
Prunes the given string if it shall be done.
Definition: NamedColumnsParser.cpp:128