Getting rid of third party dependency for boost_program_options

This commit is contained in:
Dennis Luxen 2011-03-11 16:52:30 +00:00
parent 6a3fe6edeb
commit a2b096b096
2 changed files with 108 additions and 44 deletions

View File

@ -1,8 +1,21 @@
/* /*
* ServerConfiguration.h open source routing machine
* Copyright (C) Dennis Luxen, 2010
* Created on: 26.11.2010
* Author: dennis 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_ #ifndef SERVERCONFIGURATION_H_

View File

@ -1,8 +1,21 @@
/* /*
* BaseConfiguration.h open source routing machine
* Copyright (C) Dennis Luxen, 2010
* Created on: 26.11.2010
* Author: dennis 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_ #ifndef BASECONFIGURATION_H_
@ -12,57 +25,95 @@
#include <string> #include <string>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <string>
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <boost/config.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
#include "../DataStructures/HashTable.h" #include "../DataStructures/HashTable.h"
class BaseConfiguration { class BaseConfiguration {
public: public:
BaseConfiguration(const char * configFile) { BaseConfiguration(const char * configFile) {
std::ifstream config( configFile ); std::ifstream config( configFile );
if(!config) { if(!config) {
std::cerr << "[config] .ini not found" << std::endl; std::cerr << "[config] .ini not found" << std::endl;
return; return;
} }
//parameters std::string line;
options.insert("*"); try {
if (config.is_open()) {
while ( config.good() ) {
getline (config,line);
std::vector<std::string> 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: " <<e.what() << std::endl;
}
}
std::string GetParameter(const char * key){ // for (boost::program_options::detail::config_file_iterator i(config, options), e ; i != e; ++i) {
return GetParameter(std::string(key)); // 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: " <<e.what() << std::endl;
if(config.is_open())
config.close();
}
}
std::string GetParameter(std::string key) { std::string GetParameter(const char * key){
return parameters.Find(key); return GetParameter(std::string(key));
} }
void SetParameter(const char* key, const char* value) { std::string GetParameter(std::string key) {
SetParameter(std::string(key), std::string(value)); return parameters.Find(key);
} }
void SetParameter(std::string key, std::string value) { void SetParameter(const char* key, const char* value) {
parameters.Set(key, value); SetParameter(std::string(key), std::string(value));
} }
void SetParameter(std::string key, std::string value) {
parameters.Set(key, value);
}
private: private:
std::set<std::string> options; void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = "=") {
HashTable<std::string, std::string> parameters; std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
//Speichert alle Einträge aus INI Datei 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<std::string, std::string> parameters;
//Speichert alle Einträge aus INI Datei
}; };
#endif /* BASECONFIGURATION_H_ */ #endif /* BASECONFIGURATION_H_ */