Commit 85b923e9 authored by Geithner, Thomas's avatar Geithner, Thomas Committed by Mall, Anon
Browse files

added config class

parent da0434a6
......@@ -18,10 +18,12 @@ OBJS=\
cmd-frontend.o \
coap-frontend.o \
avahi-service.o \
config.o \
TARGETS=\
desk-main \
avahi-test \
config-test \
#avahi-test \
all: $(TARGETS)
......
/*
* config-test.cpp
*
* Created on: 11.10.2017
* Author: geith
*/
#include "config.h"
#include <iostream>
#include <unistd.h>
using namespace std;
using namespace desk;
int main(int argc, char** argv){
Config conf("test.conf");
try{
cout << "key_1 -> " << conf.get("key_1") << endl;
} catch(ConfigException& e){
cout << e.what() << endl;
}
cout << "key_2 -> " << conf.get("key_2", "default_value") << endl;
cout << "key_3 -> " << conf.get("key_3", "default_value") << endl;
cout << "key_4 -> " << conf.get("key_4", "default_value") << endl;
conf.set("key_4", "val_4");
return 0;
}
/*
* config.cpp
*
* Created on: 11.10.2017
* Author: geith
*/
#include "config.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
using namespace desk;
Config::Config(const string file):
m_configFile(file)
{
readFile();
}
void
Config::readFile()
{
ifstream in(m_configFile);
if(in.fail()){
// could not open config file
return;
}
string line;
while(!in.eof() && !in.fail()){
getline(in, line);
cout << __func__ << ": got line \"" << line << "\"" << endl;
if(line.empty()){
continue;
}
istringstream l_in(line);
string key;
string value;
l_in >> key >> value;
cout << __func__ << ": key=" << key << ", value=" << value << endl;
m_config[key] = value;
}
in.close();
}
void
Config::writeFile() throw(ConfigException)
{
ofstream out(m_configFile);
if(out.fail()){
// could not open config file
throw ConfigException("could not write file");
}
for(auto c: m_config){
out << c.first << " " << c.second << endl;
}
out.close();
}
std::string
Config::get(const std::string& key) throw(ConfigException)
{
try{
return m_config.at(key);
} catch(out_of_range& e){
throw ConfigException("key \"" + key + "\" not found");
}
}
std::string
Config::get(const std::string& key, const std::string& default_value)
{
try{
return m_config.at(key);
} catch(out_of_range& e){
return default_value;
}
}
void
Config::set(const std::string& key, const std::string& value)
{
m_config[key] = value;
writeFile();
}
/*
* config.h
*
* Created on: 11.10.2017
* Author: geith
*/
#ifndef CONFIG_H_
#define CONFIG_H_
#include <string>
#include <map>
namespace desk{
class ConfigException: public std::exception{
public:
ConfigException(std::string msg) _GLIBCXX_USE_NOEXCEPT
: m_msg(msg){ }
virtual const char* what()
const _GLIBCXX_USE_NOEXCEPT{
return m_msg.c_str();
}
protected:
std::string m_msg;
};
class Config{
public:
Config(const std::string file);
std::string get(const std::string& key) throw(ConfigException);
std::string get(const std::string& key, const std::string& default_value);
void set(const std::string& key, const std::string& value);
protected:
void readFile();
void writeFile() throw(ConfigException);
const std::string m_configFile;
std::map<std::string,std::string> m_config;
};
}
#endif /* CONFIG_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment