lokinet/llarp/config.cpp

63 lines
1.7 KiB
C++
Raw Normal View History

2017-11-28 14:05:31 +00:00
#include "config.hpp"
2018-02-01 13:21:00 +00:00
#include <llarp/config.h>
2017-10-03 19:14:46 +00:00
#include "ini.hpp"
2018-01-26 14:17:51 +00:00
#include "mem.hpp"
2017-10-03 19:14:46 +00:00
2018-01-29 14:27:24 +00:00
namespace llarp {
2018-01-08 13:49:05 +00:00
2018-01-29 14:27:24 +00:00
template <typename Config, typename Section>
static Section find_section(Config &c, const std::string &name,
const Section &fallback) {
2018-02-01 13:21:00 +00:00
if (c.sections.find(name) == c.sections.end()) return fallback;
2018-01-29 14:27:24 +00:00
return c.sections[name].values;
2017-10-03 19:14:46 +00:00
}
2018-01-29 14:27:24 +00:00
bool Config::Load(const char *fname) {
std::ifstream f;
f.open(fname);
if (f.is_open()) {
ini::Parser parser(f);
auto &top = parser.top();
router = find_section(top, "router", section_t{});
network = find_section(top, "network", section_t{});
netdb = find_section(top, "netdb", section_t{});
links = find_section(top, "links", section_t{});
return true;
}
return false;
};
2017-10-03 19:14:46 +00:00
2018-02-01 13:21:00 +00:00
} // namespace llarp
2017-10-03 19:14:46 +00:00
2018-01-29 14:27:24 +00:00
extern "C" {
2018-01-08 13:49:05 +00:00
2018-01-29 14:27:24 +00:00
void llarp_new_config(struct llarp_config **conf) {
llarp_config *c = new llarp_config;
*conf = c;
}
2017-10-03 19:14:46 +00:00
2018-01-29 14:27:24 +00:00
void llarp_free_config(struct llarp_config **conf) {
2018-02-01 13:21:00 +00:00
if (*conf) delete *conf;
2018-01-29 14:27:24 +00:00
*conf = nullptr;
}
2017-10-03 19:14:46 +00:00
2018-01-29 14:27:24 +00:00
int llarp_load_config(struct llarp_config *conf, const char *fname) {
2018-02-01 13:21:00 +00:00
if (!conf->impl.Load(fname)) return -1;
2018-01-29 14:27:24 +00:00
return 0;
}
2018-01-08 13:49:05 +00:00
2018-01-29 14:27:24 +00:00
void llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter) {
iter->conf = conf;
std::map<std::string, llarp::Config::section_t &> sections = {
2018-01-08 13:49:05 +00:00
{"router", conf->impl.router},
{"network", conf->impl.network},
2018-01-19 16:51:27 +00:00
{"links", conf->impl.links},
2018-01-29 14:27:24 +00:00
{"netdb", conf->impl.netdb}};
for (const auto &section : sections)
for (const auto &item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
2017-10-03 19:14:46 +00:00
}