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-2017 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 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43 
44 
46  const std::string& defDelim,
47  const std::string& lineDelim,
48  bool prune, bool ignoreCase)
49  : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
50  reinitMap(def, defDelim, prune);
51 }
52 
53 
55 
56 
57 void
58 NamedColumnsParser::reinit(const std::string& def,
59  const std::string& defDelim,
60  const std::string& lineDelim,
61  bool prune, bool ignoreCase) {
62  myAmCaseInsensitive = ignoreCase;
63  reinitMap(def, defDelim, prune);
64  myLineDelimiter = lineDelim;
65 }
66 
67 
68 void
69 NamedColumnsParser::parseLine(const std::string& line) {
71 }
72 
73 
74 std::string
75 NamedColumnsParser::get(const std::string& name, bool prune) const {
76  PosMap::const_iterator i = myDefinitionsMap.find(name);
77  if (i == myDefinitionsMap.end()) {
78  if (myAmCaseInsensitive) {
80  }
81  if (i == myDefinitionsMap.end()) {
82  throw UnknownElement(name);
83  }
84  }
85  int pos = (*i).second;
86  if (myLineParser.size() <= pos) {
87  throw OutOfBoundsException();
88  }
89  std::string ret = myLineParser.get(pos);
90  checkPrune(ret, prune);
91  return ret;
92 }
93 
94 
95 bool
96 NamedColumnsParser::know(const std::string& name) const {
97  PosMap::const_iterator i = myDefinitionsMap.find(name);
98  if (i == myDefinitionsMap.end()) {
99  if (myAmCaseInsensitive) {
101  }
102  }
103  if (i == myDefinitionsMap.end()) {
104  return false;
105  }
106  int pos = (*i).second;
107  return myLineParser.size() > pos;
108 }
109 
110 
111 bool
113  return (int)myDefinitionsMap.size() == myLineParser.size();
114 }
115 
116 
117 void
119  const std::string& delim,
120  bool prune) {
121  if (myAmCaseInsensitive) {
123  }
124  myDefinitionsMap.clear();
125  int pos = 0;
126  StringTokenizer st(s, delim);
127  while (st.hasNext()) {
128  std::string next = st.next();
129  checkPrune(next, prune);
130  myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
131  }
132 }
133 
134 
135 void
136 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
137  if (!prune) {
138  return;
139  }
140  std::string::size_type idx = str.find_first_not_of(" ");
141  if (idx != std::string::npos) {
142  str = str.substr(idx);
143  }
144  idx = str.find_last_not_of(" ");
145  if (idx != std::string::npos && idx != str.length() - 1) {
146  str = str.substr(0, idx + 1);
147  }
148 }
149 
150 
151 
152 /****************************************************************************/
153 
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:63
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.