Mana
Loading...
Searching...
No Matches
xml.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "utils/xml.h"
23
24#include <libxml/xmlerror.h>
25
26#include "log.h"
27
28#include "utils/filesystem.h"
29
30namespace XML
31{
33 {
34 std::string file;
35 };
36
37#if LIBXML_VERSION >= 21200
38 static void xmlLogger(void *ctx, const xmlError *error)
39#else
40 static void xmlLogger(void *ctx, xmlErrorPtr error)
41#endif
42 {
43 auto *context = static_cast<XMLContext*>(ctx);
44
45 if (context)
46 Log::info("Error in XML file '%s' on line %d",
47 context->file.c_str(), error->line);
48 else
49 Log::info("Error in unknown XML file on line %d",
50 error->line);
51
52 Log::info("%s", error->message);
53
54 // No need to keep errors around
55 xmlCtxtResetLastError(error->ctxt);
56 }
57
58 Document::Document(const std::string &filename, bool useResman):
59 mDoc(nullptr)
60 {
61 XMLContext ctx;
62 ctx.file = filename;
63 xmlSetStructuredErrorFunc(&ctx, xmlLogger);
64
65 size_t size;
66 char *data = nullptr;
67
68 if (useResman)
69 data = (char *) FS::loadFile(filename, size);
70 else
71 data = (char *) SDL_LoadFile(filename.c_str(), &size);
72
73 if (data)
74 {
75 mDoc = xmlParseMemory(data, size);
76 SDL_free(data);
77
78 if (!mDoc)
79 Log::info("Error parsing XML file %s", filename.c_str());
80 }
81 else
82 {
83 Log::info("Error loading %s: %s", filename.c_str(), SDL_GetError());
84 }
85
86 xmlSetStructuredErrorFunc(nullptr, xmlLogger);
87 }
88
90 {
91 if (mDoc)
92 xmlFreeDoc(mDoc);
93 }
94
95 void init()
96 {
97 // Initialize libxml2 and check for potential ABI mismatches between
98 // compiled version and the shared library actually used.
99 xmlInitParser();
100 LIBXML_TEST_VERSION;
101
102 // Handle libxml2 error messages
103 xmlSetStructuredErrorFunc(nullptr, xmlLogger);
104 }
105
106
107 Writer::Writer(const std::string &fileName)
108 {
109 mWriter = xmlNewTextWriterFilename(fileName.c_str(), 0);
110 if (!mWriter)
111 {
112 Log::info("Error creating XML writer for file %s", fileName.c_str());
113 return;
114 }
115
116 xmlTextWriterSetIndent(mWriter, 1);
117 xmlTextWriterStartDocument(mWriter, nullptr, nullptr, nullptr);
118 }
119
121 {
122 if (!mWriter)
123 return;
124
125 xmlTextWriterEndDocument(mWriter);
126 xmlFreeTextWriter(mWriter);
127 }
128
129} // namespace XML
~Document()
Destructor.
Definition xml.cpp:89
Document(const std::string &filename, bool useResman=true)
Constructor that attempts to load the given file through the resource manager.
Definition xml.cpp:58
xmlDocPtr mDoc
Definition xml.h:210
Writer(const std::string &fileName)
Definition xml.cpp:107
xmlTextWriterPtr mWriter
Definition xml.h:252
void * loadFile(const std::string &path, size_t &datasize)
Definition filesystem.h:288
void info(const char *log_text,...) LOG_PRINTF_ATTR
XML helper functions.
Definition xml.cpp:31
void init()
Initializes the XML engine.
Definition xml.cpp:95
std::string file
Definition xml.cpp:34