summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/XMLUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/XMLUtils.cpp')
-rw-r--r--xbmc/utils/XMLUtils.cpp343
1 files changed, 343 insertions, 0 deletions
diff --git a/xbmc/utils/XMLUtils.cpp b/xbmc/utils/XMLUtils.cpp
new file mode 100644
index 0000000..d921602
--- /dev/null
+++ b/xbmc/utils/XMLUtils.cpp
@@ -0,0 +1,343 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "XMLUtils.h"
10#include "URL.h"
11#include "StringUtils.h"
12
13bool XMLUtils::GetHex(const TiXmlNode* pRootNode, const char* strTag, uint32_t& hexValue)
14{
15 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
16 if (!pNode || !pNode->FirstChild()) return false;
17 return sscanf(pNode->FirstChild()->Value(), "%x", &hexValue) == 1;
18}
19
20
21bool XMLUtils::GetUInt(const TiXmlNode* pRootNode, const char* strTag, uint32_t& uintValue)
22{
23 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
24 if (!pNode || !pNode->FirstChild()) return false;
25 uintValue = atol(pNode->FirstChild()->Value());
26 return true;
27}
28
29bool XMLUtils::GetUInt(const TiXmlNode* pRootNode, const char* strTag, uint32_t &value, const uint32_t min, const uint32_t max)
30{
31 if (GetUInt(pRootNode, strTag, value))
32 {
33 if (value < min) value = min;
34 if (value > max) value = max;
35 return true;
36 }
37 return false;
38}
39
40bool XMLUtils::GetLong(const TiXmlNode* pRootNode, const char* strTag, long& lLongValue)
41{
42 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
43 if (!pNode || !pNode->FirstChild()) return false;
44 lLongValue = atol(pNode->FirstChild()->Value());
45 return true;
46}
47
48bool XMLUtils::GetInt(const TiXmlNode* pRootNode, const char* strTag, int& iIntValue)
49{
50 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
51 if (!pNode || !pNode->FirstChild()) return false;
52 iIntValue = atoi(pNode->FirstChild()->Value());
53 return true;
54}
55
56bool XMLUtils::GetInt(const TiXmlNode* pRootNode, const char* strTag, int &value, const int min, const int max)
57{
58 if (GetInt(pRootNode, strTag, value))
59 {
60 if (value < min) value = min;
61 if (value > max) value = max;
62 return true;
63 }
64 return false;
65}
66
67bool XMLUtils::GetDouble(const TiXmlNode* root, const char* tag, double& value)
68{
69 const TiXmlNode* node = root->FirstChild(tag);
70 if (!node || !node->FirstChild()) return false;
71 value = atof(node->FirstChild()->Value());
72 return true;
73}
74
75bool XMLUtils::GetFloat(const TiXmlNode* pRootNode, const char* strTag, float& value)
76{
77 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
78 if (!pNode || !pNode->FirstChild()) return false;
79 value = (float)atof(pNode->FirstChild()->Value());
80 return true;
81}
82
83bool XMLUtils::GetFloat(const TiXmlNode* pRootElement, const char *tagName, float& fValue, const float fMin, const float fMax)
84{
85 if (GetFloat(pRootElement, tagName, fValue))
86 { // check range
87 if (fValue < fMin) fValue = fMin;
88 if (fValue > fMax) fValue = fMax;
89 return true;
90 }
91 return false;
92}
93
94bool XMLUtils::GetBoolean(const TiXmlNode* pRootNode, const char* strTag, bool& bBoolValue)
95{
96 const TiXmlNode* pNode = pRootNode->FirstChild(strTag );
97 if (!pNode || !pNode->FirstChild()) return false;
98 std::string strEnabled = pNode->FirstChild()->ValueStr();
99 StringUtils::ToLower(strEnabled);
100 if (strEnabled == "off" || strEnabled == "no" || strEnabled == "disabled" || strEnabled == "false" || strEnabled == "0" )
101 bBoolValue = false;
102 else
103 {
104 bBoolValue = true;
105 if (strEnabled != "on" && strEnabled != "yes" && strEnabled != "enabled" && strEnabled != "true")
106 return false; // invalid bool switch - it's probably some other string.
107 }
108 return true;
109}
110
111bool XMLUtils::GetString(const TiXmlNode* pRootNode, const char* strTag, std::string& strStringValue)
112{
113 const TiXmlElement* pElement = pRootNode->FirstChildElement(strTag);
114 if (!pElement) return false;
115
116 const char* encoded = pElement->Attribute("urlencoded");
117 const TiXmlNode* pNode = pElement->FirstChild();
118 if (pNode != NULL)
119 {
120 strStringValue = pNode->ValueStr();
121 if (encoded && StringUtils::CompareNoCase(encoded, "yes") == 0)
122 strStringValue = CURL::Decode(strStringValue);
123 return true;
124 }
125 strStringValue.clear();
126 return true;
127}
128
129std::string XMLUtils::GetString(const TiXmlNode* pRootNode, const char* strTag)
130{
131 std::string temp;
132 GetString(pRootNode, strTag, temp);
133 return temp;
134}
135
136bool XMLUtils::HasChild(const TiXmlNode* pRootNode, const char* strTag)
137{
138 const TiXmlElement* pElement = pRootNode->FirstChildElement(strTag);
139 if (!pElement) return false;
140 const TiXmlNode* pNode = pElement->FirstChild();
141 return (pNode != NULL);
142}
143
144bool XMLUtils::GetAdditiveString(const TiXmlNode* pRootNode, const char* strTag,
145 const std::string& strSeparator, std::string& strStringValue,
146 bool clear)
147{
148 std::string strTemp;
149 const TiXmlElement* node = pRootNode->FirstChildElement(strTag);
150 bool bResult=false;
151 if (node && node->FirstChild() && clear)
152 strStringValue.clear();
153 while (node)
154 {
155 if (node->FirstChild())
156 {
157 bResult = true;
158 strTemp = node->FirstChild()->Value();
159 const char* clear=node->Attribute("clear");
160 if (strStringValue.empty() || (clear && StringUtils::CompareNoCase(clear, "true") == 0))
161 strStringValue = strTemp;
162 else
163 strStringValue += strSeparator+strTemp;
164 }
165 node = node->NextSiblingElement(strTag);
166 }
167
168 return bResult;
169}
170
171/*!
172 Parses the XML for multiple tags of the given name.
173 Does not clear the array to support chaining.
174*/
175bool XMLUtils::GetStringArray(const TiXmlNode* pRootNode, const char* strTag, std::vector<std::string>& arrayValue, bool clear /* = false */, const std::string& separator /* = "" */)
176{
177 std::string strTemp;
178 const TiXmlElement* node = pRootNode->FirstChildElement(strTag);
179 bool bResult=false;
180 if (node && node->FirstChild() && clear)
181 arrayValue.clear();
182 while (node)
183 {
184 if (node->FirstChild())
185 {
186 bResult = true;
187 strTemp = node->FirstChild()->ValueStr();
188
189 const char* clearAttr = node->Attribute("clear");
190 if (clearAttr && StringUtils::CompareNoCase(clearAttr, "true") == 0)
191 arrayValue.clear();
192
193 if (strTemp.empty())
194 continue;
195
196 if (separator.empty())
197 arrayValue.push_back(strTemp);
198 else
199 {
200 std::vector<std::string> tempArray = StringUtils::Split(strTemp, separator);
201 arrayValue.insert(arrayValue.end(), tempArray.begin(), tempArray.end());
202 }
203 }
204 node = node->NextSiblingElement(strTag);
205 }
206
207 return bResult;
208}
209
210bool XMLUtils::GetPath(const TiXmlNode* pRootNode, const char* strTag, std::string& strStringValue)
211{
212 const TiXmlElement* pElement = pRootNode->FirstChildElement(strTag);
213 if (!pElement) return false;
214
215 const char* encoded = pElement->Attribute("urlencoded");
216 const TiXmlNode* pNode = pElement->FirstChild();
217 if (pNode != NULL)
218 {
219 strStringValue = pNode->Value();
220 if (encoded && StringUtils::CompareNoCase(encoded, "yes") == 0)
221 strStringValue = CURL::Decode(strStringValue);
222 return true;
223 }
224 strStringValue.clear();
225 return false;
226}
227
228bool XMLUtils::GetDate(const TiXmlNode* pRootNode, const char* strTag, CDateTime& date)
229{
230 std::string strDate;
231 if (GetString(pRootNode, strTag, strDate) && !strDate.empty())
232 {
233 date.SetFromDBDate(strDate);
234 return true;
235 }
236
237 return false;
238}
239
240bool XMLUtils::GetDateTime(const TiXmlNode* pRootNode, const char* strTag, CDateTime& dateTime)
241{
242 std::string strDateTime;
243 if (GetString(pRootNode, strTag, strDateTime) && !strDateTime.empty())
244 {
245 dateTime.SetFromDBDateTime(strDateTime);
246 return true;
247 }
248
249 return false;
250}
251
252std::string XMLUtils::GetAttribute(const TiXmlElement *element, const char *tag)
253{
254 if (element)
255 {
256 const char *attribute = element->Attribute(tag);
257 if (attribute)
258 return attribute;
259 }
260 return "";
261}
262
263void XMLUtils::SetAdditiveString(TiXmlNode* pRootNode, const char *strTag, const std::string& strSeparator, const std::string& strValue)
264{
265 std::vector<std::string> list = StringUtils::Split(strValue, strSeparator);
266 for (std::vector<std::string>::const_iterator i = list.begin(); i != list.end(); ++i)
267 SetString(pRootNode, strTag, *i);
268}
269
270void XMLUtils::SetStringArray(TiXmlNode* pRootNode, const char *strTag, const std::vector<std::string>& arrayValue)
271{
272 for (unsigned int i = 0; i < arrayValue.size(); i++)
273 SetString(pRootNode, strTag, arrayValue.at(i));
274}
275
276TiXmlNode* XMLUtils::SetString(TiXmlNode* pRootNode, const char *strTag, const std::string& strValue)
277{
278 TiXmlElement newElement(strTag);
279 TiXmlNode *pNewNode = pRootNode->InsertEndChild(newElement);
280 if (pNewNode)
281 {
282 TiXmlText value(strValue);
283 pNewNode->InsertEndChild(value);
284 }
285 return pNewNode;
286}
287
288TiXmlNode* XMLUtils::SetInt(TiXmlNode* pRootNode, const char *strTag, int value)
289{
290 std::string strValue = StringUtils::Format("%i", value);
291 return SetString(pRootNode, strTag, strValue);
292}
293
294void XMLUtils::SetLong(TiXmlNode* pRootNode, const char *strTag, long value)
295{
296 std::string strValue = StringUtils::Format("%ld", value);
297 SetString(pRootNode, strTag, strValue);
298}
299
300TiXmlNode* XMLUtils::SetFloat(TiXmlNode* pRootNode, const char *strTag, float value)
301{
302 std::string strValue = StringUtils::Format("%f", value);
303 return SetString(pRootNode, strTag, strValue);
304}
305
306TiXmlNode* XMLUtils::SetDouble(TiXmlNode* pRootNode, const char* strTag, double value)
307{
308 std::string strValue = StringUtils::Format("%lf", value);
309 return SetString(pRootNode, strTag, strValue);
310}
311
312void XMLUtils::SetBoolean(TiXmlNode* pRootNode, const char *strTag, bool value)
313{
314 SetString(pRootNode, strTag, value ? "true" : "false");
315}
316
317void XMLUtils::SetHex(TiXmlNode* pRootNode, const char *strTag, uint32_t value)
318{
319 std::string strValue = StringUtils::Format("%x", value);
320 SetString(pRootNode, strTag, strValue);
321}
322
323void XMLUtils::SetPath(TiXmlNode* pRootNode, const char *strTag, const std::string& strValue)
324{
325 TiXmlElement newElement(strTag);
326 newElement.SetAttribute("pathversion", path_version);
327 TiXmlNode *pNewNode = pRootNode->InsertEndChild(newElement);
328 if (pNewNode)
329 {
330 TiXmlText value(strValue);
331 pNewNode->InsertEndChild(value);
332 }
333}
334
335void XMLUtils::SetDate(TiXmlNode* pRootNode, const char *strTag, const CDateTime& date)
336{
337 SetString(pRootNode, strTag, date.IsValid() ? date.GetAsDBDate() : "");
338}
339
340void XMLUtils::SetDateTime(TiXmlNode* pRootNode, const char *strTag, const CDateTime& dateTime)
341{
342 SetString(pRootNode, strTag, dateTime.IsValid() ? dateTime.GetAsDBDateTime() : "");
343}