Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
xmlFile.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_FILEHANDLERS_XMLFILE_HPP_
2#define IV_SRC_FILEHANDLERS_XMLFILE_HPP_
3
4#include "core/assert.hpp"
5#include "core/defines.hpp"
6#include "third_party/pugiXML/pugixml.hpp"
7
8#include <limits>
9#include <vector>
10
11namespace iv::file::xml
12{
13
14class node : public pugi::xml_node
15{
16public:
17 node() = default;
18 // ReSharper disable once CppNonExplicitConvertingConstructor
19 node(const pugi::xml_node &_node);
20 node(const node &other) = default;
21 node(node &&other) = default;
22 ~node() = default;
23
24 node &operator=(const node &other) = default;
25 node &operator=(node &&other) = default;
26
27 [[nodiscard]] bool emptyNode() const;
28 [[nodiscard]] bool isNull() const;
29
30 void removeAttributes();
31
32 template<typename T>
33 T getAttribute(const std::string_view key, T defaultValue) const
34 {
35 auto attr = pugi::xml_node::attribute(key.data());
36
37 if constexpr (std::is_same_v<T, double>)
38 {
39 return attr.as_double(defaultValue);
40 }
41 else if constexpr (std::is_same_v<T, float>)
42 {
43 return attr.as_float(defaultValue);
44 }
45 else if constexpr (std::is_same_v<T, int32_t> || std::is_same_v<T, int16_t> || std::is_same_v<T, int8_t>)
46 {
47 return attr.as_int(defaultValue);
48 }
49 else if constexpr (std::is_same_v<T, int64_t>)
50 {
51 return attr.as_llong(defaultValue);
52 }
53 else if constexpr (std::is_same_v<T, uint32_t> || std::is_same_v<T, uint16_t> || std::is_same_v<T, uint8_t>)
54 {
55 return attr.as_uint(defaultValue);
56 }
57 else if constexpr (std::is_same_v<T, uint64_t>)
58 {
59 return attr.as_ullong(defaultValue);
60 }
61 else if constexpr (std::is_same_v<T, bool>)
62 {
63 return attr.as_bool(defaultValue);
64 }
65 else if constexpr (std::is_same_v<T, std::string>)
66 {
67 return std::string(attr.as_string(defaultValue.c_str()));
68 }
69 else
70 {
71 static_assert(iv::assert::always_false<T>, "Unreachable code");
72 return T {};
73 }
74 }
75
76 template<typename T>
77 bool addAttribute(const std::string_view key, T value)
78 {
79 pugi::xml_attribute attribute = pugi::xml_node::append_attribute(key.data());
80
81 if constexpr (std::is_same_v<T, std::string>)
82 {
83 attribute.set_value(value.c_str());
84 }
85 else if constexpr (std::is_same_v<T, std::string_view>)
86 {
87 attribute.set_value(std::string(value).c_str());
88 }
89 else
90 {
91 attribute.set_value(value);
92 }
93
94 const bool inserted = attribute != nullptr;
95
96 if (not inserted)
97 {
98 IV_ASSERT_MSG("[CFileXML] Attribute '%s' can't inserted in node '%s'.", keyName.c_str(),
99 pugi::xml_node::name());
100 }
101
102 return inserted;
103 }
104
105 void setText(const std::string_view text) const
106 {
107 pugi::xml_node::text().set(text.data());
108 }
109
110 [[nodiscard]] std::string getText() const
111 {
112 return pugi::xml_node::text().as_string();
113 }
114
115 void removeChild(std::string_view name);
116 void removeChildren();
117 [[nodiscard]] bool hasChild(std::string_view name) const;
118 void appendNode(const iv::file::xml::node &_node);
119 [[nodiscard]] iv::file::xml::node getChild(std::string_view name) const;
120 [[nodiscard]] std::vector<node> getChildren() const;
121 iv::file::xml::node appendChild(std::string_view name);
122
123private:
124 std::string m_nodeName;
125};
126
127class File final
128{
129public:
130 static File fromString(std::string_view xmlString);
131
132 File() = default;
133 explicit File(std::string_view pathFile);
134 File(const File &other) = delete;
135 File(File &&other) = default;
136 ~File() = default;
137
138 File &operator=(const File &other) = delete;
139 File &operator=(File &&other) = delete;
140
141 bool read(bool checkIntegrity = true);
142 [[nodiscard]] bool save();
143 [[nodiscard]] bool hasChanges() const;
144
145 [[nodiscard]] iv::types::timestamp lastTimeEdited() const;
146
147 [[nodiscard]] iv::file::xml::node getChild(std::string_view name) const;
148 iv::file::xml::node setChild(std::string_view name, std::string_view parentName = "");
149
150 template<typename T>
151 T getAttribute(const std::string_view node, std::string_view key, T defaultValue) const
152 {
153 return getChild(node).getAttribute(key, defaultValue);
154 }
155
156 template<typename T>
157 bool addAttribute(const std::string_view node, std::string_view key, T value)
158 {
159 return getChild(node).addAttribute(key, value);
160 }
161
162 [[nodiscard]] std::string toString() const;
163
164private:
165 [[nodiscard]] uint16_t prvCalculateFileValidation() const;
166 [[nodiscard]] bool setMetadata(uint16_t crcValue) const;
167 bool prvCheckFileValidation(uint16_t crcValue) const;
168
169 std::string m_pathFile;
170 pugi::xml_document m_xml;
171
172 struct Files
173 {
174 static constexpr std::string_view fileNameTmp = "/tmp/diamar_serialized.xml";
175 };
176
177 struct Keys
178 {
179 static constexpr std::string_view fileIntegrity = "FileIntegrity";
180 static constexpr std::string_view lastTimeEdited = "LastTimeEdited";
181 static constexpr std::string_view lastUserEdited = "LastUserEdited";
182 static constexpr std::string_view lastPCEdited = "LastPCEdited";
183 };
184
185 struct Sections
186 {
187 static constexpr std::string metadata {"Metadata"};
188 };
189
191 {
192 static constexpr uint64_t fileIntegrityInvalid = std::numeric_limits<uint64_t>::max();
193 };
194};
195
196}// namespace iv::file::xml
197
198#endif//IV_SRC_FILEHANDLERS_XMLFILE_HPP_
#define IV_ASSERT_MSG(msg,...)
Definition assert.hpp:152
Definition xmlFile.hpp:128
static File fromString(std::string_view xmlString)
Definition xmlFile.cpp:313
File & operator=(File &&other)=delete
bool prvCheckFileValidation(uint16_t crcValue) const
Definition xmlFile.cpp:333
bool addAttribute(const std::string_view node, std::string_view key, T value)
Definition xmlFile.hpp:157
T getAttribute(const std::string_view node, std::string_view key, T defaultValue) const
Definition xmlFile.hpp:151
bool save()
Definition xmlFile.cpp:124
bool hasChanges() const
Definition xmlFile.cpp:148
File & operator=(const File &other)=delete
File(File &&other)=default
iv::file::xml::node getChild(std::string_view name) const
Definition xmlFile.cpp:170
pugi::xml_document m_xml
Definition xmlFile.hpp:170
bool read(bool checkIntegrity=true)
Definition xmlFile.cpp:99
std::string m_pathFile
Definition xmlFile.hpp:169
iv::file::xml::node setChild(std::string_view name, std::string_view parentName="")
Definition xmlFile.cpp:182
bool setMetadata(uint16_t crcValue) const
Definition xmlFile.cpp:285
File(const File &other)=delete
std::string toString() const
Converts current XML data into string format.
Definition xmlFile.cpp:212
iv::types::timestamp lastTimeEdited() const
Definition xmlFile.cpp:165
uint16_t prvCalculateFileValidation() const
Definition xmlFile.cpp:261
Definition xmlFile.hpp:15
T getAttribute(const std::string_view key, T defaultValue) const
Definition xmlFile.hpp:33
std::string getText() const
Definition xmlFile.hpp:110
void removeAttributes()
Definition xmlFile.cpp:23
bool hasChild(std::string_view name) const
Definition xmlFile.cpp:67
bool isNull() const
Definition xmlFile.cpp:33
iv::file::xml::node getChild(std::string_view name) const
Definition xmlFile.cpp:43
void removeChildren()
Definition xmlFile.cpp:38
node & operator=(node &&other)=default
void setText(const std::string_view text) const
Definition xmlFile.hpp:105
bool addAttribute(const std::string_view key, T value)
Definition xmlFile.hpp:77
void removeChild(std::string_view name)
Definition xmlFile.cpp:28
node(const node &other)=default
std::vector< node > getChildren() const
Definition xmlFile.cpp:77
bool emptyNode() const
Definition xmlFile.cpp:18
node(node &&other)=default
std::string m_nodeName
Definition xmlFile.hpp:124
node & operator=(const node &other)=default
void appendNode(const iv::file::xml::node &_node)
Definition xmlFile.cpp:72
iv::file::xml::node appendChild(std::string_view name)
Definition xmlFile.cpp:53
constexpr bool always_false
Definition assert.hpp:14
Definition xmlFile.cpp:13
uint64_t timestamp
Definition types.hpp:21
Definition xmlFile.hpp:191
static constexpr uint64_t fileIntegrityInvalid
Definition xmlFile.hpp:192
Definition xmlFile.hpp:173
static constexpr std::string_view fileNameTmp
Definition xmlFile.hpp:174
Definition xmlFile.hpp:178
static constexpr std::string_view fileIntegrity
Definition xmlFile.hpp:179
static constexpr std::string_view lastPCEdited
Definition xmlFile.hpp:182
static constexpr std::string_view lastTimeEdited
Definition xmlFile.hpp:180
static constexpr std::string_view lastUserEdited
Definition xmlFile.hpp:181
Definition xmlFile.hpp:186
static constexpr std::string metadata
Definition xmlFile.hpp:187