liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ldb_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 "ldb_reader.h"
8 #include "ldb_chunks.h"
9 #include "data.h"
10 #include "reader_util.h"
11 #include "reader_struct.h"
12 
13 bool LDB_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 database file.\n", filename.c_str());
17  return false;
18  }
19  std::string header;
20  reader.ReadString(header, reader.ReadInt());
21  if (header.length() != 11) {
22  LcfReader::SetError("%s is not a valid RPG2000 database.\n", filename.c_str());
23  return false;
24  }
25  if (header != "LcfDataBase") {
26  fprintf(stderr, "Warning: %s header is not LcfDataBase and might not be a valid RPG2000 database.\n", filename.c_str());
27  }
29 
30  // Delayed initialization of some actor fields because they are engine
31  // dependent
32  std::vector<RPG::Actor>::iterator it;
33  for (it = Data::actors.begin(); it != Data::actors.end(); ++it) {
34  (*it).Setup();
35  }
36 
37  return true;
38 }
39 
40 bool LDB_Reader::Save(const std::string& filename, const std::string& encoding) {
41  LcfWriter writer(filename, encoding);
42  if (!writer.IsOk()) {
43  LcfReader::SetError("Couldn't open %s database file.\n", filename.c_str());
44  return false;
45  }
46  const std::string header("LcfDataBase");
47  writer.WriteInt(header.size());
48  writer.Write(header);
50  return true;
51 }
52 
53 bool LDB_Reader::SaveXml(const std::string& filename) {
54  XmlWriter writer(filename);
55  if (!writer.IsOk()) {
56  LcfReader::SetError("Couldn't open %s database file.\n", filename.c_str());
57  return false;
58  }
59  writer.BeginElement("LDB");
61  writer.EndElement("LDB");
62  return true;
63 }
64 
65 bool LDB_Reader::LoadXml(const std::string& filename) {
66  XmlReader reader(filename);
67  if (!reader.IsOk()) {
68  LcfReader::SetError("Couldn't open %s database file.\n", filename.c_str());
69  return false;
70  }
72  reader.Parse();
73  return true;
74 }
bool IsOk() const
Definition: writer_xml.cpp:199
RPG::Database data
Definition: data.cpp:11
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
bool LoadXml(const std::string &filename)
Definition: ldb_reader.cpp:65
int ReadInt()
Definition: reader_lcf.cpp:82
void ReadString(std::string &ref, size_t size)
Definition: reader_lcf.cpp:162
bool Save(const std::string &filename, const std::string &encoding)
Definition: ldb_reader.cpp:40
bool Load(const std::string &filename, const std::string &encoding)
Definition: ldb_reader.cpp:13
static void SetError(const char *fmt,...)
Definition: reader_lcf.cpp:234
bool SaveXml(const std::string &filename)
Definition: ldb_reader.cpp:53
void Parse()
Definition: reader_xml.cpp:79
std::vector< RPG::Actor > & actors
Definition: data.cpp:13