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