Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
XMLParser.cpp
1/*
2 * Copyright (C) 2018 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "XMLParser.h"
19
20XMLParser::XMLParser() {}
21
22XMLParser::~XMLParser() {}
23
24rapidxml::xml_node<>* XMLParser::AppendNode(rapidxml::xml_document<>& doc,
25 rapidxml::xml_node<>* parentNode,
26 const char* name,
27 rapidxml::node_type nodeType)
28{
29 rapidxml::xml_node<>* node = doc.allocate_node(nodeType, name);
30 parentNode->append_node(node);
31 return node;
32}
33
34void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, wxString value)
35{
36 node->value(doc.allocate_string(value.mb_str()));
37}
38
39void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, int value)
40{
41 node->value(doc.allocate_string(wxString::Format("%d", value).mb_str()));
42}
43
44void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, double value)
45{
46 node->value(doc.allocate_string(wxString::FromCDouble(value, 13).mb_str()));
47}
48
49void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
50 rapidxml::xml_node<>* node,
51 const char* atrName,
52 wxString value)
53{
54 node->append_attribute(doc.allocate_attribute(atrName, doc.allocate_string(value.mb_str())));
55}
56
57void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
58 rapidxml::xml_node<>* node,
59 const char* atrName,
60 int value)
61{
62 node->append_attribute(
63 doc.allocate_attribute(atrName, doc.allocate_string(wxString::Format("%d", value).mb_str())));
64}
65
66void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
67 rapidxml::xml_node<>* node,
68 const char* atrName,
69 double value)
70{
71 node->append_attribute(
72 doc.allocate_attribute(atrName, doc.allocate_string(wxString::FromCDouble(value, 13).mb_str())));
73}
74
75double XMLParser::GetNodeValueDouble(rapidxml::xml_node<>* parent, const char* nodeName)
76{
77 double dValue = 0.0;
78 if(parent) {
79 auto node = parent->first_node(nodeName);
80 if(node) wxString(node->value()).ToCDouble(&dValue);
81 }
82 return dValue;
83}
84
85int XMLParser::GetNodeValueInt(rapidxml::xml_node<>* parent, const char* nodeName)
86{
87 long iValue = -1;
88 if(parent) {
89 auto node = parent->first_node(nodeName);
90 if(node) wxString(node->value()).ToCLong(&iValue);
91 }
92 return (int)iValue;
93}
94
95int XMLParser::GetAttributeValueInt(rapidxml::xml_node<>* parent, const char* nodeName, const char* atrName)
96{
97 long iValue = -1;
98 if(parent) {
99 auto node = parent->first_node(nodeName);
100 if(node) {
101 auto atr = node->first_attribute(atrName);
102 if(atr) wxString(atr->value()).ToCLong(&iValue);
103 }
104 }
105 return (int)iValue;
106}
107
108int XMLParser::GetAttributeValueInt(rapidxml::xml_node<>* node, const char* atrName)
109{
110 long intValue;
111 auto atr = node->first_attribute(atrName);
112 if(!atr) return false;
113 wxString(atr->value()).ToCLong(&intValue);
114 return (int)intValue;
115}