SUMO - Simulation of Urban MObility
NamedColumnsParser.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // A parser to retrieve information from a table with known column
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <map>
33 #include <string>
36 #include "NamedColumnsParser.h"
37 
38 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47 
48 
50  const std::string& defDelim,
51  const std::string& lineDelim,
52  bool prune, bool ignoreCase)
53  : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
54  reinitMap(def, defDelim, prune);
55 }
56 
57 
59 
60 
61 void
62 NamedColumnsParser::reinit(const std::string& def,
63  const std::string& defDelim,
64  const std::string& lineDelim,
65  bool prune, bool ignoreCase) {
66  myAmCaseInsensitive = ignoreCase;
67  reinitMap(def, defDelim, prune);
68  myLineDelimiter = lineDelim;
69 }
70 
71 
72 void
73 NamedColumnsParser::parseLine(const std::string& line) {
75 }
76 
77 
78 std::string
79 NamedColumnsParser::get(const std::string& name, bool prune) const {
80  PosMap::const_iterator i = myDefinitionsMap.find(name);
81  if (i == myDefinitionsMap.end()) {
82  if (myAmCaseInsensitive) {
84  }
85  if (i == myDefinitionsMap.end()) {
86  throw UnknownElement(name);
87  }
88  }
89  size_t pos = (*i).second;
90  if (myLineParser.size() <= pos) {
91  throw OutOfBoundsException();
92  }
93  std::string ret = myLineParser.get(pos);
94  checkPrune(ret, prune);
95  return ret;
96 }
97 
98 
99 bool
100 NamedColumnsParser::know(const std::string& name) const {
101  PosMap::const_iterator i = myDefinitionsMap.find(name);
102  if (i == myDefinitionsMap.end()) {
103  if (myAmCaseInsensitive) {
105  }
106  }
107  if (i == myDefinitionsMap.end()) {
108  return false;
109  }
110  size_t pos = (*i).second;
111  return myLineParser.size() > pos;
112 }
113 
114 
115 bool
117  return myDefinitionsMap.size() == myLineParser.size();
118 }
119 
120 
121 void
123  const std::string& delim,
124  bool prune) {
125  if (myAmCaseInsensitive) {
127  }
128  myDefinitionsMap.clear();
129  int pos = 0;
130  StringTokenizer st(s, delim);
131  while (st.hasNext()) {
132  std::string next = st.next();
133  checkPrune(next, prune);
134  myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
135  }
136 }
137 
138 
139 void
140 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
141  if (!prune) {
142  return;
143  }
144  size_t idx = str.find_first_not_of(" ");
145  if (idx != std::string::npos) {
146  str = str.substr(idx);
147  }
148  idx = str.find_last_not_of(" ");
149  if (idx != std::string::npos && idx != str.length() - 1) {
150  str = str.substr(0, idx + 1);
151  }
152 }
153 
154 
155 
156 /****************************************************************************/
157 
PosMap myDefinitionsMap
The map of column item names to their positions within the table.
std::string next()
std::string get(const std::string &name, bool prune=false) const
Returns the named information.
std::string get(size_t pos) const
void reinit(const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true)
Reinitialises the parser.
NamedColumnsParser()
Constructor.
size_t size() const
bool hasFullDefinition() const
Returns whether the number of named columns matches the actual number.
void checkPrune(std::string &str, bool prune) const
Prunes the given string if it shall be done.
bool know(const std::string &name) const
Returns the information whether the named column is known.
~NamedColumnsParser()
Destructor.
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:67
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.
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.