19 constexpr unsigned int ParseFlags = pugi::parse_default | pugi::parse_declaration | pugi::parse_comments;
20 constexpr unsigned int FormatFlags = pugi::format_write_bom | pugi::format_default;
22 Document::Document() :
23 mDoc(std::make_shared<
pugi::xml_document>()),
24 mResult(std::make_shared<
pugi::xml_parse_result>())
29 bool Document::open(
const std::string& _filename)
34 bool Document::open(
const std::filesystem::path& _filename)
38 *mResult = mDoc->load_file(_filename.c_str(), ParseFlags);
41 mLastErrorFile = _filename;
46 bool Document::open(std::istream& _stream)
50 *mResult = mDoc->load(_stream, ParseFlags);
53 mLastErrorFromStream =
true;
58 bool Document::open(IDataStream* _stream)
62 const auto data = _stream->readAll();
64 *mResult = mDoc->load_buffer(data.data(), data.size(), ParseFlags);
66 mLastErrorFromStream =
true;
71 bool Document::save(
const std::string& _filename)
76 bool Document::save(
const std::filesystem::path& _filename)
79 bool result = mDoc->save_file(_filename.c_str(),
"\t", FormatFlags);
83 mLastError =
"Failed to save XML file";
84 mLastErrorFromStream =
false;
85 mLastErrorFile = _filename;
90 bool Document::save(std::ostream& _stream)
93 mDoc->save(_stream,
"\t", FormatFlags);
96 mLastError =
"Failed to save XML stream";
97 mLastErrorFromStream =
true;
99 return static_cast<bool>(_stream);
102 void Document::clear()
108 bool Document::open(
const UString& _filename)
110 return open(_filename.asUTF8());
113 bool Document::save(
const UString& _filename)
115 return save(_filename.asUTF8());
118 void Document::clearLastError()
121 mResult->status = pugi::status_ok;
123 mLastErrorFile.clear();
124 mLastErrorFromStream =
false;
127 ElementPtr Document::getRoot()
const
129 return mDoc->document_element();
132 ElementPtr Document::createDeclaration(std::string_view _version, std::string_view _encoding)
135 for (
auto child = mDoc->first_child(); child;)
137 auto next = child.next_sibling();
138 if (child.type() == pugi::node_declaration)
139 mDoc->remove_child(child);
143 auto decl = mDoc->prepend_child(pugi::node_declaration);
144 decl.set_name(
"xml");
145 decl.append_attribute(
"version") = _version;
146 decl.append_attribute(
"encoding") = _encoding;
150 ElementPtr Document::createRoot(std::string_view _name)
153 for (
auto child = mDoc->first_child(); child;)
155 auto next = child.next_sibling();
156 if (child.type() == pugi::node_element)
157 mDoc->remove_child(child);
161 return mDoc->append_child(_name);
164 std::string Document::getLastError()
const
169 size_t line = 0, col = 0;
170 std::string lineText;
171 if (mResult->offset >= 0 && !mLastErrorFile.empty())
173 std::ifstream file(mLastErrorFile, std::ios::binary);
177 std::string data((std::istreambuf_iterator<char>(file)), {});
178 for (ptrdiff_t i = 0; i < mResult->offset && i < (ptrdiff_t)data.size(); ++i)
183 ptrdiff_t start = mResult->offset;
184 while (start > 0 && data[start - 1] !=
'\n')
186 col = mResult->offset - start + 1;
187 ptrdiff_t end = data.size();
188 for (ptrdiff_t i = start; i < (ptrdiff_t)data.size(); ++i)
196 lineText = data.substr(start, end - start);
200 toString(
"'", mResult->description(),
"', ", fileName,
"(", line,
",", col,
"): ", lineText);
202 if (!mLastError.empty())
209 ElementEnumerator::ElementEnumerator(std::shared_ptr<pugi::xml_node> parent)
211 for (
auto child : parent->children())
212 mElements.emplace_back(child);
215 bool ElementEnumerator::next()
217 return ++mIndex < mElements.size();
220 bool ElementEnumerator::next(std::string_view _name)
224 if (mElements[mIndex].getName() == _name)
230 ElementPtr ElementEnumerator::operator->()
const
232 return mElements[mIndex];
235 ElementPtr ElementEnumerator::current()
237 return mElements[mIndex];
240 ElementPtr::ElementPtr(
const pugi::xml_node& _node) :
241 mNode(_node ? std::make_shared<pugi::xml_node>(_node) : nullptr)
245 ElementPtr* ElementPtr::operator->()
250 const ElementPtr* ElementPtr::operator->()
const
255 ElementPtr ElementPtr::createChild(std::string_view _name, std::string_view _content)
257 auto child = mNode->append_child(_name);
258 if (!_content.empty())
259 child.text().set(_content);
260 return ElementPtr(child);
263 void ElementPtr::removeChild(ElementPtr _child)
265 mNode->remove_child(*_child.mNode);
268 void ElementPtr::addAttribute(std::string_view _key, std::string_view _value)
270 mNode->append_attribute(_key) = _value;
273 void ElementPtr::removeAttribute(std::string_view _key)
275 mNode->remove_attribute(_key);
278 void ElementPtr::setAttribute(std::string_view _key, std::string_view _value)
280 auto attr = mNode->attribute(_key);
282 attr.set_value(_value);
284 mNode->append_attribute(_key) = _value;
287 void ElementPtr::setContent(std::string_view _content)
289 mNode->remove_children();
290 mNode->text().set(_content);
293 void ElementPtr::clear()
295 mNode->remove_children();
296 mNode->remove_attributes();
299 bool ElementPtr::findAttribute(std::string_view _name, std::string& _value)
301 auto attr = mNode->attribute(_name);
304 _value = attr.as_string();
310 std::string_view ElementPtr::findAttribute(std::string_view _name)
312 auto attr = mNode->attribute(_name);
314 return attr.as_string();
318 std::string ElementPtr::getName()
const
320 return mNode->name();
323 std::string ElementPtr::getContent()
const
325 return mNode->text().as_string();
331 for (
auto attr : mNode->attributes())
332 attrs.emplace_back(attr.name(), attr.as_string());
336 ElementPtr ElementPtr::getParent()
const
338 return ElementPtr(mNode->parent());
341 ElementEnumerator ElementPtr::getElementEnumerator()
343 return ElementEnumerator(mNode);
346 ElementPtr::operator pugi::xml_node()
const
351 const pugi::xml_node& ElementPtr::node()
const
356 Element::Element(std::shared_ptr<pugi::xml_node> _node, std::shared_ptr<pugi::xml_document> _doc) :
358 mDocument(std::move(_doc))
362 std::unique_ptr<Element> ElementPtr::createCopy()
364 auto doc = std::make_shared<pugi::xml_document>();
365 auto copy = doc->append_copy(*mNode);
366 auto nodeCopy = std::make_shared<pugi::xml_node>(copy);
367 return std::make_unique<Element>(nodeCopy, doc);
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
std::string toUtf8(const std::filesystem::path &path)
Convert a filesystem path to UTF-8 bytes.
std::filesystem::path toPath(std::string_view text)
Convert UTF-8 bytes to a native filesystem path.
constexpr unsigned int FormatFlags
std::vector< PairAttribute > VectorAttributes
constexpr unsigned int ParseFlags