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
8#include <fstream>
9
10#include "Config.hpp"
11
12namespace galaxy
13{
14 namespace core
15 {
16 Config::Config() noexcept
17 : m_loaded {false}
18 {
19 m_config = "{\"config\":{}}"_json;
20 }
21
22 Config::Config(std::string_view file) noexcept
23 : m_loaded {false}
24 {
25 m_config = "{\"config\":{}}"_json;
26
27 load(file);
28 }
29
30 Config::~Config() noexcept
31 {
32 m_loaded = false;
33 m_config.clear();
34 }
35
36 void Config::load(std::string_view file)
37 {
38 auto path = std::filesystem::path(file);
39 m_path = path.string();
40
41 if (!std::filesystem::exists(path))
42 {
43 std::ofstream ofs {m_path, std::ofstream::out | std::ofstream::trunc};
44
45 if (ofs.good())
46 {
47 ofs << m_config.dump(4);
48 ofs.close();
49 }
50 else
51 {
52 ofs.close();
53 GALAXY_LOG(GALAXY_FATAL, "Failed to save config file to disk.");
54 }
55 }
56 else
57 {
58 std::ifstream input {m_path, std::ifstream::in};
59
60 if (!input.good())
61 {
62 input.close();
63 GALAXY_LOG(GALAXY_FATAL, "Failed to load config file.");
64 }
65 else
66 {
67 input >> m_config;
68 input.close();
69 }
70 }
71
72 m_loaded = true;
73 }
74
76 {
77 if (m_loaded)
78 {
79 std::ofstream ofs {m_path, std::ofstream::out | std::ofstream::trunc};
80
81 if (ofs.good())
82 {
83 ofs << m_config.dump(4);
84 }
85 else
86 {
87 GALAXY_LOG(GALAXY_ERROR, "Failed to save config file to disk.");
88 }
89
90 ofs.close();
91 }
92 else
93 {
94 GALAXY_LOG(GALAXY_WARNING, "Attempted to save config that was not loaded.");
95 }
96 }
97
98 bool Config::has(const std::string& key) noexcept
99 {
100 if (m_loaded)
101 {
102 return m_config["config"].contains(key);
103 }
104
105 return false;
106 }
107
108 bool Config::has(const std::string& key, const std::string& section, const std::string& delim)
109 {
110 if (m_loaded)
111 {
112 const auto sections = strutils::split(section, delim);
113 if (sections.empty())
114 {
115 // Single section.
116 const auto& root = m_config["config"];
117 if (root.contains(section))
118 {
119 return root[section].contains(key);
120 }
121 }
122 else
123 {
124 // Multiple sections.
125 nlohmann::json* leaf = &m_config["config"];
126 for (const auto& sec : sections)
127 {
128 if (leaf->contains(sec))
129 {
130 leaf = &leaf->at(sec);
131 }
132 }
133
134 if (leaf->contains(key))
135 {
136 return true;
137 }
138 }
139 }
140
141 return false;
142 }
143
144 bool Config::empty() const
145 {
146 if (m_loaded)
147 {
148 return m_config.at("config").empty();
149 }
150
151 return true;
152 }
153
154 void Config::raw(const nlohmann::json& json) noexcept
155 {
156 m_config = json;
157 m_loaded = true;
158 }
159
160 const nlohmann::json& Config::raw() const noexcept
161 {
162 return m_config;
163 }
164 } // namespace core
165} // namespace galaxy
#define GALAXY_WARNING
Definition Log.hpp:24
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_FATAL
Definition Log.hpp:26
#define GALAXY_ERROR
Definition Log.hpp:25
nlohmann::json m_config
Config file as JSON.
Definition Config.hpp:215
const nlohmann::json & raw() const noexcept
Get raw json object.
Definition Config.cpp:160
~Config() noexcept
Destructor.
Definition Config.cpp:30
bool empty() const
Is the config file blank.
Definition Config.cpp:144
void save()
Save the config file. Must be opened first.
Definition Config.cpp:75
bool has(const std::string &key) noexcept
Check if the config file actually has a value.
Definition Config.cpp:98
void load(std::string_view file)
Checks if config exists and flags if a config needs to be created.
Definition Config.cpp:36
Config() noexcept
Constructor.
Definition Config.cpp:16
bool m_loaded
Config loaded flag.
Definition Config.hpp:210
std::string m_path
Filepath.
Definition Config.hpp:220
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 Async.hpp:17