NamedColumnsParser Class Reference

#include <NamedColumnsParser.h>


Detailed Description

A parser to retrieve information from a table with known columns.

When initialised, this parser stores the given information about the order of the named elements and allows the retrieval of lines using the names of these elements. Use it like this:

Todo:
What happens if an uninitialised NamedColumnsParser is used? exceptions?

Definition at line 56 of file NamedColumnsParser.h.


Public Member Functions

std::string get (const std::string &name, bool prune=false) const throw (UnknownElement, OutOfBoundsException)
 Returns the named information.
bool know (const std::string &name) const throw ()
 Returns the information whether the named column is known.
 NamedColumnsParser (const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true) throw ()
 Constructor.
 NamedColumnsParser () throw ()
 Constructor.
void parseLine (const std::string &line) throw ()
 Parses the contents of the line.
void reinit (const std::string &def, const std::string &defDelim=";", const std::string &lineDelim=";", bool chomp=false, bool ignoreCase=true) throw ()
 Reinitialises the parser.
 ~NamedColumnsParser () throw ()
 Destructor.

Private Types

typedef std::map< std::string,
size_t
PosMap
 The map's definition of column item names to their positions within the table.

Private Member Functions

void checkPrune (std::string &str, bool prune) const throw ()
 Prunes the given string if it shall be done.
void reinitMap (std::string def, const std::string &delim=";", bool chomp=false) throw ()
 Rebuilds the map of attribute names to their positions in a table.

Private Attributes

bool myAmCaseInsensitive
 Information whether case insensitive match shall be done.
PosMap myDefinitionsMap
 The map of column item names to their positions within the table.
std::string myLineDelimiter
 The delimiter to split the column items on.
StringTokenizer myLineParser
 The contents of the current line.

Member Typedef Documentation

typedef std::map<std::string, size_t> NamedColumnsParser::PosMap [private]

The map's definition of column item names to their positions within the table.

Definition at line 169 of file NamedColumnsParser.h.


Constructor & Destructor Documentation

NamedColumnsParser::NamedColumnsParser (  )  throw ()

Constructor.

Does nothing, a later call to reinit is necessary

Definition at line 44 of file NamedColumnsParser.cpp.

00044 {}

NamedColumnsParser::NamedColumnsParser ( const std::string &  def,
const std::string &  defDelim = ";",
const std::string &  lineDelim = ";",
bool  chomp = false,
bool  ignoreCase = true 
) throw ()

Constructor.

Initialises the parser (mainly using "reinitMap", only "ignoreCase" and "lineDelim" are saved directly into member variables - "reinit" does the same).

Parameters:
[in] def The line that describes (names) the entries
[in] defDelim Delimiter for the entry names
[in] lineDelim Delimiter used within data lines
[in] chomp Whether the lines shall be trimmed (white spaces shall be removed)
[in] ignoreCase Whether the case shall be ignored when parsing the definitions

Definition at line 47 of file NamedColumnsParser.cpp.

References reinitMap().

00051         : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
00052     reinitMap(def, defDelim, prune);
00053 }

NamedColumnsParser::~NamedColumnsParser (  )  throw ()

Destructor.

Definition at line 56 of file NamedColumnsParser.cpp.

00056 {}


Member Function Documentation

void NamedColumnsParser::checkPrune ( std::string &  str,
bool  prune 
) const throw () [private]

Prunes the given string if it shall be done.

If prune==true, the given string is prunned (all leading/trailing spaces are removed).

Parameters:
[in,out] str The string to prune (optionally)
[in] prune Whether the string shall be prunned

Definition at line 132 of file NamedColumnsParser.cpp.

Referenced by get(), and reinitMap().

00132                                                                        {
00133     if (!prune) {
00134         return;
00135     }
00136     size_t idx = str.find_first_not_of(" ");
00137     if (idx!=std::string::npos) {
00138         str = str.substr(idx);
00139     }
00140     idx = str.find_last_not_of(" ");
00141     if (idx!=std::string::npos&&idx!=str.length()-1) {
00142         str = str.substr(0, idx+1);
00143     }
00144 }

std::string NamedColumnsParser::get ( const std::string &  name,
bool  prune = false 
) const throw (UnknownElement, OutOfBoundsException)

