liblcf
lmt_reader.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of liblcf. Copyright (c) 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 <fstream>
11 #include <cerrno>
12 #include <cstring>
13 
14 #include "lcf/lmt/reader.h"
15 #include "lcf/lmt/chunks.h"
16 #include "lcf/reader_util.h"
17 #include "reader_struct.h"
18 
19 namespace lcf {
20 
21 std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(StringView filename, StringView encoding) {
22  std::ifstream stream(ToString(filename), std::ios::binary);
23  if (!stream.is_open()) {
24  fprintf(stderr, "Failed to open LMT file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
25  return nullptr;
26  }
27  return LMT_Reader::Load(stream, encoding);
28 }
29 
30 bool LMT_Reader::Save(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding, SaveOpt opt) {
31  std::ofstream stream(ToString(filename), std::ios::binary);
32  if (!stream.is_open()) {
33  fprintf(stderr, "Failed to open LMT file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
34  return false;
35  }
36  return LMT_Reader::Save(stream, tmap, engine, encoding, opt);
37 }
38 
39 bool LMT_Reader::SaveXml(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
40  std::ofstream stream(ToString(filename), std::ios::binary);
41  if (!stream.is_open()) {
42  fprintf(stderr, "Failed to open LMT XML file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
43  return false;
44  }
45  return LMT_Reader::SaveXml(stream, tmap, engine);
46 }
47 
48 std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(StringView filename) {
49  std::ifstream stream(ToString(filename), std::ios::binary);
50  if (!stream.is_open()) {
51  fprintf(stderr, "Failed to open LMT XML file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
52  return nullptr;
53  }
54  return LMT_Reader::LoadXml(stream);
55 }
56 
57 std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(std::istream& filestream, StringView encoding) {
58  LcfReader reader(filestream, ToString(encoding));
59  if (!reader.IsOk()) {
60  LcfReader::SetError("Couldn't parse map tree file.\n");
61  return nullptr;
62  }
63  std::string header;
64  reader.ReadString(header, reader.ReadInt());
65  if (header.length() != 10) {
66  LcfReader::SetError("This is not a valid RPG2000 map tree.\n");
67  return nullptr;
68  }
69  if (header != "LcfMapTree") {
70  fprintf(stderr, "Warning: This header is not LcfMapTree and might not be a valid RPG2000 map tree.\n");
71  }
72  auto tmap = std::make_unique<lcf::rpg::TreeMap>();
73  tmap->lmt_header = std::move(header);
74  TypeReader<rpg::TreeMap>::ReadLcf(*tmap, reader, 0);
75  return tmap;
76 }
77 
78 bool LMT_Reader::Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding, SaveOpt opt) {
79  LcfWriter writer(filestream, engine, ToString(encoding));
80  if (!writer.IsOk()) {
81  LcfReader::SetError("Couldn't parse map tree file.\n");
82  return false;
83  }
84  std::string header;
85  if ( tmap.lmt_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
86  header = "LcfMapTree";
87  } else {
88  header= tmap.lmt_header;
89  }
90  writer.WriteInt(header.size());
91  writer.Write(header);
92  TypeReader<rpg::TreeMap>::WriteLcf(tmap, writer);
93  return true;
94 }
95 
96 bool LMT_Reader::SaveXml(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
97  XmlWriter writer(filestream, engine);
98  if (!writer.IsOk()) {
99  LcfReader::SetError("Couldn't parse map tree file.\n");
100  return false;
101  }
102  writer.BeginElement("LMT");
103  TypeReader<rpg::TreeMap>::WriteXml(tmap, writer);
104  writer.EndElement("LMT");
105  return true;
106 }
107 
108 std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(std::istream& filestream) {
109  XmlReader reader(filestream);
110  if (!reader.IsOk()) {
111  LcfReader::SetError("Couldn't parse map tree file.\n");
112  return nullptr;
113  }
114  auto tmap = std::make_unique<lcf::rpg::TreeMap>();
115  reader.SetHandler(new RootXmlHandler<rpg::TreeMap>(*tmap, "LMT"));
116  reader.Parse();
117  return tmap;
118 }
119 
120 } //namespace lcf
Definition: dbarray.cpp:13