R-Type  2
Doom but in better
Loading...
Searching...
No Matches
TOMLLoader.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** TOMLLoader.cpp
6*/
7
14#include "TOMLLoader.hpp"
15
17{
18 _loadNodeTypeEquivalence();
19};
20
21TOMLLoader::TOMLLoader(const std::string &tomlPath)
22{
23 setTOMLPath(tomlPath);
24 _loadNodeTypeEquivalence();
25};
26
28{
29 update(tomlInstance);
30};
31
32TOMLLoader::TOMLLoader(const toml::table &tomlTable, const std::string &tomlPath)
33{
34 _toml = tomlTable;
35 _tomlLoaded = true;
36 _tomlPath = tomlPath;
37 _loadNodeTypeEquivalence();
38};
39
40TOMLLoader::TOMLLoader(const toml::array &tomlArray, const std::string &tomlPath)
41{
42 _tomlLoaded = true;
43 update(tomlArray);
44 _tomlPath = tomlPath;
45 _loadNodeTypeEquivalence();
46};
47
48TOMLLoader::TOMLLoader(const toml::array &tomlArray, const std::string &key, const std::string &tomlPath)
49{
50 _tomlLoaded = true;
51 update(tomlArray);
52 _tomlPath = tomlPath;
53 _loadNodeTypeEquivalence();
54};
55
56void TOMLLoader::setTOMLPath(const std::string &tomlPath)
57{
58 _tomlPath = tomlPath;
59 _loadTOML();
60};
61
62const bool TOMLLoader::isTOMLLoaded() const
63{
64 return _tomlLoaded;
65};
66
67const std::string TOMLLoader::getTOMLPath() const
68{
69 return _tomlPath;
70};
71
72const std::string TOMLLoader::getTOMLString() const
73{
74 _ensureLoaded();
75 return _tomlString;
76};
77
78const toml::table TOMLLoader::getRawTOML() const
79{
80 _ensureLoaded();
81 return _toml;
82};
83
84const toml::node_type TOMLLoader::getValueType(const std::string &key) const
85{
86 _ensureLoaded();
87 if (!hasKey(key)) {
88 throw CustomExceptions::NoTOMLKey(_tomlPath, key);
89 }
90 return _toml[key].type();
91}
92
93const std::string TOMLLoader::getValueTypeAsString(const std::string &key) const
94{
95 _ensureLoaded();
96 toml::node_type nodeType = getValueType(key);
97 auto it = _nodeTypeEquivalence.find(nodeType);
98 if (it != _nodeTypeEquivalence.end()) {
99 return it->second;
100 }
101 return "unknown";
102}
103
104const std::string TOMLLoader::getTypeAsString(const std::string &key) const
105{
106 return getValueTypeAsString(key);
107}
108
109const std::string TOMLLoader::getTypeAsString(const toml::node_type &type) const
110{
111 auto it = _nodeTypeEquivalence.find(type);
112 if (it != _nodeTypeEquivalence.end()) {
113 return it->second;
114 }
115 return "unknown";
116}
117
118const bool TOMLLoader::hasKey(const std::string &key) const
119{
120 _ensureLoaded();
121 return _toml.contains(key);
122};
123
124std::vector<std::string> TOMLLoader::getKeys() const
125{
126 _ensureLoaded();
127 std::vector<std::string> keys;
128 for (const auto &[key, _] : _toml) {
129 keys.push_back(std::string(key));
130 }
131 return keys;
132};
133
134toml::table TOMLLoader::getTable(const std::string &key) const
135{
136 _ensureLoaded();
137 if (auto table = _toml[key].as_table()) {
138 return *table;
139 }
140 throw CustomExceptions::NoTOMLKey(_tomlPath, key);
141};
142
143toml::array TOMLLoader::getArray(const std::string &key) const
144{
145 _ensureLoaded();
146 if (auto array = _toml[key].as_array()) {
147 return *array;
148 }
149 throw CustomExceptions::NoTOMLKey(_tomlPath, key);
150};
151
153{
154 _tomlLoaded = copy.isTOMLLoaded();
155 if (!_tomlLoaded) {
156 return;
157 }
158 _tomlPath = copy.getTOMLPath();
159 _toml = copy.getRawTOML();
160}
161
162void TOMLLoader::update(const toml::table &copy)
163{
164 _toml = copy;
165 _tomlLoaded = true;
166}
167
168void TOMLLoader::update(const toml::array &copy)
169{
170 _toml = toml::table{ {"array", copy} };
171 _tomlLoaded = true;
172}
173
174void TOMLLoader::update(const toml::array &copy, const std::string &key)
175{
176 _toml = toml::table{ {key, copy} };
177 _tomlLoaded = true;
178}
179
181{
182 _ensureLoaded();
183 PRETTY_INFO << "TOML Contents:\n" + _tomlString << std::endl;
184};
185
187{
188 update(copy);
189 return *this;
190}
191
192TOMLLoader &TOMLLoader::operator=(const toml::table &copy)
193{
194 update(copy);
195 return *this;
196}
197
198TOMLLoader &TOMLLoader::operator=(const toml::array &copy)
199{
200 update(copy);
201 return *this;
202}
203
204void TOMLLoader::_loadTOML()
205{
206 _tomlLoaded = false;
207 try {
208 _toml = toml::parse_file(_tomlPath);
209 }
210 catch (const toml::parse_error &e) {
211 throw CustomExceptions::InvalidTOML(_tomlPath, e.what());
212 }
213 std::ostringstream oss;
214 oss << _toml;
215 _tomlString = oss.str();
216 _tomlLoaded = true;
217};
218
219void TOMLLoader::_ensureLoaded() const
220{
221 if (!_tomlLoaded) {
222 throw CustomExceptions::NoTOML(_tomlPath);
223 }
224};
225
226void TOMLLoader::_loadNodeTypeEquivalence()
227{
228 _nodeTypeEquivalence[toml::node_type::none] = "none";
229 _nodeTypeEquivalence[toml::node_type::date] = "date";
230 _nodeTypeEquivalence[toml::node_type::time] = "time";
231 _nodeTypeEquivalence[toml::node_type::table] = "table";
232 _nodeTypeEquivalence[toml::node_type::array] = "array";
233 _nodeTypeEquivalence[toml::node_type::string] = "string";
234 _nodeTypeEquivalence[toml::node_type::integer] = "integer";
235 _nodeTypeEquivalence[toml::node_type::boolean] = "boolean";
236 _nodeTypeEquivalence[toml::node_type::date_time] = "date_time";
237 _nodeTypeEquivalence[toml::node_type::floating_point] = "floating_point";
238}
239
240std::ostream &operator<<(std::ostream &os, const TOMLLoader &node)
241{
242 os << node.getTOMLString();
243 return os;
244};
#define PRETTY_INFO
Info log with details and colour.
std::ostream & operator<<(std::ostream &os, const TOMLLoader &node)
Overloads the stream insertion operator for TOMLLoader.
Contains the TOMLLoader class for handling loading and navigation of TOML data.
This is the class in charge of informing the user that the toml file they provided is invalid.
Definition Invalid.hpp:608
This is the class in charge of informing the user that they tried to access a non-existant toml key i...
Definition No.hpp:772
This is the class in charge of informing the user that they tried to access a non-existant toml insta...
Definition No.hpp:742
A utility class for parsing, navigating, and managing TOML files and data.
const toml::node_type getValueType(const std::string &key) const
Retrieves the type of a value for a specific key as a TOML node type.
const std::string getTOMLString() const
Retrieves the TOML data as a string.
void update(const TOMLLoader &copy)
Updates the current loader with another loader's data.
const toml::table getRawTOML() const
Retrieves the raw TOML table.
TOMLLoader & operator=(const TOMLLoader &copy)
Copy assignment operator.
TOMLLoader()
Default constructor.
const bool isTOMLLoaded() const
Checks if the TOML file is successfully loaded.
const std::string getTOMLPath() const
Retrieves the path of the loaded TOML file.
toml::table getTable(const std::string &key) const
Retrieves a TOML table for a specific key.
const bool hasKey(const std::string &key) const
Checks if a specific key exists in the TOML data.
void setTOMLPath(const std::string &tomlPath)
Sets the path of the TOML file to load.
void printTOML() const
Prints the TOML data to the debug stream.
std::vector< std::string > getKeys() const
Retrieves all keys from the TOML table.
const std::string getValueTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string.
const std::string getTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string (alias).
toml::array getArray(const std::string &key) const
Retrieves a TOML array for a specific key.