Returns the named information.

Tries to find the given variable name within the parsed definition line ("myDefinitionsMap"). If the value was not within the definition, an UnknownElement exception is thrown. Otherwise, the method tries to return the element from the parsed value line that is at the obtained position. If the value line had less tokens than the position assumes, an OutOfBoundsException is thrown, otherwise the value at the position returned (optionally prunned).

Parameters:
[in] name The name of the value to retrieve
[in] prune Whether the returned value shall be trimmed (leading/trainling spaces removed)
Returns:
The obtained value
Exceptions:
UnknownElement when the element was not named during the initialisation
OutOfBoundsException when the line was too short and did not contain the item

Definition at line 77 of file NamedColumnsParser.cpp.

References checkPrune(), StringTokenizer::get(), myAmCaseInsensitive, myDefinitionsMap, myLineParser, StringTokenizer::size(), and StringUtils::to_lower_case().

Referenced by NIImporter_VISUM::getNamedEdge(), NIImporter_VISUM::getNamedEdgeContinuating(), NIImporter_VISUM::getNamedFloat(), NIImporter_VISUM::getNamedNode(), NIImporter_VISUM::getNamedString(), NIImporter_VISUM::getWeightedBool(), NIImporter_VISUM::getWeightedFloat(), PCLoaderVisum::load(), NIImporter_VISUM::parse_AreaSubPartElement(), NIImporter_VISUM::parse_Connectors(), NIImporter_VISUM::parse_Districts(), NIImporter_VISUM::parse_EdgePolys(), NIImporter_VISUM::parse_Edges(), NIImporter_VISUM::parse_Kante(), NIImporter_VISUM::parse_Lanes(), NIImporter_VISUM::parse_LanesConnections(), NIImporter_VISUM::parse_Nodes(), NIImporter_VISUM::parse_NodesToTrafficLights(), NIImporter_VISUM::parse_PartOfArea(), NIImporter_VISUM::parse_Phases(), NIImporter_VISUM::parse_Point(), NIImporter_VISUM::parse_SignalGroups(), NIImporter_VISUM::parse_SignalGroupsToPhases(), NIImporter_VISUM::parse_TrafficLights(), NIImporter_VISUM::parse_Turns(), NIImporter_VISUM::parse_Types(), NIImporter_VISUM::parse_VSysTypes(), and RODFDetFlowLoader::read().

00077                                                                                                            {
00078     PosMap::const_iterator i = myDefinitionsMap.find(name);
00079     if (i==myDefinitionsMap.end()) {
00080         if (myAmCaseInsensitive) {
00081             i = myDefinitionsMap.find(StringUtils::to_lower_case(name));
00082         }
00083         if (i==myDefinitionsMap.end()) {
00084             throw UnknownElement(name);
00085         }
00086     }
00087     size_t pos = (*i).second;
00088     if (myLineParser.size()<=pos) {
00089         throw OutOfBoundsException();
00090     }
00091     std::string ret = myLineParser.get(pos);
00092     checkPrune(ret, prune);
00093     return ret;
00094 }

bool NamedColumnsParser::know ( const std::string &  name  )  const throw ()

Returns the information whether the named column is known.

Parameters:
[in] name The name of the value to check
Returns:
Whether the named value is stored in the parsed line

Definition at line 98 of file NamedColumnsParser.cpp.

References myAmCaseInsensitive, myDefinitionsMap, myLineParser, StringTokenizer::size(), and StringUtils::to_lower_case().

Referenced by NIImporter_VISUM::getNamedEdge(), NIImporter_VISUM::getNamedEdgeContinuating(), NIImporter_VISUM::getNamedFloat(), NIImporter_VISUM::getNamedNode(), NIImporter_VISUM::getNamedString(), NIImporter_VISUM::parse_Connectors(), NIImporter_VISUM::parse_Districts(), NIImporter_VISUM::parse_Edges(), NIImporter_VISUM::parse_TrafficLights(), NIImporter_VISUM::parse_Turns(), NIImporter_VISUM::parse_TurnsToSignalGroups(), NIImporter_VISUM::parse_VSysTypes(), and RODFDetFlowLoader::read().

