-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathreader_xml.h
139 lines (114 loc) · 2.61 KB
/
reader_xml.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* This file is part of liblcf. Copyright (c) 2020 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#ifndef LCF_READER_XML_H
#define LCF_READER_XML_H
#include "lcf/config.h"
#include "lcf/dbarray.h"
#include <string>
#include <vector>
#include <cstdio>
#if LCF_SUPPORT_XML
# include <expat.h>
#endif
#include <stdint.h>
#include "lcf/reader_util.h"
namespace lcf {
/**
* XmlHandler abstract base class (forward reference).
*/
class XmlHandler;
/**
* XmlReader class template.
*/
class XmlReader {
public:
/**
* Constructs a new File Reader.
*
* @param filestream already opened filestream.
*/
XmlReader(std::istream& filestream);
/**
* Destructor. Closes the opened file.
*/
~XmlReader();
/**
* Checks if the file is readable and if no error occured.
*
* @return true if the stream is okay.
*/
bool IsOk() const;
/**
* Reports a parsing error.
*/
void Error(const char* fmt, ...);
/**
* Parses the XML file.
*/
void Parse();
/**
* Changes the handler.
*/
void SetHandler(XmlHandler* handler);
/**
* Parses a primitive type.
*/
template <class T>
static void Read(T& ref, const std::string& data);
/**
* Parses a vector of primitive type.
*/
template <class T>
static void ReadVector(std::vector<T>& ref, const std::string& data);
/**
* Parses a vector of primitive type.
*/
template <class T>
static void ReadVector(DBArray<T>& ref, const std::string& data);
/**
* Start element callback.
*/
void StartElement(const char* name, const char** atts);
/**
* Character data callback.
*/
void CharacterData(const char* s, int len);
/**
* End element callback.
*/
void EndElement(const char* name);
protected:
/** File-stream managed by this Reader. */
std::istream& stream;
/** Expat XML parser object. */
#if LCF_SUPPORT_XML
XML_Parser parser;
#else
void* parser;
#endif
/** Nesting depth. */
int nesting;
/** Handler stack. */
std::vector<XmlHandler*> handlers;
/** Text buffer. */
std::string buffer;
};
/**
* XmlHandler abstract base class.
*/
class XmlHandler {
public:
virtual void StartElement(XmlReader& /* reader */, const char* /* name */, const char** /* atts */) {}
virtual void CharacterData(XmlReader& /* reader */, const std::string& /* data */) {}
virtual void EndElement(XmlReader& /* reader */, const char* /* name */) {}
XmlHandler() {}
virtual ~XmlHandler() {}
};
} //namespace lcf
#endif