liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lmt_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 "lmt_reader.h"
8 #include "lmt_chunks.h"
9 #include "data.h"
10 #include "reader_util.h"
11 #include "reader_struct.h"
12 
13 bool LMT_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 tree file.\n", filename.c_str());
17  return false;
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 tree.\n", filename.c_str());
23  return false;
24  }
25  if (header != "LcfMapTree") {
26  fprintf(stderr, "Warning: %s header is not LcfMapTree and might not be a valid RPG2000 map tree.\n", filename.c_str());
27  }
29  return true;
30 }
31 
32 bool LMT_Reader::Save(const std::string& filename, const std::string &encoding) {
33  LcfWriter writer(filename, encoding);
34  if (!writer.IsOk()) {
35  LcfReader::SetError("Couldn't find %s map tree file.\n", filename.c_str());
36  return false;
37  }
38  const std::string header("LcfMapTree");
39  writer.WriteInt(header.size());
40  writer.Write(header);
42  return true;
43 }
44 
45 bool LMT_Reader::SaveXml(const std::string& filename) {
46  XmlWriter writer(filename);
47  if (!writer.IsOk()) {
48  LcfReader::SetError("Couldn't find %s map tree file.\n", filename.c_str());
49  return false;
50  }
51  writer.BeginElement("LMT");
53  writer.EndElement("LMT");
54  return true;
55 }
56 
57 bool LMT_Reader::LoadXml(const std::string& filename) {
58  XmlReader reader(filename);
59  if (!reader.IsOk()) {
60  LcfReader::SetError("Couldn't open %s map tree file.\n", filename.c_str());
61  return false;
62  }
64  reader.Parse();
65  return true;
66 }
bool IsOk() const
Definition: writer_xml.cpp:199
bool SaveXml(const std::string &filename)
Definition: lmt_reader.cpp:45
bool Save(const std::string &filename, const std::string &encoding)
Definition: lmt_reader.cpp:32
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
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:234
bool LoadXml(const std::string &filename)
Definition: lmt_reader.cpp:57
bool Load(const std::string &filename, const std::string &encoding)
Definition: lmt_reader.cpp:13
RPG::TreeMap treemap
Definition: data.cpp:32
void Parse()
Definition: reader_xml.cpp:79