GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/xmlutils.cpp Lines: 42 61 68.9 %
Date: 2017-11-29 Branches: 59 130 45.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "utils/xmlutils.h"
22
23
#include "logger.h"
24
25
#include "utils/xml.h"
26
27
#include "debug.h"
28
29
2
void readXmlIntVector(const std::string &fileName,
30
                      const std::string &rootName,
31
                      const std::string &sectionName,
32
                      const std::string &itemName,
33
                      const std::string &attributeName,
34
                      STD_VECTOR<int> &arr,
35
                      const SkipError skipError)
36
{
37
4
    XML::Document doc(fileName, UseVirtFs_true, skipError);
38
2
    XmlNodeConstPtrConst rootNode = doc.rootNode();
39
40

4
    if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
41
    {
42
        logger->log("Error while loading %s!", fileName.c_str());
43
        return;
44
    }
45
46
8
    for_each_xml_child_node(sectionNode, rootNode)
47
    {
48

6
        if (!xmlNameEqual(sectionNode, sectionName.c_str()))
49
4
            continue;
50
24
        for_each_xml_child_node(childNode, sectionNode)
51
        {
52

22
            if (xmlNameEqual(childNode, itemName.c_str()))
53
            {
54
10
                const int val = XML::getProperty(childNode,
55
10
                    attributeName.c_str(), -1);
56
10
                if (val == -1)
57
                    continue;
58
10
                arr.push_back(val);
59
            }
60

12
            else if (xmlNameEqual(childNode, "include"))
61
            {
62
                const std::string name = XML::getProperty(
63
                    childNode, "name", "");
64
                if (!name.empty())
65
                {
66
                    readXmlIntVector(name,
67
                        rootName,
68
                        sectionName,
69
                        itemName,
70
                        attributeName,
71
                        arr,
72
                        skipError);
73
                }
74
            }
75
        }
76
    }
77
}
78
79
2
void readXmlStringMap(const std::string &fileName,
80
                      const std::string &rootName,
81
                      const std::string &sectionName,
82
                      const std::string &itemName,
83
                      const std::string &attributeKeyName,
84
                      const std::string &attributeValueName,
85
                      std::map<std::string, std::string> &arr,
86
                      const SkipError skipError)
87
{
88
4
    XML::Document doc(fileName, UseVirtFs_true, skipError);
89
2
    XmlNodeConstPtrConst rootNode = doc.rootNode();
90
91

4
    if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
92
    {
93
        logger->log("Error while loading %s!", fileName.c_str());
94
        return;
95
    }
96
97
120
    for_each_xml_child_node(sectionNode, rootNode)
98
    {
99

118
        if (!xmlNameEqual(sectionNode, sectionName.c_str()))
100
62
            continue;
101
112
        for_each_xml_child_node(childNode, sectionNode)
102
        {
103

56
            if (xmlNameEqual(childNode, itemName.c_str()))
104
            {
105
                const std::string key = XML::getProperty(childNode,
106

336
                    attributeKeyName.c_str(), "");
107
56
                if (key.empty())
108
                    continue;
109
                const std::string val = XML::getProperty(childNode,
110

336
                    attributeValueName.c_str(), "");
111
112
                arr[key] = val;
112
            }
113
            else if (xmlNameEqual(childNode, "include"))
114
            {
115
                const std::string name = XML::getProperty(
116
                    childNode, "name", "");
117
                if (!name.empty())
118
                {
119
                    readXmlStringMap(name,
120
                        rootName,
121
                        sectionName,
122
                        itemName,
123
                        attributeKeyName,
124
                        attributeValueName,
125
                        arr,
126
                        skipError);
127
                }
128
            }
129
        }
130
    }
131
}
132
133
2
void readXmlIntMap(const std::string &fileName,
134
                   const std::string &rootName,
135
                   const std::string &sectionName,
136
                   const std::string &itemName,
137
                   const std::string &attributeKeyName,
138
                   const std::string &attributeValueName,
139
                   std::map<int32_t, int32_t> &arr,
140
                   const SkipError skipError)
141
{
142
4
    XML::Document doc(fileName, UseVirtFs_true, skipError);
143
2
    XmlNodeConstPtrConst rootNode = doc.rootNode();
144
145

4
    if (rootNode == nullptr || !xmlNameEqual(rootNode, rootName.c_str()))
146
    {
147
        logger->log("Error while loading %s!", fileName.c_str());
148
        return;
149
    }
150
151
8
    for_each_xml_child_node(sectionNode, rootNode)
152
    {
153

6
        if (!xmlNameEqual(sectionNode, sectionName.c_str()))
154
4
            continue;
155
16
        for_each_xml_child_node(childNode, sectionNode)
156
        {
157

14
            if (xmlNameEqual(childNode, itemName.c_str()))
158
            {
159
                const std::string key = XML::getProperty(childNode,
160

36
                    attributeKeyName.c_str(), "");
161
6
                if (key.empty())
162
                    continue;
163
6
                const int32_t val = XML::getProperty(childNode,
164
6
                    attributeValueName.c_str(), 0);
165
18
                arr[atoi(key.c_str())] = val;
166
            }
167

8
            else if (xmlNameEqual(childNode, "include"))
168
            {
169
                const std::string name = XML::getProperty(
170
                    childNode, "name", "");
171
                if (!name.empty())
172
                {
173
                    readXmlIntMap(name,
174
                        rootName,
175
                        sectionName,
176
                        itemName,
177
                        attributeKeyName,
178
                        attributeValueName,
179
                        arr,
180
                        skipError);
181
                }
182
            }
183
        }
184
    }
185
}