00098                                                             {
00099     PosMap::const_iterator i = myDefinitionsMap.find(name);
00100     if (i==myDefinitionsMap.end()) {
00101         if (myAmCaseInsensitive) {
00102             i = myDefinitionsMap.find(StringUtils::to_lower_case(name));
00103         }
00104     }
00105     if (i==myDefinitionsMap.end()) {
00106         return false;
00107     }
00108     size_t pos = (*i).second;
00109     return myLineParser.size()>pos;
00110 }

void NamedColumnsParser::parseLine ( const std::string &  line  )  throw ()

Parses the contents of the line.

Parses the line by tokenizing it using a StringTokenizer and the set line delimiter ("myLineDelimiter"). Stores the tokenized line into "myLineParser"

Parameters:
[in] line The line to parse

Definition at line 71 of file NamedColumnsParser.cpp.

References myLineDelimiter, and myLineParser.

Referenced by PCLoaderVisum::load(), NIImporter_VISUM::load(), and RODFDetFlowLoader::read().

00071                                                            {
00072     myLineParser = StringTokenizer(line, myLineDelimiter);
00073 }

void NamedColumnsParser::reinit ( const std::string &  def,
const std::string &  defDelim = ";",
const std::string &  lineDelim = ";",
bool  chomp = false,
bool  ignoreCase = true 
) throw ()

Reinitialises the parser.

Initialises the parser (mainly using "reinitMap", only "ignoreCase" and "lineDelim" are saved directly into member variables

Parameters:
[in] def The line that describes (names) the entries
[in] defDelim Delimiter for the entry names
[in] lineDelim Delimiter used within data lines
[in] chomp Whether the lines shall be trimmed (white spaces shall be removed)
[in] ignoreCase Whether the case shall be ignored when parsing the definitions

Definition at line 60 of file NamedColumnsParser.cpp.

References myAmCaseInsensitive, myLineDelimiter, and reinitMap().

Referenced by PCLoaderVisum::load(), NIImporter_VISUM::load(), and RODFDetFlowLoader::read().

00063                                                                 {
00064     myAmCaseInsensitive = ignoreCase;
00065     reinitMap(def, defDelim, prune);
00066     myLineDelimiter = lineDelim;
00067 }

void NamedColumnsParser::reinitMap ( std::string  def,
const std::string &  delim = ";",
bool  chomp = false 
) throw () [private]

Rebuilds the map of attribute names to their positions in a table.

The given definition string is split using the given delimiter. The obtained tokens are stired in "" together with their positions within the tokenized string. If wished (myAmCaseInsensitive==true), the definition string is converted into lower case, first. Also, if chomp==true, each token ist prunned.

Parameters:
[in] def The definition string
[in] delim The delimiter string
[in] chomp Whether the tokens shall be prunned

Definition at line 114 of file NamedColumnsParser.cpp.

References checkPrune(), StringTokenizer::hasNext(), myAmCaseInsensitive, myDefinitionsMap, StringTokenizer::next(), and StringUtils::to_lower_case().

Referenced by NamedColumnsParser(), and reinit().

00116                                                   {
00117     if (myAmCaseInsensitive) {
00118         s = StringUtils::to_lower_case(s);
00119     }
00120     myDefinitionsMap.clear();
00121     int pos = 0;
00122     StringTokenizer st(s, delim);
00123     while (st.hasNext()) {
00124         std::string next = st.next();
00125         checkPrune(next, prune);
00126         myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
00127     }
00128 }


Field Documentation

Information whether case insensitive match shall be done.

Definition at line 181 of file NamedColumnsParser.h.

Referenced by get(), know(), reinit(), and reinitMap().

The map of column item names to their positions within the table.

Definition at line 172 of file NamedColumnsParser.h.

Referenced by get(), know(), and reinitMap().

std::string NamedColumnsParser::myLineDelimiter [private]

The delimiter to split the column items on.

Definition at line 175 of file NamedColumnsParser.h.

Referenced by parseLine(), and reinit().

The contents of the current line.

Definition at line 178 of file NamedColumnsParser.h.

Referenced by get(), know(), and parseLine().


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:50 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6