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 }
LcfReader::Read
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:47
XmlReader::Read
static void Read(T &ref, const std::string &data)
RectXmlHandler::ref
RPG::Rect & ref
Definition: lmt_rect.cpp:57
RawStruct::ReadLcf
static void ReadLcf(T &ref, LcfReader &stream, uint32_t length)
RawStruct::WriteLcf
static void WriteLcf(const T &ref, LcfWriter &stream)
XmlReader::Error
void Error(const char *fmt,...)
Definition: reader_xml.cpp:59
XmlHandler
Definition: reader_xml.h:116
XmlReader
Definition: reader_xml.h:31
WrapperXmlHandler
Definition: reader_struct.h:705
RPG::Rect::r
uint32_t r
Definition: rpg_rect.h:26
XmlReader::SetHandler
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
RawStruct::BeginXml
static void BeginXml(T &ref, XmlReader &stream)
RPG
Definition: rpg_actor.h:26
XmlWriter::EndElement
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
reader_struct.h
LcfWriter
Definition: writer_lcf.h:27
RectXmlHandler::RectXmlHandler
RectXmlHandler(RPG::Rect &ref)
Definition: lmt_rect.cpp:60
lmt_reader.h
XmlWriter
Definition: writer_xml.h:22
RPG::Rect
Definition: rpg_rect.h:22
LcfWriter::Write
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
RPG::Rect::l
uint32_t l
Definition: rpg_rect.h:24
lmt_chunks.h
RawStruct::WriteXml
static void WriteXml(const T &ref, XmlWriter &stream)
RectXmlHandler::CharacterData
void CharacterData(XmlReader &, const std::string &data)
Definition: lmt_rect.cpp:78
RectXmlHandler
Definition: lmt_rect.cpp:55
RawStruct
Definition: reader_struct.h:97
Data::data
RPG::Database data
Definition: data.cpp:14
RawStruct::LcfSize
static int LcfSize(const T &ref, LcfWriter &stream)
XmlWriter::WriteNode
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:155
RectXmlHandler::field
uint32_t * field
Definition: lmt_rect.cpp:58
RPG::Rect::b
uint32_t b
Definition: rpg_rect.h:27
LcfReader
Definition: reader_lcf.h:35
RPG::Rect::t
uint32_t t
Definition: rpg_rect.h:25
RectXmlHandler::StartElement
void StartElement(XmlReader &stream, const char *name, const char **)
Definition: lmt_rect.cpp:61
XmlWriter::BeginElement
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
RectXmlHandler::EndElement
void EndElement(XmlReader &, const char *)
Definition: lmt_rect.cpp:75