galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Config.cpp
Go to the documentation of this file.
1
7
9
10#include "Config.hpp"
11
12namespace galaxy
13{
14 Config::Config() noexcept
15 {
16 m_config = "{}"_json;
17 }
18
19 Config::Config(std::string_view file) noexcept
20 {
21 m_config = "{}"_json;
22 load(file);
23 }
24
25 Config::~Config() noexcept
26 {
27 m_config.clear();
28 }
29
30 void Config::load(std::string_view file)
31 {
32 auto path = std::filesystem::path(file);
33 m_path = path.string();
34
35 // The first time we load, if there is no config file, we save the default file
36 // and use that instead.
37 if (!std::filesystem::exists(path))
38 {
39 const auto result = fileutils::write(m_path, m_config.dump(4));
40 if (result != std::nullopt)
41 {
42 result.value().log();
43 }
44 }
45 else
46 {
47 const auto result = fileutils::read(m_path);
48 if (result)
49 {
50 m_config = nlohmann::json::parse(result.value());
51 }
52 else
53 {
54 result.error().log();
55 }
56 }
57 }
58
60 {
61 const auto result = fileutils::write(m_path, m_config.dump(4));
62 if (result != std::nullopt)
63 {
64 result.value().log();
65 }
66 }
67
68 bool Config::has(const std::string& key) noexcept
69 {
70 return m_config.contains(key);
71 }
72
73 bool Config::has(const std::string& key, const std::string& section, const std::string& delim)
74 {
75 const auto sections = str::split(section, delim);
76 if (sections.empty())
77 {
78 // Single section.
79 const auto& root = m_config;
80 if (root.contains(section))
81 {
82 return root[section].contains(key);
83 }
84 }
85 else
86 {
87 // Multiple sections.
88 nlohmann::json* leaf = &m_config;
89 for (const auto& sec : sections)
90 {
91 if (leaf->contains(sec))
92 {
93 leaf = &leaf->at(sec);
94 }
95 }
96
97 if (leaf->contains(key))
98 {
99 return true;
100 }
101 }
102
103 return false;
104 }
105
106 bool Config::empty() const
107 {
108 return m_config.empty();
109 }
110
111 void Config::raw(const nlohmann::json& json) noexcept
112 {
113 m_config = json;
114 }
115
116 const nlohmann::json& Config::raw() const noexcept
117 {
118 return m_config;
119 }
120} // namespace galaxy
Config() noexcept
Constructor.
Definition Config.cpp:14
void save()
Save the config file. Must be opened first.
Definition Config.cpp:59
void load(std::string_view file)
Loads config json from disk.
Definition Config.cpp:30
~Config() noexcept
Destructor.
Definition Config.cpp:25
nlohmann::json m_config
Config file as JSON.
Definition Config.hpp:208
bool empty() const
Is the config file blank.
Definition Config.cpp:106
std::string m_path
Filepath.
Definition Config.hpp:213
const nlohmann::json & raw() const noexcept
Get raw json object.
Definition Config.cpp:116
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
Definition Config.cpp:68
std::optional< FileError > write(const std::string &filepath, const std::string &data)
Writes a non-binary file to disk.
Definition FileUtils.cpp:56
std::expected< std::string, FileError > read(const std::string &filepath)
Read a non-binary file on disk.
Definition FileUtils.cpp:27
std::vector< std::string > split(std::string_view input, std::string_view delim) noexcept
Split a string based on a delimiter.
Timer.hpp galaxy.
Definition Timer.cpp:18