liblcf
ldb_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 "ldb_reader.h"
15 #include "ldb_chunks.h"
16 #include "data.h"
17 #include "reader_util.h"
18 #include "reader_struct.h"
19 
21  ++db.system.save_count;
22 }
23 
24 bool LDB_Reader::Load(const std::string& filename, const std::string& encoding) {
25  std::ifstream stream(filename.c_str(), std::ios::binary);
26  if (!stream.is_open()) {
27  fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
28  return false;
29  }
30  return LDB_Reader::Load(stream, encoding);
31 }
32 
33 bool LDB_Reader::Save(const std::string& filename, const std::string& encoding, SaveOpt opt) {
34  std::ofstream stream(filename.c_str(), std::ios::binary);
35  if (!stream.is_open()) {
36  fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
37  return false;
38  }
39  return LDB_Reader::Save(stream, encoding, opt);
40 }
41 
42 bool LDB_Reader::SaveXml(const std::string& filename) {
43  std::ofstream stream(filename.c_str(), std::ios::binary);
44  if (!stream.is_open()) {
45  fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
46  return false;
47  }
48  return LDB_Reader::SaveXml(stream);
49 }
50 
51 bool LDB_Reader::LoadXml(const std::string& filename) {
52  std::ifstream stream(filename.c_str(), std::ios::binary);
53  if (!stream.is_open()) {
54  fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
55  return false;
56  }
57  return LDB_Reader::LoadXml(stream);
58 }
59 
60 bool LDB_Reader::Load(std::istream& filestream, const std::string& encoding) {
61  LcfReader reader(filestream, encoding);
62  if (!reader.IsOk()) {
63  LcfReader::SetError("Couldn't parse database file.\n");
64  return false;
65  }
66  std::string header;
67  reader.ReadString(header, reader.ReadInt());
68  if (header.length() != 11) {
69  LcfReader::SetError("This is not a valid RPG2000 database.\n");
70  return false;
71  }
72  if (header != "LcfDataBase") {
73  fprintf(stderr, "Warning: This header is not LcfDataBase and might not be a valid RPG2000 database.\n");
74  }
75  Data::data.ldb_header = header;
77 
78  // Delayed initialization of some actor fields because they are engine
79  // dependent
80  std::vector<RPG::Actor>::iterator it;
81  for (it = Data::actors.begin(); it != Data::actors.end(); ++it) {
82  (*it).Setup();
83  }
84 
85  return true;
86 }
87 
88 bool LDB_Reader::Save(std::ostream& filestream, const std::string& encoding, SaveOpt opt) {
89  LcfWriter writer(filestream, encoding);
90  if (!writer.IsOk()) {
91  LcfReader::SetError("Couldn't parse database file.\n");
92  return false;
93  }
94  std::string header;
95  if ( Data::data.ldb_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
96  header = "LcfDataBase";
97  } else {
98  header= Data::data.ldb_header;
99  }
100  writer.WriteInt(header.size());
101  writer.Write(header);
103  return true;
104 }
105 
106 bool LDB_Reader::SaveXml(std::ostream& filestream) {
107  XmlWriter writer(filestream);
108  if (!writer.IsOk()) {
109  LcfReader::SetError("Couldn't parse database file.\n");
110  return false;
111  }
112  writer.BeginElement("LDB");
114  writer.EndElement("LDB");
115  return true;
116 }
117 
118 bool LDB_Reader::LoadXml(std::istream& filestream) {
119  XmlReader reader(filestream);
120  if (!reader.IsOk()) {
121  LcfReader::SetError("Couldn't parse database file.\n");
122  return false;
123  }
125  reader.Parse();
126  return true;
127 }
RPG::Database data
Definition: data.cpp:14
bool IsOk() const
Definition: writer_xml.cpp:199
void SetHandler(XmlHandler *handler)
Definition: reader_xml.cpp:80
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:161
void WriteInt(int val)
Definition: writer_lcf.cpp:51
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
int32_t save_count
Definition: rpg_system.h:222
void PrepareSave(RPG::Database &db)
Definition: ldb_reader.cpp:20
void Write(const void *ptr, size_t size, size_t nmemb)
Definition: writer_lcf.cpp:24
bool LoadXml(const std::string &filename)
Definition: ldb_reader.cpp:51
int ReadInt()
Definition: reader_lcf.cpp:84
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:186
SaveOpt
Definition: lcf_saveopt.h:16
bool IsOk() const
Definition: writer_lcf.cpp:125
bool Load(const std::string &filename, const std::string &encoding)
Definition: ldb_reader.cpp:24
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:278
bool SaveXml(const std::string &filename)
Definition: ldb_reader.cpp:42
bool IsOk() const
Definition: reader_xml.cpp:55
bool IsOk() const
Definition: reader_lcf.cpp:192
void Parse()
Definition: reader_xml.cpp:67
std::string ldb_header
Definition: rpg_database.h:43
bool Save(const std::string &filename, const std::string &encoding, SaveOpt opt=SaveOpt::eNone)
Definition: ldb_reader.cpp:33
std::vector< RPG::Actor > & actors
Definition: data.cpp:16