liblcf
Loading...
Searching...
No Matches
lmt_reader.cpp
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) 2020 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 "lmt_reader.h"
15#include "lmt_chunks.h"
16#include "data.h"
17#include "reader_util.h"
18#include "reader_struct.h"
19
20bool LMT_Reader::Load(const std::string& filename, const std::string& encoding) {
21 std::ifstream stream(filename.c_str(), std::ios::binary);
22 if (!stream.is_open()) {
23 fprintf(stderr, "Failed to open LMT file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
24 return false;
25 }
26 return LMT_Reader::Load(stream, encoding);
27}
28
29bool LMT_Reader::Save(const std::string& filename, const std::string& encoding, SaveOpt opt) {
30 std::ofstream stream(filename.c_str(), std::ios::binary);
31 if (!stream.is_open()) {
32 fprintf(stderr, "Failed to open LMT file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
33 return false;
34 }
35 return LMT_Reader::Save(stream, encoding, opt);
36}
37
38bool LMT_Reader::SaveXml(const std::string& filename) {
39 std::ofstream stream(filename.c_str(), std::ios::binary);
40 if (!stream.is_open()) {
41 fprintf(stderr, "Failed to open LMT XML file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
42 return false;
43 }
44 return LMT_Reader::SaveXml(stream);
45}
46
47bool LMT_Reader::LoadXml(const std::string& filename) {
48 std::ifstream stream(filename.c_str(), std::ios::binary);
49 if (!stream.is_open()) {
50 fprintf(stderr, "Failed to open LMT XML file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
51 return false;
52 }
53 return LMT_Reader::LoadXml(stream);
54}
55
56bool LMT_Reader::Load(std::istream& filestream, const std::string &encoding) {
57 LcfReader reader(filestream, encoding);
58 if (!reader.IsOk()) {
59 LcfReader::SetError("Couldn't parse map tree file.\n");
60 return false;
61 }
62 std::string header;
63 reader.ReadString(header, reader.ReadInt());
64 if (header.length() != 10) {
65 LcfReader::SetError("This is not a valid RPG2000 map tree.\n");
66 return false;
67 }
68 if (header != "LcfMapTree") {
69 fprintf(stderr, "Warning: This header is not LcfMapTree and might not be a valid RPG2000 map tree.\n");
70 }
71 Data::treemap.lmt_header = std::move(header);
73 return true;
74}
75
76bool LMT_Reader::Save(std::ostream& filestream, const std::string &encoding, SaveOpt opt) {
77 LcfWriter writer(filestream, encoding);
78 if (!writer.IsOk()) {
79 LcfReader::SetError("Couldn't parse map tree file.\n");
80 return false;
81 }
82 std::string header;
83 if ( Data::treemap.lmt_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
84 header = "LcfMapTree";
85 } else {
87 }
88 writer.WriteInt(header.size());
89 writer.Write(header);
91 return true;
92}
93
94bool LMT_Reader::SaveXml(std::ostream& filestream) {
95 XmlWriter writer(filestream);
96 if (!writer.IsOk()) {
97 LcfReader::SetError("Couldn't parse map tree file.\n");
98 return false;
99 }
100 writer.BeginElement("LMT");
102 writer.EndElement("LMT");
103 return true;
104}
105
106bool LMT_Reader::LoadXml(std::istream& filestream) {
107 XmlReader reader(filestream);
108 if (!reader.IsOk()) {
109 LcfReader::SetError("Couldn't parse map tree file.\n");
110 return false;
111 }
113 reader.Parse();
114 return true;
115}
int ReadInt()
Definition: reader_lcf.cpp:84
bool IsOk() const
Definition: reader_lcf.cpp:192
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:186
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:278
void WriteInt(int val)
Definition: writer_lcf.cpp:51
bool IsOk() const
Definition: writer_lcf.cpp:125
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
std::string lmt_header
Definition: rpg_treemap.h:27
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
bool IsOk() const
Definition: reader_xml.cpp:55
void Parse()
Definition: reader_xml.cpp:67
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
bool IsOk() const
Definition: writer_xml.cpp:199
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
SaveOpt
Definition: lcf_saveopt.h:16
@ ePreserveHeader
RPG::TreeMap treemap
Definition: data.cpp:35
bool LoadXml(const std::string &filename)
Definition: lmt_reader.cpp:47
bool Save(const std::string &filename, const std::string &encoding, SaveOpt opt=SaveOpt::eNone)
Definition: lmt_reader.cpp:29
bool Load(const std::string &filename, const std::string &encoding)
Definition: lmt_reader.cpp:20
bool SaveXml(const std::string &filename)
Definition: lmt_reader.cpp:38