File: Xml.h

package info (click to toggle)
libexadrums 0.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: cpp: 6,970; ansic: 220; makefile: 160; sh: 12
file content (185 lines) | stat: -rw-r--r-- 3,543 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Xml.h
 *
 *  Created on: 11 Feb 2018
 *      Author: jeremy
 */

#ifndef SOURCE_UTIL_XML_H_
#define SOURCE_UTIL_XML_H_

#include <tinyxml2.h>

#include <sstream>
#include <string>
#include <vector>

namespace Util
{

	/**
	 * Xml Element wrapper, very useful for range-based for loops.
	 */
	class XmlElement
	{

	public:

		XmlElement(tinyxml2::XMLElement* element_, const std::string& name_) : element{element_}, name{name_} {}
		XmlElement(tinyxml2::XMLElement* element_) : element{element_}, name{} {}

		inline XmlElement begin() const
		{
			if(name.empty())
			{
				return XmlElement{element->FirstChildElement()};
			}

			return XmlElement{element->FirstChildElement(name.data())};
		}

		inline XmlElement end() const
		{
			return XmlElement{nullptr};
		}

		inline XmlElement operator++()
		{
			if(name.empty())
			{
				element = element->NextSiblingElement();
			}
			else
			{
				element = element->NextSiblingElement(name.data());
			}

			return *this;
		}

		inline XmlElement operator*() const
		{
			return *this;
		}

		inline bool operator!=(const XmlElement& xe) const
		{
			return element != xe.element;
		}

		inline const char* GetText() const
		{
			return element->GetText();
		}

		template <typename T>
		inline bool GetValue(T& value) const
		{
			if(!element)
			{
				value = T{};
				return false;
			}
			auto text = element->GetText();
			if(text == nullptr)
			{
				value = T{};
				return false;
			}
			std::istringstream iss{text};
			iss >> value;
			return true;
		}

		template <typename T>
		inline T GetValue() const
		{
			T value{};
			GetValue(value);
			return value;
		}

		template <typename T>
		inline void Attribute(const std::string& name, T& value) const
		{
			auto attr = element->Attribute(name.data());
			if(attr == nullptr)
			{
				return;
			}
			std::istringstream iss{attr};
			iss >> value;
		}

		template <typename T>
		inline T Attribute(const std::string& name) const
		{
			T value{};
			Attribute<T>(name, value);
			return value;
		}

		inline XmlElement FirstChildElement(const std::string& childName) const
		{
			return XmlElement{element->FirstChildElement(childName.data())};
		}

	private:

		tinyxml2::XMLElement* element;
		std::string name;
	};

	/**
	 * Structure that holds an xml attribute: a name and a value.
	 */
    struct XmlAttr
    {
        template <typename T>
        XmlAttr(std::string name_, const T& val) : name(std::move(name_))
        {
            std::ostringstream oss;
            oss << val;
            value = oss.str();
        }

        std::string name;
        std::string value;
    };

    /**
     * Creates a new xml element.
     * @param doc Xml document in which the xml element is to be inserted.
     * @param name Tag name of the xml element.
     * @param text Contents.
     * @param attributes Xml attributes.
     * @return
     */
    template <typename T = std::string>
    tinyxml2::XMLElement* CreateXmlElement(tinyxml2::XMLDocument& doc, const std::string name, const T& text = "", const std::vector<XmlAttr>& attributes = {})
    {
        namespace xml = tinyxml2;

        xml::XMLElement* element = doc.NewElement(name.data());

        std::ostringstream oss;
        oss << text;

        if(!oss.str().empty())
        {
            element->SetText(oss.str().data());
        }

        for(const auto& itAttr : attributes)
        {
            element->SetAttribute(itAttr.name.data(), itAttr.value.data());
        }

        return element;
    }


}


#endif /* SOURCE_UTIL_XML_H_ */