From a82383d78e0e224de20e61b2055f3151d682399c Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Tue, 6 Aug 2013 17:33:26 +0200 Subject: [PATCH] Refactoring base config class into proper ini file name --- Util/IniFile.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/Util/IniFile.h b/Util/IniFile.h index e69de29bb..ec01ade91 100644 --- a/Util/IniFile.h +++ b/Util/IniFile.h @@ -0,0 +1,88 @@ +/* + open source routing machine + Copyright (C) Dennis Luxen, 2010 + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU AFFERO General Public License as published by +the Free Software Foundation; either version 3 of the License, or +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +or see http://www.gnu.org/licenses/agpl.txt. + */ + +#ifndef INI_FILE_H_ +#define INI_FILE_H_ + +#include "OSRMException.h" +#include "../DataStructures/HashTable.h" + +#include + +#include +#include +#include +#include + +class IniFile { +public: + IniFile(const char * configFile) { + std::ifstream config( configFile ); + if(!config) { + throw OSRMException("[config] .ini not found"); + } + + std::string line; + if (config.is_open()) { + while ( config.good() ) { + getline (config,line); + std::vector tokens; + Tokenize(line, tokens); + if(2 == tokens.size() ) + parameters.insert(std::make_pair(tokens[0], tokens[1])); + } + config.close(); + } + } + + std::string GetParameter(const std::string & key){ + return parameters.Find(key); + } + + void SetParameter(const char* key, const char* value) { + SetParameter(std::string(key), std::string(value)); + } + + void SetParameter(const std::string key, std::string value) { + parameters[key] = value; + } + +private: + void Tokenize( + const std::string& str, + std::vector& tokens, + const std::string& delimiters = "=" + ) { + std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); + std::string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) { + std::string temp = str.substr(lastPos, pos - lastPos); + boost::trim(temp); + tokens.push_back( temp ); + lastPos = str.find_first_not_of(delimiters, pos); + pos = str.find_first_of(delimiters, lastPos); + } + } + + HashTable parameters; +}; + +#endif /* INI_FILE_H_ */ \ No newline at end of file