liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lmu_movecommand.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 "rpg_movecommand.h"
8 #include "reader_struct.h"
9 
10 template <>
11 struct RawStruct<RPG::MoveCommand> {
12  static void ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t length);
13  static void WriteLcf(const RPG::MoveCommand& ref, LcfWriter& stream);
14  static int LcfSize(const RPG::MoveCommand& ref, LcfWriter& stream);
15  static void WriteXml(const RPG::MoveCommand& ref, XmlWriter& stream);
16  static void BeginXml(RPG::MoveCommand& ref, XmlReader& stream);
17 };
18 
19 template <>
20 struct RawStruct<std::vector<RPG::MoveCommand> > {
21  static void ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length);
22  static void WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
23  static int LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream);
24  static void WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream);
25  static void BeginXml(std::vector<RPG::MoveCommand>& ref, XmlReader& stream);
26 };
27 
31 void RawStruct<RPG::MoveCommand>::ReadLcf(RPG::MoveCommand& ref, LcfReader& stream, uint32_t /* length */) {
32  ref.command_id = stream.ReadInt();
33  switch (ref.command_id) {
35  stream.Read(ref.parameter_a);
36  break;
38  stream.Read(ref.parameter_a);
39  break;
41  stream.ReadString(ref.parameter_string, stream.ReadInt());
42  stream.Read(ref.parameter_a);
43  break;
45  stream.ReadString(ref.parameter_string, stream.ReadInt());
46  stream.Read(ref.parameter_a);
47  stream.Read(ref.parameter_b);
48  stream.Read(ref.parameter_c);
49  break;
50  }
51 }
52 
54  stream.WriteInt(ref.command_id);
55  switch (ref.command_id) {
57  stream.Write(ref.parameter_a);
58  break;
60  stream.Write(ref.parameter_a);
61  break;
63  stream.WriteInt(stream.Decode(ref.parameter_string).size());
64  stream.Write(ref.parameter_string);
65  stream.Write(ref.parameter_a);
66  break;
68  stream.WriteInt(stream.Decode(ref.parameter_string).size());
69  stream.Write(ref.parameter_string);
70  stream.Write(ref.parameter_a);
71  stream.Write(ref.parameter_b);
72  stream.Write(ref.parameter_c);
73  break;
74  }
75 }
76 
78  int result = 0;
79  result += LcfReader::IntSize(ref.command_id);
80  switch (ref.command_id) {
82  result += LcfReader::IntSize(ref.parameter_a);
83  break;
85  result += LcfReader::IntSize(ref.parameter_a);
86  break;
88  result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
89  result += stream.Decode(ref.parameter_string).size();
90  result += LcfReader::IntSize(ref.parameter_a);
91  break;
93  result += LcfReader::IntSize(stream.Decode(ref.parameter_string).size());
94  result += stream.Decode(ref.parameter_string).size();
95  result += LcfReader::IntSize(ref.parameter_a);
96  result += LcfReader::IntSize(ref.parameter_b);
97  result += LcfReader::IntSize(ref.parameter_c);
98  break;
99  }
100  return result;
101 }
102 
104  stream.BeginElement("MoveCommand");
105  stream.WriteNode<int>("command_id", ref.command_id);
106  switch (ref.command_id) {
108  stream.WriteNode<int>("parameter_a", ref.parameter_a);
109  break;
111  stream.WriteNode<int>("parameter_a", ref.parameter_a);
112  break;
114  stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
115  stream.WriteNode<int>("parameter_a", ref.parameter_a);
116  break;
118  stream.WriteNode<std::string>("parameter_string", ref.parameter_string);
119  stream.WriteNode<int>("parameter_a", ref.parameter_a);
120  stream.WriteNode<int>("parameter_b", ref.parameter_b);
121  stream.WriteNode<int>("parameter_c", ref.parameter_c);
122  break;
123  }
124  stream.EndElement("MoveCommand");
125 }
126 
128 private:
130  int* field;
132 public:
134  ref(ref), field(NULL), parameter_string(false) {}
135  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
136  if (strcmp(name, "command_id") == 0)
137  field = &ref.command_id;
138  else if (strcmp(name, "parameter_a") == 0)
139  field = &ref.parameter_a;
140  else if (strcmp(name, "parameter_b") == 0)
141  field = &ref.parameter_b;
142  else if (strcmp(name, "parameter_c") == 0)
143  field = &ref.parameter_c;
144  else if (strcmp(name, "parameter_string") == 0)
145  parameter_string = true;
146  else {
147  stream.Error("Unrecognized field '%s'", name);
148  field = NULL;
149  parameter_string = false;
150  }
151  }
152  void EndElement(XmlReader& /* stream */, const char* /* name */) {
153  field = NULL;
154  parameter_string = false;
155  }
156  void CharacterData(XmlReader& /* stream */, const std::string& data) {
157  if (field != NULL)
158  XmlReader::Read<int>(*field, data);
159  else if (parameter_string)
160  XmlReader::Read<std::string>(ref.parameter_string, data);
161  }
162 };
163 
165  stream.SetHandler(new WrapperXmlHandler("MoveCommand", new MoveCommandXmlHandler(ref)));
166 }
167 
171 void RawStruct<std::vector<RPG::MoveCommand> >::ReadLcf(std::vector<RPG::MoveCommand>& ref, LcfReader& stream, uint32_t length) {
172  unsigned long startpos = stream.Tell();
173  unsigned long endpos = startpos + length;
174  do {
175  RPG::MoveCommand command;
176  RawStruct<RPG::MoveCommand>::ReadLcf(command, stream, 0);
177  ref.push_back(command);
178  } while (stream.Tell() != endpos);
179 }
180 
181 void RawStruct<std::vector<RPG::MoveCommand> >::WriteLcf(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
182  std::vector<RPG::MoveCommand>::const_iterator it;
183  for (it = ref.begin(); it != ref.end(); it++)
185 }
186 
187 int RawStruct<std::vector<RPG::MoveCommand> >::LcfSize(const std::vector<RPG::MoveCommand>& ref, LcfWriter& stream) {
188  int result = 0;
189  std::vector<RPG::MoveCommand>::const_iterator it;
190  for (it = ref.begin(); it != ref.end(); it++)
191  result += RawStruct<RPG::MoveCommand>::LcfSize(*it, stream);
192  return result;
193 }
194 
195 void RawStruct<std::vector<RPG::MoveCommand> >::WriteXml(const std::vector<RPG::MoveCommand>& ref, XmlWriter& stream) {
196  std::vector<RPG::MoveCommand>::const_iterator it;
197  for (it = ref.begin(); it != ref.end(); it++)
199 }
200 
202 public:
203  MoveCommandVectorXmlHandler(std::vector<RPG::MoveCommand>& ref) : ref(ref) {}
204 
205  void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
206  if (strcmp(name, "MoveCommand") != 0)
207  stream.Error("Expecting %s but got %s", "MoveCommand", name);
208  ref.resize(ref.size() + 1);
209  RPG::MoveCommand& obj = ref.back();
210  stream.SetHandler(new MoveCommandXmlHandler(obj));
211  }
212 private:
213  std::vector<RPG::MoveCommand>& ref;
214 };
215 
216 void RawStruct<std::vector<RPG::MoveCommand> >::BeginXml(std::vector<RPG::MoveCommand>& obj, XmlReader& stream) {
217  stream.SetHandler(new MoveCommandVectorXmlHandler(obj));
218 }
RPG::Database data
Definition: data.cpp:11
MoveCommandXmlHandler(RPG::MoveCommand &ref)
void StartElement(XmlReader &stream, const char *name, const char **)
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:92
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
int ReadInt()
Definition: reader_lcf.cpp:82
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
MoveCommandVectorXmlHandler(std::vector< RPG::MoveCommand > &ref)
std::string parameter_string
static void WriteLcf(const T &ref, LcfWriter &stream)
static int LcfSize(const T &ref, LcfWriter &stream)
void StartElement(XmlReader &stream, const char *name, const char **)
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)
static void WriteXml(const T &ref, XmlWriter &stream)
void EndElement(XmlReader &, const char *)
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:157
RPG::MoveCommand & ref
std::vector< RPG::MoveCommand > & ref