SUMO - Simulation of Urban MObility
NamedColumnsParser.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00008 // A parser to retrieve information from a table with known column
00009 /****************************************************************************/
00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00012 /****************************************************************************/
00013 //
00014 //   This file is part of SUMO.
00015 //   SUMO is free software: you can redistribute it and/or modify
00016 //   it under the terms of the GNU General Public License as published by
00017 //   the Free Software Foundation, either version 3 of the License, or
00018 //   (at your option) any later version.
00019 //
00020 /****************************************************************************/
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <map>
00033 #include <string>
00034 #include <utils/common/UtilExceptions.h>
00035 #include <utils/common/StringUtils.h>
00036 #include "NamedColumnsParser.h"
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ===========================================================================
00044 // method definitions
00045 // ===========================================================================
00046 NamedColumnsParser::NamedColumnsParser() {}
00047 
00048 
00049 NamedColumnsParser::NamedColumnsParser(const std::string& def,
00050                                        const std::string& defDelim,
00051                                        const std::string& lineDelim,
00052                                        bool prune, bool ignoreCase)
00053     : myLineDelimiter(lineDelim), myAmCaseInsensitive(ignoreCase) {
00054     reinitMap(def, defDelim, prune);
00055 }
00056 
00057 
00058 NamedColumnsParser::~NamedColumnsParser() {}
00059 
00060 
00061 void
00062 NamedColumnsParser::reinit(const std::string& def,
00063                            const std::string& defDelim,
00064                            const std::string& lineDelim,
00065                            bool prune, bool ignoreCase) {
00066     myAmCaseInsensitive = ignoreCase;
00067     reinitMap(def, defDelim, prune);
00068     myLineDelimiter = lineDelim;
00069 }
00070 
00071 
00072 void
00073 NamedColumnsParser::parseLine(const std::string& line) {
00074     myLineParser = StringTokenizer(line, myLineDelimiter);
00075 }
00076 
00077 
00078 std::string
00079 NamedColumnsParser::get(const std::string& name, bool prune) const throw(UnknownElement, OutOfBoundsException) {
00080     PosMap::const_iterator i = myDefinitionsMap.find(name);
00081     if (i == myDefinitionsMap.end()) {
00082         if (myAmCaseInsensitive) {
00083             i = myDefinitionsMap.find(StringUtils::to_lower_case(name));
00084         }
00085         if (i == myDefinitionsMap.end()) {
00086             throw UnknownElement(name);
00087         }
00088     }
00089     size_t pos = (*i).second;
00090     if (myLineParser.size() <= pos) {
00091         throw OutOfBoundsException();
00092     }
00093     std::string ret = myLineParser.get(pos);
00094     checkPrune(ret, prune);
00095     return ret;
00096 }
00097 
00098 
00099 bool
00100 NamedColumnsParser::know(const std::string& name) const {
00101     PosMap::const_iterator i = myDefinitionsMap.find(name);
00102     if (i == myDefinitionsMap.end()) {
00103         if (myAmCaseInsensitive) {
00104             i = myDefinitionsMap.find(StringUtils::to_lower_case(name));
00105         }
00106     }
00107     if (i == myDefinitionsMap.end()) {
00108         return false;
00109     }
00110     size_t pos = (*i).second;
00111     return myLineParser.size() > pos;
00112 }
00113 
00114 
00115 bool
00116 NamedColumnsParser::hasFullDefinition() const {
00117     return myDefinitionsMap.size() == myLineParser.size();
00118 }
00119 
00120 
00121 void
00122 NamedColumnsParser::reinitMap(std::string s,
00123                               const std::string& delim,
00124                               bool prune) {
00125     if (myAmCaseInsensitive) {
00126         s = StringUtils::to_lower_case(s);
00127     }
00128     myDefinitionsMap.clear();
00129     int pos = 0;
00130     StringTokenizer st(s, delim);
00131     while (st.hasNext()) {
00132         std::string next = st.next();
00133         checkPrune(next, prune);
00134         myDefinitionsMap.insert(std::map<std::string, int>::value_type(next, pos++));
00135     }
00136 }
00137 
00138 
00139 void
00140 NamedColumnsParser::checkPrune(std::string& str, bool prune) const {
00141     if (!prune) {
00142         return;
00143     }
00144     size_t idx = str.find_first_not_of(" ");
00145     if (idx != std::string::npos) {
00146         str = str.substr(idx);
00147     }
00148     idx = str.find_last_not_of(" ");
00149     if (idx != std::string::npos && idx != str.length() - 1) {
00150         str = str.substr(0, idx + 1);
00151     }
00152 }
00153 
00154 
00155 
00156 /****************************************************************************/
00157 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines