1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include "config.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
#include <string>
namespace pt = boost::property_tree;
using namespace std::string_literals;
namespace {
const std::string default_datapath {"/var/lib/blog"};
const std::string default_listen {"::1:9000"};
const std::string default_name{"blog"};
const std::string default_keywords{"blog, reichwein.it, weblog"};
}
Config::Config(const std::filesystem::path& config_filename):
m_dataPath{default_datapath},
m_listenAddress{"::1"},
m_listenPort{9000},
m_name{default_name},
m_keywords{default_keywords}
{
try {
pt::ptree tree;
pt::read_xml(config_filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace);
m_dataPath = tree.get<std::string>("config.datapath", default_datapath);
m_name = tree.get<std::string>("config.name", default_name);
std::string listen {tree.get<std::string>("config.port", default_listen)};
auto pos{listen.find_last_of(':')};
if (pos == std::string::npos)
throw std::runtime_error("Bad port address: "s + listen);
m_listenAddress = listen.substr(0, pos);
m_listenPort = std::stoi(listen.substr(pos + 1));
if (m_listenPort < 0 || m_listenPort > 65535)
throw std::runtime_error("Bad listen port: "s + std::to_string(m_listenPort));
m_keywords = tree.get<std::string>("config.keywords", default_keywords);
} catch (const std::exception& ex) {
std::cerr << "Error reading config file " << config_filename << ". Using defaults." << std::endl;
}
}
std::string Config::getDataPath() const
{
return m_dataPath;
}
std::string Config::getListenAddress() const
{
return m_listenAddress;
}
int Config::getListenPort() const
{
return m_listenPort;
}
std::string Config::getName() const
{
return m_name;
}
std::string Config::getKeywords() const
{
return m_keywords;
}
|