lokinet/llarp/config/ini.hpp

64 lines
1.5 KiB
C++
Raw Normal View History

2019-01-22 14:13:26 +00:00
#ifndef LOKINET_BOOTSERV_CONFIG_HPP
#define LOKINET_BOOTSERV_CONFIG_HPP
#include <string_view>
#include <string>
2019-01-22 14:13:26 +00:00
#include <functional>
#include <memory>
#include <unordered_map>
#include <vector>
2020-06-08 12:42:10 +00:00
#include <util/fs.hpp>
2017-10-03 19:14:46 +00:00
2019-01-22 14:13:26 +00:00
namespace llarp
{
2019-01-22 14:13:26 +00:00
struct ConfigParser
{
using SectionValues_t = std::unordered_multimap<std::string, std::string>;
using Config_impl_t = std::unordered_map<std::string, SectionValues_t>;
2019-01-22 14:13:26 +00:00
/// clear parser
void
Clear();
2019-01-22 14:13:26 +00:00
/// load config file for bootserv
/// return true on success
/// return false on error
bool
2020-06-08 12:42:10 +00:00
LoadFile(const fs::path fname);
2019-01-22 14:13:26 +00:00
/// load from string
/// return true on success
/// return false on error
bool
LoadFromStr(std::string_view str);
2019-01-22 14:13:26 +00:00
/// iterate all sections and thier values
void
IterAll(std::function<void(std::string_view, const SectionValues_t&)> visit);
2019-01-22 14:13:26 +00:00
/// visit a section in config read only by name
/// return false if no section or value propagated from visitor
bool
VisitSection(const char* name, std::function<bool(const SectionValues_t&)> visit) const;
2020-08-27 12:43:53 +00:00
/// add a config option that is appended at the end of the config buffer with no comments
void
AddOverride(std::string section, std::string key, std::string value);
/// save config and any overrides to the file it was loaded from
void
Save();
2020-08-27 12:43:53 +00:00
private:
2019-01-22 14:13:26 +00:00
bool
Parse();
std::vector<char> m_Data;
2019-01-22 14:13:26 +00:00
Config_impl_t m_Config;
2020-08-27 12:43:53 +00:00
Config_impl_t m_Overrides;
2020-06-08 12:42:10 +00:00
fs::path m_FileName;
};
2019-01-22 14:13:26 +00:00
} // namespace llarp
2017-10-03 19:14:46 +00:00
2019-01-22 14:13:26 +00:00
#endif