liblcf
reader_xml.h
Go to the documentation of this file.
1 /*
2  * This file is part of liblcf. Copyright (c) 2020 liblcf authors.
3  * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4  *
5  * liblcf is Free/Libre Open Source Software, released under the MIT License.
6  * For the full copyright and license information, please view the COPYING
7  * file that was distributed with this source code.
8  */
9 
10 #ifndef LCF_READER_XML_H
11 #define LCF_READER_XML_H
12 
13 #include <string>
14 #include <vector>
15 #include <cstdio>
16 #if defined(LCF_SUPPORT_XML)
17 # include <expat.h>
18 #endif
19 #include <stdint.h>
20 #include "lcf_options.h"
21 #include "reader_util.h"
22 
26 class XmlHandler;
27 
31 class XmlReader {
32 
33 public:
39  XmlReader(std::istream& filestream);
40 
44  ~XmlReader();
45 
51  bool IsOk() const;
52 
56  void Error(const char* fmt, ...);
57 
61  void Parse();
62 
66  void SetHandler(XmlHandler* handler);
67 
71  template <class T>
72  static void Read(T& ref, const std::string& data);
73 
77  template <class T>
78  static void ReadVector(std::vector<T>& ref, const std::string& data);
79 
83  void StartElement(const char* name, const char** atts);
84 
88  void CharacterData(const char* s, int len);
89 
93  void EndElement(const char* name);
94 
95 protected:
97  std::istream& stream;
99 #if defined(LCF_SUPPORT_XML)
100  XML_Parser parser;
101 #else
102  void* parser;
103 #endif
104 
105  int nesting;
107  std::vector<XmlHandler*> handlers;
109  std::string buffer;
110 
111 };
112 
116 class XmlHandler {
117 
118 public:
119  virtual void StartElement(XmlReader& /* reader */, const char* /* name */, const char** /* atts */) {}
120  virtual void CharacterData(XmlReader& /* reader */, const std::string& /* data */) {}
121  virtual void EndElement(XmlReader& /* reader */, const char* /* name */) {}
123  virtual ~XmlHandler() {}
124 
125 };
126 
127 #endif
XmlReader(std::istream &filestream)
Definition: reader_xml.cpp:32
RPG::Database data
Definition: data.cpp:14
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
virtual ~XmlHandler()
Definition: reader_xml.h:123
void Error(const char *fmt,...)
Definition: reader_xml.cpp:59
std::istream & stream
Definition: reader_xml.h:97
virtual void CharacterData(XmlReader &, const std::string &)
Definition: reader_xml.h:120
virtual void StartElement(XmlReader &, const char *, const char **)
Definition: reader_xml.h:119
static void ReadVector(std::vector< T > &ref, const std::string &data)
Definition: reader_xml.cpp:182
std::string buffer
Definition: reader_xml.h:109
void StartElement(const char *name, const char **atts)
Definition: reader_xml.cpp:84
void CharacterData(const char *s, int len)
Definition: reader_xml.cpp:91
std::vector< XmlHandler * > handlers
Definition: reader_xml.h:107
bool IsOk() const
Definition: reader_xml.cpp:55
void * parser
Definition: reader_xml.h:102
void EndElement(const char *name)
Definition: reader_xml.cpp:95
void Parse()
Definition: reader_xml.cpp:67
virtual void EndElement(XmlReader &, const char *)
Definition: reader_xml.h:121
int nesting
Definition: reader_xml.h:105
static void Read(T &ref, const std::string &data)