From a2b096b096ad49eff65cf2504cb735cad4205e29 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Fri, 11 Mar 2011 16:52:30 +0000 Subject: [PATCH] Getting rid of third party dependency for boost_program_options --- Server/ServerConfiguration.h | 21 ++++-- Util/BaseConfiguration.h | 131 ++++++++++++++++++++++++----------- 2 files changed, 108 insertions(+), 44 deletions(-) diff --git a/Server/ServerConfiguration.h b/Server/ServerConfiguration.h index e1237d58d..0393a7fe5 100644 --- a/Server/ServerConfiguration.h +++ b/Server/ServerConfiguration.h @@ -1,8 +1,21 @@ /* - * ServerConfiguration.h - * - * Created on: 26.11.2010 - * Author: dennis + 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 SERVERCONFIGURATION_H_ diff --git a/Util/BaseConfiguration.h b/Util/BaseConfiguration.h index 2db9d5438..d94c3e32f 100644 --- a/Util/BaseConfiguration.h +++ b/Util/BaseConfiguration.h @@ -1,8 +1,21 @@ /* - * BaseConfiguration.h - * - * Created on: 26.11.2010 - * Author: dennis + 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 BASECONFIGURATION_H_ @@ -12,57 +25,95 @@ #include #include #include +#include #include #include -#include -#include -#include - #include "../DataStructures/HashTable.h" class BaseConfiguration { public: - BaseConfiguration(const char * configFile) { - std::ifstream config( configFile ); - if(!config) { - std::cerr << "[config] .ini not found" << std::endl; - return; - } + BaseConfiguration(const char * configFile) { + std::ifstream config( configFile ); + if(!config) { + std::cerr << "[config] .ini not found" << std::endl; + return; + } - //parameters - options.insert("*"); + std::string line; + try { + if (config.is_open()) { + while ( config.good() ) { + getline (config,line); + std::vector tokens; + Tokenize(line, tokens); + if(tokens.size() != 2) + continue; + else + parameters.Add(tokens[0], tokens[1]); + } + config.close(); + } - try { - for (boost::program_options::detail::config_file_iterator i(config, options), e ; i != e; ++i) { - std::cout << "[config] " << i->string_key << " = " << i->value[0] << std::endl; - parameters.Add(i->string_key, i->value[0]); - } - } catch(std::exception& e) { - std::cerr << "[config] .ini not found -> Exception: " <string_key << " = " << i->value[0] << std::endl; +// parameters.Add(i->string_key, i->value[0]); +// } + } catch(std::exception& e) { + std::cerr << "[config] .ini not found -> Exception: " < options; - HashTable parameters; - //Speichert alle Einträge aus INI Datei + 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); + TrimStringRight(temp); + TrimStringLeft(temp); + tokens.push_back( temp ); + lastPos = str.find_first_not_of(delimiters, pos); + pos = str.find_first_of(delimiters, lastPos); + } + } + void TrimStringRight(std::string& str) { + std::string::size_type pos = str.find_last_not_of(" "); + if (pos != std::string::npos) + str.erase(pos+1); + else + str.erase( str.begin() , str.end() ); + } + void TrimStringLeft(std::string& str) { + std::string::size_type pos = str.find_first_not_of(" "); + if (pos != std::string::npos) + str.erase(0, pos); + else + str.erase( str.begin() , str.end() ); + } + + HashTable parameters; + //Speichert alle Einträge aus INI Datei }; #endif /* BASECONFIGURATION_H_ */