liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ldb_eventcommand.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 liblcf authors
3  * This file is released under the MIT License
4  * http://opensource.org/licenses/MIT
5  */
6 
7 #include <string>
8 #include <vector>
9 #include "reader_struct.h"
10 #include "rpg_eventcommand.h"
11 
12 template <>
13 struct RawStruct<RPG::EventCommand> {
14  static void ReadLcf(RPG::EventCommand& ref, LcfReader& stream, uint32_t length);
15  static void WriteLcf(const RPG::EventCommand& ref, LcfWriter& stream);
16  static int LcfSize(const RPG::EventCommand& ref, LcfWriter& stream);
17  static void WriteXml(const RPG::EventCommand& ref, XmlWriter& stream);
18  static void BeginXml(RPG::EventCommand& ref, XmlReader& stream);
19 };
20 
21 template <>
22 struct RawStruct<std::vector<RPG::EventCommand> > {
23  static void ReadLcf(std::vector<RPG::EventCommand>& ref, LcfReader& stream, uint32_t length);
24  static void WriteLcf(const std::vector<RPG::EventCommand>& ref, LcfWriter& stream);
25  static int LcfSize(const std::vector<RPG::EventCommand>& ref, LcfWriter& stream);
26  static void WriteXml(const std::vector<RPG::EventCommand>& ref, XmlWriter& stream);
27  static void BeginXml(std::vector<RPG::EventCommand>& ref, XmlReader& stream);
28 };
29 
33 void RawStruct<RPG::EventCommand>::ReadLcf(RPG::EventCommand& event_command, LcfReader& stream, uint32_t /* length */) {
34  stream.Read(event_command.code);
35  if (event_command.code != 0) {
36  stream.Read(event_command.indent);
37  stream.ReadString(event_command.string, stream.ReadInt());
38  for (int i = stream.ReadInt(); i > 0; i--) {
39  event_command.parameters.push_back(stream.ReadInt());
40  }
41  }
42 }
43 
45  stream.Write(event_command.code);
46  stream.Write(event_command.indent);
47  stream.WriteInt(stream.Decode(event_command.string).size());
48  stream.Write(event_command.string);
49  int count = event_command.parameters.size();
50  stream.Write(count);
51  for (int i = 0; i < count; i++)
52  stream.Write(event_command.parameters[i]);
53 }
54 
56  int result = 0;
57  result += LcfReader::IntSize(event_command.code);
58  result += LcfReader::IntSize(event_command.indent);
59  result += LcfReader::IntSize(event_command.string.size());
60  result += stream.Decode(event_command.string).size();
61  int count = event_command.parameters.size();
62  result += LcfReader::IntSize(count);
63  for (int i = 0; i < count; i++)
64  result += LcfReader::IntSize(event_command.parameters[i]);
65  return result;
66 }
67 
69  stream.BeginElement("EventCommand");
70  stream.WriteNode<int>("code", event_command.code);
71  stream.WriteNode<int>("indent", event_command.indent);
72  stream.WriteNode<std::string>("string", event_command.string);
73  stream.WriteNode<std::vector<int> >("parameters", event_command.parameters);
74  stream.EndElement("EventCommand");
75 }
76 
78 private:
80  enum {
86  } field;
87 public:
89  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
90  if (strcmp(name, "code") == 0)
91  field = Code;
92  else if (strcmp(name, "indent") == 0)
93  field = Indent;
94  else if (strcmp(name, "string") == 0)
95  field = String;
96  else if (strcmp(name, "parameters") == 0)
97  field = Parameters;
98  else {
99  stream.Error("Unrecognized field '%s'", name);
100  field = None;
101  }
102  }
103  void EndElement(XmlReader& /* stream */, const char* /* name */) {
104  field = None;
105  }
106  void CharacterData(XmlReader& /* stream */, const std::string& data) {
107  switch (field) {
108  case None:
109  break;
110  case Code:
111  XmlReader::Read<int>(ref.code, data);
112  break;
113  case Indent:
114  XmlReader::Read<int>(ref.indent, data);
115  break;
116  case String:
117  XmlReader::Read<std::string>(ref.string, data);
118  break;
119  case Parameters:
120  XmlReader::Read<std::vector<int> >(ref.parameters, data);
121  break;
122  }
123  }
124 };
125 
127  stream.SetHandler(new WrapperXmlHandler("EventCommand", new EventCommandXmlHandler(ref)));
128 }
129 
134  std::vector<RPG::EventCommand>& event_commands, LcfReader& stream, uint32_t length) {
135  // Event Commands is a special array
136  // Has no size information. Is terminated by 4 times 0x00.
137  unsigned long startpos = stream.Tell();
138  unsigned long endpos = startpos + length;
139  for (;;) {
140  uint8_t ch;
141  stream.Read(ch);
142  if (ch == 0) {
143  stream.Seek(3, LcfReader::FromCurrent);
144  break;
145  }
146  stream.Ungetch(ch);
147  RPG::EventCommand command;
148  RawStruct<RPG::EventCommand>::ReadLcf(command, stream, 0);
149  event_commands.push_back(command);
150  }
151  assert(stream.Tell() == endpos);
152 }
153 
154 void RawStruct<std::vector<RPG::EventCommand> >::WriteLcf(const std::vector<RPG::EventCommand>& event_commands, LcfWriter& stream) {
155  int count = event_commands.size();
156  for (int i = 0; i < count; i++)
157  RawStruct<RPG::EventCommand>::WriteLcf(event_commands[i], stream);
158  for (int i = 0; i < 4; i++)
159  stream.WriteInt(0);
160 }
161 
162 int RawStruct<std::vector<RPG::EventCommand> >::LcfSize(const std::vector<RPG::EventCommand>& event_commands, LcfWriter& stream) {
163  int result = 0;
164  int count = event_commands.size();
165  for (int i = 0; i < count; i++)
166  result += RawStruct<RPG::EventCommand>::LcfSize(event_commands[i], stream);
167  result += 4;
168  return result;
169 }
170 
171 void RawStruct<std::vector<RPG::EventCommand> >::WriteXml(const std::vector<RPG::EventCommand>& event_commands, XmlWriter& stream) {
172  std::vector<RPG::EventCommand>::const_iterator it;
173  for (it = event_commands.begin(); it != event_commands.end(); it++)
175 }
176 
178 public:
179  EventCommandVectorXmlHandler(std::vector<RPG::EventCommand>& ref) : ref(ref) {}
180 
181  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
182  if (strcmp(name, "EventCommand") != 0)
183  stream.Error("Expecting %s but got %s", "EventCommand", name);
184  ref.resize(ref.size() + 1);
185  RPG::EventCommand& obj = ref.back();
186  stream.SetHandler(new EventCommandXmlHandler(obj));
187  }
188 private:
189  std::vector<RPG::EventCommand>& ref;
190 };
191 
192 void RawStruct<std::vector<RPG::EventCommand> >::BeginXml(std::vector<RPG::EventCommand>& obj, XmlReader& stream) {
193  stream.SetHandler(new EventCommandVectorXmlHandler(obj));
194 }
RPG::Database data
Definition: data.cpp:11
EventCommandXmlHandler(RPG::EventCommand &ref)
void Seek(size_t pos, SeekMode mode=FromStart)
Definition: reader_lcf.cpp:178
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:92
RPG::EventCommand & ref
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:163
std::string Decode(const std::string &str_to_encode)
Definition: writer_lcf.cpp:121
void WriteInt(int val)
Definition: writer_lcf.cpp:58
void Read(void *ptr, size_t size, size_t nmemb)
Definition: reader_lcf.cpp:52
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:71
STL namespace.
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:33
void StartElement(XmlReader &stream, const char *name, const char **)
int ReadInt()
Definition: reader_lcf.cpp:82
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
std::vector< RPG::EventCommand > & ref
static void WriteLcf(const T &ref, LcfWriter &stream)
static int LcfSize(const T &ref, LcfWriter &stream)
void EndElement(XmlReader &, const char *)
std::vector< int > parameters
EventCommandVectorXmlHandler(std::vector< RPG::EventCommand > &ref)
bool Ungetch(uint8_t ch)
Definition: reader_lcf.cpp:198
static int IntSize(unsigned int x)
Definition: reader_lcf.cpp:255
Definition: rpg_actor.h:23
static void BeginXml(T &ref, XmlReader &stream)
uint32_t Tell()
Definition: reader_lcf.cpp:194
void CharacterData(XmlReader &, const std::string &data)
enum EventCommandXmlHandler::@0 field
static void WriteXml(const T &ref, XmlWriter &stream)
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:157
void StartElement(XmlReader &stream, const char *name, const char **)