1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/config.cpp

81 lines
2 KiB
C++
Raw Normal View History

2017-11-28 15:05:31 +01:00
#include "config.hpp"
2018-02-01 14:21:00 +01:00
#include <llarp/config.h>
2017-10-03 21:14:46 +02:00
#include "ini.hpp"
2018-01-26 15:17:51 +01:00
#include "mem.hpp"
2017-10-03 21:14:46 +02:00
namespace llarp
{
template < typename Config, typename Section >
static const Section &
find_section(Config &c, const std::string &name, const Section &fallback)
{
if(c.sections.find(name) == c.sections.end())
return fallback;
return c.sections[name].values;
2018-01-29 15:27:24 +01: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{});
connect = find_section(top, "connect", section_t{});
netdb = find_section(top, "netdb", section_t{});
iwp_links = find_section(top, "bind", section_t{});
2018-07-09 19:32:11 +02:00
services = find_section(top, "services", section_t{});
return true;
}
return false;
};
2017-10-03 21:14:46 +02:00
2018-02-01 14:21:00 +01:00
} // namespace llarp
2017-10-03 21:14:46 +02:00
void
llarp_new_config(struct llarp_config **conf)
{
2018-01-29 15:27:24 +01:00
llarp_config *c = new llarp_config;
*conf = c;
2018-01-29 15:27:24 +01:00
}
2017-10-03 21:14:46 +02:00
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
2018-01-29 15:27:24 +01:00
*conf = nullptr;
}
2017-10-03 21:14:46 +02:00
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
2018-01-29 15:27:24 +01:00
return 0;
}
2018-01-08 14:49:05 +01:00
void
llarp_config_iter(struct llarp_config *conf, struct llarp_config_iterator *iter)
{
2018-07-17 06:37:50 +02:00
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
2018-01-08 14:49:05 +01:00
{"network", conf->impl.network},
{"connect", conf->impl.connect},
{"bind", conf->impl.iwp_links},
2018-07-09 19:32:11 +02:00
{"netdb", conf->impl.netdb},
{"services", conf->impl.services}};
2018-06-06 19:02:57 +02:00
for(const auto item : conf->impl.router)
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
for(const auto section : sections)
for(const auto item : section.second)
2018-01-29 15:27:24 +01:00
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}