liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lmu_reader.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 "lmu_reader.h"
8 #include "lmu_chunks.h"
9 #include "reader_lcf.h"
10 #include "reader_util.h"
11 #include "reader_struct.h"
12 
13 std::auto_ptr<RPG::Map> LMU_Reader::Load(const std::string& filename, const std::string& encoding) {
14  LcfReader reader(filename, encoding);
15  if (!reader.IsOk()) {
16  LcfReader::SetError("Couldn't find %s map file.\n", filename.c_str());
17  return std::auto_ptr<RPG::Map>(NULL);
18  }
19  std::string header;
20  reader.ReadString(header, reader.ReadInt());
21  if (header.length() != 10) {
22  LcfReader::SetError("%s is not a valid RPG2000 map.\n", filename.c_str());
23  return std::auto_ptr<RPG::Map>(NULL);
24  }
25  if (header != "LcfMapUnit") {
26  fprintf(stderr, "Warning: %s header is not LcfMapUnit and might not be a valid RPG2000 map.\n", filename.c_str());
27  }
28 
29  RPG::Map* map = new RPG::Map();
30  Struct<RPG::Map>::ReadLcf(*map, reader);
31  return std::auto_ptr<RPG::Map>(map);
32 }
33 
34 bool LMU_Reader::Save(const std::string& filename, const RPG::Map& map, const std::string& encoding) {
35  LcfWriter writer(filename, encoding);
36  if (!writer.IsOk()) {
37  LcfReader::SetError("Couldn't find %s map file.\n", filename.c_str());
38  return false;
39  }
40  const std::string header("LcfMapUnit");
41  writer.WriteInt(header.size());
42  writer.Write(header);
43 
44  Struct<RPG::Map>::WriteLcf(map, writer);
45  return true;
46 }
47 
48 bool LMU_Reader::SaveXml(const std::string& filename, const RPG::Map& map) {
49  XmlWriter writer(filename);
50  if (!writer.IsOk()) {
51  LcfReader::SetError("Couldn't find %s map file.\n", filename.c_str());
52  return false;
53  }
54  writer.BeginElement("LMU");
55  Struct<RPG::Map>::WriteXml(map, writer);
56  writer.EndElement("LMU");
57  return true;
58 }
59 
60 std::auto_ptr<RPG::Map> LMU_Reader::LoadXml(const std::string& filename) {
61  XmlReader reader(filename);
62  if (!reader.IsOk()) {
63  LcfReader::SetError("Couldn't find %s map file.\n", filename.c_str());
64  return std::auto_ptr<RPG::Map>(NULL);
65  }
66 
67  RPG::Map* map = new RPG::Map();
68  reader.SetHandler(new RootXmlHandler<RPG::Map>(*map, "LMU"));
69  reader.Parse();
70  return std::auto_ptr<RPG::Map>(map);
71 }
72 
bool IsOk() const
Definition: writer_xml.cpp:199
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:92
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:163
void WriteInt(int val)
Definition: writer_lcf.cpp:58
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
bool IsOk() const
Definition: writer_lcf.cpp:117
bool IsOk() const
Definition: reader_xml.cpp:67
bool IsOk() const
Definition: reader_lcf.cpp:170
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:33
int ReadInt()
Definition: reader_lcf.cpp:82
bool SaveXml(const std::string &filename, const RPG::Map &map)
Definition: lmu_reader.cpp:48
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
std::auto_ptr< RPG::Map > Load(const std::string &filename, const std::string &encoding)
Definition: lmu_reader.cpp:13
static void ReadLcf(S &obj, LcfReader &stream)
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:234
static void WriteLcf(const S &obj, LcfWriter &stream)
void Parse()
Definition: reader_xml.cpp:79
std::auto_ptr< RPG::Map > LoadXml(const std::string &filename)
Definition: lmu_reader.cpp:60
bool Save(const std::string &filename, const RPG::Map &map, const std::string &encoding)
Definition: lmu_reader.cpp:34
static void WriteXml(const S &obj, XmlWriter &stream)