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

80 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
#include <string_view>
#include <string>
2019-01-22 15:13:26 +01:00
#include <functional>
#include <memory>
#include <unordered_map>
#include <vector>
#include <llarp/util/file.hpp>
2017-10-03 21:14:46 +02:00
2019-01-22 15:13:26 +01:00
namespace llarp
{
2019-01-22 15:13:26 +01: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 15:13:26 +01:00
/// clear parser
void
Clear();
2019-01-22 15:13:26 +01:00
/// load config file for bootserv
/// return true on success
/// return false on error
bool
LoadFile(const fs::path& fname);
/// load new .ini file from string (calls ParseAll() rather than Parse())
/// return true on success
/// return false on error
bool
LoadNewFromStr(std::string_view str);
2019-01-22 15:13:26 +01:00
/// load from string
/// return true on success
/// return false on error
bool
LoadFromStr(std::string_view str);
2019-01-22 15:13:26 +01:00
/// iterate all sections and thier values
void
IterAll(std::function<void(std::string_view, const SectionValues_t&)> visit);
2019-01-22 15:13:26 +01: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;
/// add a config option that is appended in another file
2020-08-27 14:43:53 +02:00
void
AddOverride(fs::path file, std::string section, std::string key, std::string value);
2020-08-27 14:43:53 +02:00
/// save config overrides
2020-08-27 14:43:53 +02:00
void
Save();
2020-08-27 14:43:53 +02:00
/// save new .ini config file to path
void
SaveNew() const;
2022-06-27 19:14:03 +02:00
inline void
Filename(fs::path f)
{
m_FileName = f;
};
private:
bool
ParseAll();
2019-01-22 15:13:26 +01:00
bool
Parse();
std::string m_Data;
2019-01-22 15:13:26 +01:00
Config_impl_t m_Config;
std::unordered_map<fs::path, Config_impl_t, util::FileHash> m_Overrides;
2020-06-08 14:42:10 +02:00
fs::path m_FileName;
};
2019-01-22 15:13:26 +01:00
} // namespace llarp