liblcf
lmt_rect.cpp
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 #include "lmt_reader.h"
11 #include "lmt_chunks.h"
12 #include "reader_struct.h"
13 
14 template <>
15 struct RawStruct<RPG::Rect> {
16  static void ReadLcf(RPG::Rect& ref, LcfReader& stream, uint32_t length);
17  static void WriteLcf(const RPG::Rect& ref, LcfWriter& stream);
18  static int LcfSize(const RPG::Rect& ref, LcfWriter& stream);
19  static void WriteXml(const RPG::Rect& ref, XmlWriter& stream);
20  static void BeginXml(RPG::Rect& ref, XmlReader& stream);
21 };
22 
26 void RawStruct<RPG::Rect>::ReadLcf(RPG::Rect& ref, LcfReader& stream, uint32_t length) {
27  assert(length == 16);
28  (void)length;
29  stream.Read(ref.l);
30  stream.Read(ref.t);
31  stream.Read(ref.r);
32  stream.Read(ref.b);
33 }
34 
36  stream.Write(ref.l);
37  stream.Write(ref.t);
38  stream.Write(ref.r);
39  stream.Write(ref.b);
40 }
41 
42 int RawStruct<RPG::Rect>::LcfSize(const RPG::Rect& /* ref */, LcfWriter& /* stream */) {
43  return 4 * 4;
44 }
45 
47  stream.BeginElement("Rect");
48  stream.WriteNode<int32_t>("l", ref.l);
49  stream.WriteNode<int32_t>("t", ref.t);
50  stream.WriteNode<int32_t>("r", ref.r);
51  stream.WriteNode<int32_t>("b", ref.b);
52  stream.EndElement("Rect");
53 }
54 
55 class RectXmlHandler : public XmlHandler {
56 private:
58  uint32_t* field;
59 public:
61  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
62  if (strcmp(name, "l") == 0)
63  field = &ref.l;
64  else if (strcmp(name, "t") == 0)
65  field = &ref.t;
66  else if (strcmp(name, "r") == 0)
67  field = &ref.r;
68  else if (strcmp(name, "b") == 0)
69  field = &ref.b;
70  else {
71  stream.Error("Unrecognized field '%s'", name);
72  field = NULL;
73  }
74  }
75  void EndElement(XmlReader& /* stream */, const char* /* name */) {
76  field = NULL;
77  }
78  void CharacterData(XmlReader& /* stream */, const std::string& data) {
79  if (field != NULL)
81  }
82 };
83 
85  stream.SetHandler(new WrapperXmlHandler("Rect", new RectXmlHandler(ref)));
86 }
void StartElement(XmlReader &stream, const char *name, const char **)
Definition: lmt_rect.cpp:61
RPG::Database data
Definition: data.cpp:14
void EndElement(XmlReader &, const char *)
Definition: lmt_rect.cpp:75
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
RPG::Rect & ref
Definition: lmt_rect.cpp:57
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
static void ReadLcf(T &ref, LcfReader &stream, uint32_t length)
void Error(const char *fmt,...)
Definition: reader_xml.cpp:59
RectXmlHandler(RPG::Rect &ref)
Definition: lmt_rect.cpp:60
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
static void WriteLcf(const T &ref, LcfWriter &stream)
static int LcfSize(const T &ref, LcfWriter &stream)
uint32_t b
Definition: rpg_rect.h:27
uint32_t r
Definition: rpg_rect.h:26
Definition: rpg_actor.h:26
static void BeginXml(T &ref, XmlReader &stream)
uint32_t t
Definition: rpg_rect.h:25
uint32_t * field
Definition: lmt_rect.cpp:58
uint32_t l
Definition: rpg_rect.h:24
void CharacterData(XmlReader &, const std::string &data)
Definition: lmt_rect.cpp:78
static void WriteXml(const T &ref, XmlWriter &stream)
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:155
static void Read(T &ref, const std::string &data)