62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#ifndef _CONFIGFILEMANAGER_H
|
|
#define _CONFIGFILEMANAGER_H
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include "definitions.h"
|
|
|
|
class configfilemanager{
|
|
private:
|
|
std::map<std::string, std::string> configs;
|
|
std::mutex mtx;
|
|
|
|
|
|
public:
|
|
configfilemanager(std::string filepath = "./ncsambawatcher.config")
|
|
{
|
|
std::ifstream is(filepath);
|
|
if(!is.good())
|
|
{
|
|
std::cerr << "File not exits: " << filepath << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
std::string tmp;
|
|
|
|
while(!is.eof())
|
|
{
|
|
std::getline(is, tmp);
|
|
std::cout << tmp << std::endl;
|
|
if (tmp.at(0) == '#') // ignore comments
|
|
continue;
|
|
|
|
std::vector<std::string> splited = splitString(tmp, '=');
|
|
if (splited.size() != 2)
|
|
{
|
|
std::cerr << "Invalid line: " << tmp << std::endl;
|
|
continue;
|
|
}
|
|
|
|
configs.insert(std::make_pair(splited.at(0), splited.at(1)));
|
|
}
|
|
|
|
std::cout << "Config file loaded successfuly" << std::endl;
|
|
}
|
|
|
|
std::string at(const std::string &config)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
return configs.at(config);
|
|
}
|
|
|
|
std::string at(const char* config)
|
|
{
|
|
return at(std::string(config));
|
|
}
|
|
|
|
};
|
|
|
|
#endif // _CONFIGFILEMANAGER_H
|