You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/config.cpp

79 lines
1.9 KiB
C++

7 years ago
#include "config.hpp"
7 years ago
#include <llarp/config.h>
7 years ago
#include "ini.hpp"
7 years ago
#include "mem.hpp"
7 years ago
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;
7 years ago
}
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, "iwp-connect", section_t{});
netdb = find_section(top, "netdb", section_t{});
iwp_links = find_section(top, "iwp-links", section_t{});
return true;
}
return false;
};
7 years ago
7 years ago
} // namespace llarp
7 years ago
7 years ago
extern "C" {
7 years ago
void
llarp_new_config(struct llarp_config **conf)
{
7 years ago
llarp_config *c = new llarp_config;
*conf = c;
7 years ago
}
7 years ago
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
7 years ago
*conf = nullptr;
}
7 years ago
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
7 years ago
return 0;
}
7 years ago
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 = {
7 years ago
{"router", conf->impl.router},
{"network", conf->impl.network},
{"iwp-connect", conf->impl.connect},
6 years ago
{"iwp-links", conf->impl.iwp_links},
7 years ago
{"netdb", conf->impl.netdb}};
for(const auto section : sections)
for(const auto item : section.second)
7 years ago
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
7 years ago
}