32 auto path = std::filesystem::path(file);
37 if (!std::filesystem::exists(path))
40 if (result != std::nullopt)
50 m_config = nlohmann::json::parse(result.value());
62 if (result != std::nullopt)
70 return m_config.contains(key);
73 bool Config::has(
const std::string& key,
const std::string& section,
const std::string& delim)
75 const auto sections =
str::split(section, delim);
80 if (root.contains(section))
82 return root[section].contains(key);
89 for (
const auto& sec : sections)
91 if (leaf->contains(sec))
93 leaf = &leaf->at(sec);
97 if (leaf->contains(key))
Config() noexcept
Constructor.
void save()
Save the config file. Must be opened first.
void load(std::string_view file)
Loads config json from disk.
~Config() noexcept
Destructor.
nlohmann::json m_config
Config file as JSON.
bool empty() const
Is the config file blank.
std::string m_path
Filepath.
const nlohmann::json & raw() const noexcept
Get raw json object.
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
std::optional< FileError > write(const std::string &filepath, const std::string &data)
Writes a non-binary file to disk.
std::expected< std::string, FileError > read(const std::string &filepath)
Read a non-binary file on disk.
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.