aboutsummaryrefslogtreecommitdiff
path: root/src/settings.cc
blob: f8bc8b56cb4de4174008c194f2870f09eb84ea41 (plain)
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
#include "settings.h"
#include <map>
#include <cstdlib>
#include <regex>
#include <stdexcept>
#include <confuse.h>
#include <errno.h>
#include <cstring>

namespace settings {
    std::string getString(const std::string& key) {
        cfg_opt_t opts[] = {
            CFG_STR("weapons", "/usr/share/dmtool/weapons/", CFGF_NONE),
            CFG_STR("armor", "/usr/share/dmtool/armor/", CFGF_NONE),
            CFG_STR("spells", "/usr/share/dmtool/spells/", CFGF_NONE),
            CFG_STR("creatures", "/usr/share/dmtool/creatures/", CFGF_NONE),
            CFG_STR("savedir", NULL, CFGF_NONE),
            CFG_STR("editor", "vi", CFGF_NONE),
            CFG_END()
        };
        cfg_t *cfg = cfg_init(opts, CFGF_IGNORE_UNKNOWN);
        if(cfg_parse(cfg, "/etc/dmtool.conf") == CFG_PARSE_ERROR) {
            throw std::runtime_error("Configuration file /etc/dmtool.conf could not be read: " + std::string(strerror(errno)));
        }
        try {
            std::string result(cfg_getstr(cfg, key.c_str()));
            cfg_free(cfg);
            return result;
        } catch(std::exception& e) {
            throw std::runtime_error("Cannot find key in configuration file: \"" + key + "\"");
        }
    }
    
    const std::vector<std::string> objectTypes {"weapons", "armor", "spells"};
}