summaryrefslogtreecommitdiffstats
path: root/Project/XMLParser.cpp
blob: 0d1588e8ae197cb581b5aba0d01f1e2d08dc7921 (plain)
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
/*
 *  Copyright (C) 2018  Thales Lima Oliveira <thales@ufu.br>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "XMLParser.h"

XMLParser::XMLParser() {}

XMLParser::~XMLParser() {}

rapidxml::xml_node<>* XMLParser::AppendNode(rapidxml::xml_document<>& doc,
                                            rapidxml::xml_node<>* parentNode,
                                            const char* name,
                                            rapidxml::node_type nodeType)
{
    rapidxml::xml_node<>* node = doc.allocate_node(nodeType, name);
    parentNode->append_node(node);
    return node;
}

void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, wxString value)
{
    node->value(doc.allocate_string(value.mb_str()));
}

void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, int value)
{
    node->value(doc.allocate_string(wxString::Format("%d", value).mb_str()));
}

void XMLParser::SetNodeValue(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* node, double value)
{
    node->value(doc.allocate_string(wxString::FromCDouble(value, 13).mb_str()));
}

void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
                                 rapidxml::xml_node<>* node,
                                 const char* atrName,
                                 wxString value)
{
    node->append_attribute(doc.allocate_attribute(atrName, doc.allocate_string(value.mb_str())));
}

void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
                                 rapidxml::xml_node<>* node,
                                 const char* atrName,
                                 int value)
{
    node->append_attribute(
        doc.allocate_attribute(atrName, doc.allocate_string(wxString::Format("%d", value).mb_str())));
}

void XMLParser::SetNodeAttribute(rapidxml::xml_document<>& doc,
                                 rapidxml::xml_node<>* node,
                                 const char* atrName,
                                 double value)
{
    node->append_attribute(
        doc.allocate_attribute(atrName, doc.allocate_string(wxString::FromCDouble(value, 13).mb_str())));
}

double XMLParser::GetNodeValueDouble(rapidxml::xml_node<>* parent, const char* nodeName)
{
    double dValue = 0.0;
    if(parent) {
        auto node = parent->first_node(nodeName);
        if(node) wxString(node->value()).ToCDouble(&dValue);
    }
    return dValue;
}

int XMLParser::GetNodeValueInt(rapidxml::xml_node<>* parent, const char* nodeName)
{
    long iValue = -1;
    if(parent) {
        auto node = parent->first_node(nodeName);
        if(node) wxString(node->value()).ToCLong(&iValue);
    }
    return (int)iValue;
}

int XMLParser::GetAttributeValueInt(rapidxml::xml_node<>* parent, const char* nodeName, const char* atrName)
{
    long iValue = -1;
    if(parent) {
        auto node = parent->first_node(nodeName);
        if(node) {
            auto atr = node->first_attribute(atrName);
            if(atr) wxString(atr->value()).ToCLong(&iValue);
        }
    }
    return (int)iValue;
}

int XMLParser::GetAttributeValueInt(rapidxml::xml_node<>* node, const char* atrName)
{
    long intValue;
    auto atr = node->first_attribute(atrName);
    if(!atr) return false;
    wxString(atr->value()).ToCLong(&intValue);
    return (int)intValue;
}