2021-03-09 23:24:35 +01:00
|
|
|
#pragma once
|
2019-07-02 23:28:28 +02:00
|
|
|
|
2020-05-01 21:51:15 +02:00
|
|
|
#include <string_view>
|
2020-05-02 00:41:20 +02:00
|
|
|
#include <string>
|
2019-01-22 15:13:26 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2019-07-02 23:28:28 +02:00
|
|
|
#include <unordered_map>
|
2018-08-01 04:20:40 +02:00
|
|
|
#include <vector>
|
2021-03-09 23:24:35 +01:00
|
|
|
#include <llarp/util/fs.hpp>
|
2017-10-03 21:14:46 +02:00
|
|
|
|
2019-01-22 15:13:26 +01:00
|
|
|
namespace llarp
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2019-01-22 15:13:26 +01:00
|
|
|
struct ConfigParser
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2020-04-07 22:41:11 +02:00
|
|
|
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();
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2019-01-22 15:13:26 +01:00
|
|
|
/// load config file for bootserv
|
|
|
|
/// return true on success
|
|
|
|
/// return false on error
|
|
|
|
bool
|
2020-06-08 14:42:10 +02:00
|
|
|
LoadFile(const fs::path fname);
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2019-01-22 15:13:26 +01:00
|
|
|
/// load from string
|
|
|
|
/// return true on success
|
|
|
|
/// return false on error
|
|
|
|
bool
|
2020-05-01 21:51:15 +02:00
|
|
|
LoadFromStr(std::string_view str);
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2019-01-22 15:13:26 +01:00
|
|
|
/// iterate all sections and thier values
|
2018-08-01 04:20:40 +02:00
|
|
|
void
|
2020-05-01 21:51:15 +02:00
|
|
|
IterAll(std::function<void(std::string_view, const SectionValues_t&)> visit);
|
2018-08-01 04:20:40 +02:00
|
|
|
|
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
|
2018-08-01 04:20:40 +02:00
|
|
|
bool
|
2020-04-07 22:41:11 +02:00
|
|
|
VisitSection(const char* name, std::function<bool(const SectionValues_t&)> visit) const;
|
2020-03-13 21:45:33 +01:00
|
|
|
|
2020-10-21 14:58:08 +02:00
|
|
|
/// add a config option that is appended in another file
|
2020-08-27 14:43:53 +02:00
|
|
|
void
|
2020-10-21 14:58:08 +02:00
|
|
|
AddOverride(fs::path file, std::string section, std::string key, std::string value);
|
2020-08-27 14:43:53 +02:00
|
|
|
|
2020-10-21 14:58:08 +02:00
|
|
|
/// save config overrides
|
2020-08-27 14:43:53 +02:00
|
|
|
void
|
2020-09-29 15:26:45 +02:00
|
|
|
Save();
|
2020-08-27 14:43:53 +02:00
|
|
|
|
2022-06-27 19:14:03 +02:00
|
|
|
inline void
|
|
|
|
Filename(fs::path f)
|
|
|
|
{
|
|
|
|
m_FileName = f;
|
|
|
|
};
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
private:
|
2019-01-22 15:13:26 +01:00
|
|
|
bool
|
|
|
|
Parse();
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
std::vector<char> m_Data;
|
2019-01-22 15:13:26 +01:00
|
|
|
Config_impl_t m_Config;
|
2020-10-21 14:58:08 +02:00
|
|
|
std::unordered_map<fs::path, Config_impl_t, util::FileHash> m_Overrides;
|
2020-06-08 14:42:10 +02:00
|
|
|
fs::path m_FileName;
|
2018-05-22 17:54:19 +02:00
|
|
|
};
|
|
|
|
|
2019-01-22 15:13:26 +01:00
|
|
|
} // namespace llarp
|