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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // A parser to retrieve information from a table with known column
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <map>
32 #include <string>
35 #include "NamedColumnsParser.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42 
43 
45  const std::string& defDelim,
46  const std::string& lineDelim,
47  bool prune, bool ignoreCase)
48  : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
49  reinitMap(def, defDelim, prune);
50 }
51 
52 
54 
55 
56 void
57 NamedColumnsParser::reinit(const std::string& def,
58  const std::string& defDelim,
59  const std::string& lineDelim,
60  bool prune, bool ignoreCase) {
61  myAmCaseInsensitive = ignoreCase;
62  reinitMap(def, defDelim, prune);
63  myLineDelimiter = lineDelim;
64 }
65 
66 
67 void
68 NamedColumnsParser::parseLine(const std::string& line) {
70 }
71 
72 
73 std::string
74 NamedColumnsParser::get(const std::string& name, bool prune) const {
75  PosMap::const_iterator i = myDefinitionsMap.find(name);
76  if (i == myDefinitionsMap.end()) {
77  if (myAmCaseInsensitive) {
79  }
80  if (i == myDefinitionsMap.end()) {
81  throw UnknownElement(name);
82  }
83  }
84  int pos = (*i).second;
85  if (myLineParser.size() <= pos) {
86  throw OutOfBoundsException();
87  }
88  std::string ret = myLineParser.get(pos);
89  checkPrune(ret, prune);
90  return ret;
91 }
92 
93 
94 bool
95 NamedColumnsParser::know(const std::string& name) const {
96  PosMap::const_iterator i = myDefinitionsMap.find(name);
97  if (i == myDefinitionsMap.end()) {
98  if (myAmCaseInsensitive) {
100  }
101  }
102  if (i == myDefinitionsMap.end()) {
103  return false;
104  }
105  int pos = (*i).second;
106  return myLineParser.size() > pos;
107 }
108 
109 
110 bool
112  return (int)myDefinitionsMap.size() == myLineParser.size();
113 }
114 
115 
116 void
118  const std::string& delim,
119  bool prune) {
120  if (myAmCaseInsensitive) {
122  }
123  myDefinitionsMap.clear();
124  int pos = 0;
125  StringTokenizer st(s, delim);
126  while (st.hasNext()) {
127  std::string next = st.next();
128  checkPrune(next, prune);
129  myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
130  }
131 }
132 
133 
134 void
135 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
136  if (!prune) {
137  return;
138  }
139  std::string::size_type idx = str.find_first_not_of(" ");
140  if (idx != std::string::npos) {
141  str = str.substr(idx);
142  }
143  idx = str.find_last_not_of(" ");
144  if (idx != std::string::npos && idx != str.length() - 1) {
145  str = str.substr(0, idx + 1);
146  }
147 }
148 
149 
150 
151 /****************************************************************************/
152 
PosMap myDefinitionsMap
The map of column item names to their positions within the table.
std::string next()
std::string get(int pos) const
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
bool know(const std::string &name) const
Returns the information whether the named column is known.
NamedColumnsParser()
Constructor.
~NamedColumnsParser()
Destructor.
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:62
StringTokenizer myLineParser
The contents of the current line.
void reinitMap(std::string def, const std::string &delim=";", bool chomp=false)
Rebuilds the map of attribute names to their positions in a table.
void checkPrune(std::string &str, bool prune) const
Prunes the given string if it shall be done.
bool myAmCaseInsensitive
Information whether case insensitive match shall be done.
void parseLine(const std::string &line)
Parses the contents of the line.
std::string myLineDelimiter
The delimiter to split the column items on.