libdonut  2.3.2
Application framework for cross-platform game development in C++20
xml.hpp
Go to the documentation of this file.
1 #ifndef DONUT_XML_HPP
2 #define DONUT_XML_HPP
3 
4 #include <cstddef> // std::size_t
5 #include <memory> // std::unique_ptr
6 #include <stdexcept> // std::runtime_error
7 #include <string> // std::string
8 #include <string_view> // std::string_view
9 
10 namespace donut::xml {
11 
15 struct Error : std::runtime_error {
19  std::string_view::iterator position;
20 
24  std::size_t lineNumber;
25 
26  Error(const std::string& message, std::string_view::iterator position, std::size_t lineNumber)
27  : std::runtime_error(message)
30 
31  Error(const char* message, std::string_view::iterator position, std::size_t lineNumber)
32  : std::runtime_error(message)
35 };
36 
40 struct Attribute {
41  std::string name{};
42  std::string value{};
43  std::unique_ptr<Attribute> next{};
44 };
45 
49 struct Element {
50  std::string tag{};
51  std::string content{};
52  std::unique_ptr<Attribute> attributes{};
53  std::unique_ptr<Element> children{};
54  std::unique_ptr<Element> next{};
55 };
56 
60 struct Document {
71  [[nodiscard]] static Document parse(std::string_view xmlString);
72 
73  std::unique_ptr<Element> declaration{};
74  std::unique_ptr<Element> root{};
75 };
76 
77 } // namespace donut::xml
78 
79 #endif
Definition: utilities.hpp:182
Named attribute of an Element with an optional value.
Definition: xml.hpp:40
std::string name
Name of the attribute.
Definition: xml.hpp:41
std::unique_ptr< Attribute > next
Next neighboring attribute in the list that this attribute is part of.
Definition: xml.hpp:43
std::string value
Attribute value, or empty for no value.
Definition: xml.hpp:42
Tree of Element nodes defined by an XML file.
Definition: xml.hpp:60
std::unique_ptr< Element > root
Root element of the document tree.
Definition: xml.hpp:74
std::unique_ptr< Element > declaration
Optional XML declaration.
Definition: xml.hpp:73
static Document parse(std::string_view xmlString)
Parse a document from an XML string.
Node in a Document.
Definition: xml.hpp:49
std::string content
Raw non-element text content of the element.
Definition: xml.hpp:51
std::unique_ptr< Attribute > attributes
Linked list of element attributes.
Definition: xml.hpp:52
std::unique_ptr< Element > next
Next neighboring element in the list that this element is part of.
Definition: xml.hpp:54
std::unique_ptr< Element > children
Linked list of children of this element.
Definition: xml.hpp:53
std::string tag
Element tag name.
Definition: xml.hpp:50
Exception type for errors originating from the XML API.
Definition: xml.hpp:15
std::size_t lineNumber
Line number, starting at 1 for the first line, where the error occured.
Definition: xml.hpp:24
Error(const std::string &message, std::string_view::iterator position, std::size_t lineNumber)
Definition: xml.hpp:26
Error(const char *message, std::string_view::iterator position, std::size_t lineNumber)
Definition: xml.hpp:31
std::string_view::iterator position
Iterator into the source XML string where the error originated from.
Definition: xml.hpp:19