GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/xml/libxml.h Lines: 1 1 100.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef UTILS_XML_LIBXML_H
25
#define UTILS_XML_LIBXML_H
26
27
#ifdef ENABLE_LIBXML
28
29
#define XML_INCLUDE_DEFINE
30
31
#include "enums/simpletypes/skiperror.h"
32
#include "enums/simpletypes/usevirtfs.h"
33
34
#include "utils/xml/libxml.inc"
35
36
#include "resources/resource.h"
37
38
#ifndef _GLIBCXX_STRING
39
#include <string>
40
#endif  // _GLIBCXX_STRING
41
42
#include "localconsts.h"
43
44
/**
45
 * XML helper functions.
46
 */
47
namespace XML
48
{
49
    /**
50
     * A helper class for parsing an XML document, which also cleans it up
51
     * again (RAII).
52
     */
53
    class Document final : public Resource
54
    {
55
        public:
56
            /**
57
             * Constructor that attempts to load the given file through the
58
             * resource manager. Logs errors.
59
             */
60
            Document(const std::string &filename,
61
                     const UseVirtFs useResman,
62
                     const SkipError skipError);
63
64
            /**
65
             * Constructor that attempts to load an XML document from memory.
66
             * Does not log errors.
67
             *
68
             * @param data the string to parse as XML
69
             * @param size the length of the string in bytes
70
             */
71
            Document(const char *const data, const int size);
72
73
            A_DELETE_COPY(Document)
74
75
            /**
76
             * Destructor. Frees the loaded XML file.
77
             */
78
            ~Document() override final;
79
80
            /**
81
             * Returns the root node of the document (or NULL if there was a
82
             * load error).
83
             */
84
            XmlNodePtr rootNode() A_WARN_UNUSED;
85
86
            bool isLoaded() const
87
7
            { return mDoc != nullptr; }
88
89
            bool isValid() const
90
            { return mIsValid; }
91
92
            static bool validateXml(const std::string &fileName);
93
94
        private:
95
            xmlDocPtr mDoc;
96
            bool mIsValid;
97
    };
98
99
    /**
100
     * Gets an floating point property from an XmlNodePtr.
101
     */
102
    float getFloatProperty(XmlNodeConstPtr node,
103
                           const char *const name,
104
                           float def) A_WARN_UNUSED;
105
106
    /**
107
     * Gets an double point property from an XmlNodePtr.
108
     */
109
    double getDoubleProperty(XmlNodeConstPtr node,
110
                            const char *const name,
111
                            double def) A_WARN_UNUSED;
112
113
    /**
114
     * Gets an integer property from an XmlNodePtr.
115
     */
116
    int getProperty(XmlNodeConstPtr node,
117
                    const char *const name,
118
                    int def) A_WARN_UNUSED;
119
120
    /**
121
     * Gets an integer property from an XmlNodePtr.
122
     */
123
    int getIntProperty(XmlNodeConstPtr node,
124
                       const char *const name,
125
                       int def,
126
                       const int min,
127
                       const int max) A_WARN_UNUSED;
128
129
    /**
130
     * Gets a string property from an XmlNodePtr.
131
     */
132
    std::string getProperty(XmlNodeConstPtr node,
133
                            const char *const name,
134
                            const std::string &def) A_WARN_UNUSED;
135
136
    /**
137
     * Gets a translated string property from an XmlNodePtr.
138
     */
139
    std::string langProperty(XmlNodeConstPtr node,
140
                             const char *const name,
141
                             const std::string &def) A_WARN_UNUSED;
142
143
    /**
144
     * Gets a boolean property from an XmlNodePtr.
145
     */
146
    bool getBoolProperty(XmlNodeConstPtr node,
147
                         const char *const name,
148
                         const bool def) A_WARN_UNUSED;
149
150
    /**
151
     * Finds the first child node with the given name
152
     */
153
    XmlNodePtr findFirstChildByName(XmlNodeConstPtrConst parent,
154
                                    const char *const name) A_WARN_UNUSED;
155
156
    void initXML();
157
158
    void cleanupXML();
159
}  // namespace XML
160
161
#define for_each_xml_child_node(var, parent) \
162
    for (XmlNodePtr var = parent->xmlChildrenNode; var; var = var->next)
163
164
#endif  // ENABLE_LIBXML
165
#endif  // UTILS_XML_LIBXML_H