Move lcf::Data access out of parsing code
MODIFY
src/lcf/writer_lcf.h
src/lcf/writer_lcf.h
@@ -36,9 +36,10 @@ public:
* Constructs a new File Writer.
*
* @param filestream already opened filestream.
+ * @param is2k3 Write in RPG2003 format.
* @param encoding name of the encoding.
*/
- LcfWriter(std::ostream& filestream, std::string encoding = "");
+ LcfWriter(std::ostream& filestream, bool is2k3, std::string encoding = "");
/**
* Destructor. Closes the opened file.
@@ -117,11 +118,16 @@ public:
*/
std::string Decode(StringView str_to_encode);
+ /** @return true if 2k3 format, false if 2k format */
+ bool Is2k3() const;
+
private:
/** File-stream managed by this Writer. */
std::ostream& stream;
/** Encoder object */
Encoder encoder;
+ /** Writing 2k3 format */
+ bool is2k3;
/**
* Converts a 16bit signed integer to/from little-endian.
@@ -160,6 +166,10 @@ private:
};
+inline bool LcfWriter::Is2k3() const {
+ return is2k3;
+}
+
} //namespace lcf
#endif

MODIFY
src/lcf/writer_xml.h
src/lcf/writer_xml.h
@@ -30,8 +30,9 @@ public:
* Constructs a new XML File Writer.
*
* @param filestream already opened filestream.
+ * @param is2k3 Write in RPG2003 format.
*/
- XmlWriter(std::ostream& filestream);
+ XmlWriter(std::ostream& filestream, bool is2k3);
/**
* Destructor. Closes the opened file.
@@ -103,6 +104,9 @@ public:
*/
bool IsOk() const;
+ /** @return true if 2k3 format, false if 2k format */
+ bool Is2k3() const;
+
protected:
/** File-stream managed by this Writer. */
std::ostream& stream;
@@ -110,6 +114,8 @@ protected:
int indent;
/** Indicates if writer cursor is at the beginning of the line. */
bool at_bol;
+ /** Writing 2k3 format */
+ bool is2k3;
/**
* Writes an indentation to the stream.
@@ -127,6 +133,11 @@ protected:
void WriteString(StringView s);
};
+inline bool XmlWriter::Is2k3() const {
+ return is2k3;
+}
+
+
} //namespace lcf
#endif

MODIFY
src/ldb_reader.cpp
src/ldb_reader.cpp
@@ -87,7 +87,7 @@ bool LDB_Reader::Load(std::istream& filestream, const std::string& encoding) {
}
bool LDB_Reader::Save(std::ostream& filestream, const std::string& encoding, SaveOpt opt) {
- LcfWriter writer(filestream, encoding);
+ LcfWriter writer(filestream, Data::system.ldb_id == 2003, encoding);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
return false;
@@ -105,7 +105,7 @@ bool LDB_Reader::Save(std::ostream& filestream, const std::string& encoding, Sav
}
bool LDB_Reader::SaveXml(std::ostream& filestream) {
- XmlWriter writer(filestream);
+ XmlWriter writer(filestream, Data::system.ldb_id == 2003);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
return false;

MODIFY
src/lmt_reader.cpp
src/lmt_reader.cpp
@@ -76,7 +76,7 @@ bool LMT_Reader::Load(std::istream& filestream, const std::string &encoding) {
}
bool LMT_Reader::Save(std::ostream& filestream, const std::string &encoding, SaveOpt opt) {
- LcfWriter writer(filestream, encoding);
+ LcfWriter writer(filestream, Data::system.ldb_id == 2003, encoding);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
return false;
@@ -94,7 +94,7 @@ bool LMT_Reader::Save(std::ostream& filestream, const std::string &encoding, Sav
}
bool LMT_Reader::SaveXml(std::ostream& filestream) {
- XmlWriter writer(filestream);
+ XmlWriter writer(filestream, Data::system.ldb_id == 2003);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
return false;

MODIFY
src/lmu_reader.cpp
src/lmu_reader.cpp
#include "lcf/lmu/chunks.h"
#include "lcf/reader_lcf.h"
#include "lcf/reader_util.h"
+#include "lcf/data.h"
#include "reader_struct.h"
namespace lcf {
@@ -82,7 +83,7 @@ std::unique_ptr<rpg::Map> LMU_Reader::Load(std::istream& filestream, const std::
}
bool LMU_Reader::Save(std::ostream& filestream, const rpg::Map& map, const std::string& encoding, SaveOpt opt) {
- LcfWriter writer(filestream, encoding);
+ LcfWriter writer(filestream, Data::system.ldb_id == 2003, encoding);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map file.\n");
return false;
@@ -101,7 +102,7 @@ bool LMU_Reader::Save(std::ostream& filestream, const rpg::Map& map, const std::
}
bool LMU_Reader::SaveXml(std::ostream& filestream, const rpg::Map& map) {
- XmlWriter writer(filestream);
+ XmlWriter writer(filestream, Data::system.ldb_id == 2003);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map file.\n");
return false;

MODIFY
src/lsd_reader.cpp
src/lsd_reader.cpp
#include "lcf/lsd/chunks.h"
#include "lcf/rpg/save.h"
#include "lcf/reader_util.h"
+#include "lcf/data.h"
#include "reader_struct.h"
namespace lcf {
@@ -96,7 +97,7 @@ std::unique_ptr<rpg::Save> LSD_Reader::Load(std::istream& filestream, const std:
}
bool LSD_Reader::Save(std::ostream& filestream, const rpg::Save& save, const std::string &encoding) {
- LcfWriter writer(filestream, encoding);
+ LcfWriter writer(filestream, Data::system.ldb_id == 2003, encoding);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return false;
@@ -110,7 +111,7 @@ bool LSD_Reader::Save(std::ostream& filestream, const rpg::Save& save, const std
}
bool LSD_Reader::SaveXml(std::ostream& filestream, const rpg::Save& save) {
- XmlWriter writer(filestream);
+ XmlWriter writer(filestream, Data::system.ldb_id == 2003);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse save file.\n");
return false;

MODIFY
src/reader_flags.cpp
src/reader_flags.cpp
#include "lcf/rpg/eventpagecondition.h"
#include "lcf/rpg/terrain.h"
#include "lcf/rpg/savepicture.h"
-#include "lcf/data.h"
#include "ldb_trooppagecondition_flags.h"
#include "ldb_terrain_flags.h"
@@ -51,7 +50,7 @@ void Flags<S>::ReadLcf(S& obj, LcfReader& stream, uint32_t length) {
template <class S>
void Flags<S>::WriteLcf(const S& obj, LcfWriter& stream) {
- const bool db_is2k3 = (Data::system.ldb_id == 2003);
+ const bool db_is2k3 = stream.Is2k3();
uint8_t byte = 0;
int bitidx = 0;
@@ -76,8 +75,8 @@ void Flags<S>::WriteLcf(const S& obj, LcfWriter& stream) {
}
template <class S>
-int Flags<S>::LcfSize(const S& /* obj */, LcfWriter& /* stream */) {
- const bool db_is2k3 = (Data::system.ldb_id == 2003);
+int Flags<S>::LcfSize(const S& /* obj */, LcfWriter& stream) {
+ const bool db_is2k3 = stream.Is2k3();
int num_bits = 0;
for (size_t i = 0; i < num_flags; ++i) {
const auto flag_is2k3 = flags_is2k3[i];
@@ -92,7 +91,7 @@ int Flags<S>::LcfSize(const S& /* obj */, LcfWriter& /* stream */) {
template <class S>
void Flags<S>::WriteXml(const S& obj, XmlWriter& stream) {
- const bool db_is2k3 = (Data::system.ldb_id == 2003);
+ const bool db_is2k3 = stream.Is2k3();
stream.BeginElement(name);
for (size_t i = 0; i < num_flags; ++i) {
const auto flag_is2k3 = flags_is2k3[i];

MODIFY
src/reader_struct_impl.h
src/reader_struct_impl.h
#include "lcf/lsd/reader.h"
#include "reader_struct.h"
#include "lcf/rpg/save.h"
-#include "lcf/data.h"
namespace lcf {
@@ -106,7 +105,7 @@ conditional_zero_writer(LcfWriter& stream) {
template <class S>
void Struct<S>::WriteLcf(const S& obj, LcfWriter& stream) {
- const bool db_is2k3 = (Data::system.ldb_id == 2003);
+ const bool db_is2k3 = stream.Is2k3();
auto ref = StructDefault<S>::make(db_is2k3);
int last = -1;
@@ -136,7 +135,7 @@ void Struct<S>::WriteLcf(const S& obj, LcfWriter& stream) {
template <class S>
int Struct<S>::LcfSize(const S& obj, LcfWriter& stream) {
- const bool db_is2k3 = (Data::system.ldb_id == 2003);
+ const bool db_is2k3 = stream.Is2k3();
int result = 0;
auto ref = StructDefault<S>::make(db_is2k3);
for (int i = 0; fields[i] != NULL; i++) {

MODIFY
src/writer_lcf.cpp
src/writer_lcf.cpp
namespace lcf {
-LcfWriter::LcfWriter(std::ostream& filestream, std::string encoding)
+LcfWriter::LcfWriter(std::ostream& filestream, bool is2k3, std::string encoding)
: stream(filestream)
, encoder(std::move(encoding))
+ , is2k3(is2k3)
{
}

MODIFY
src/writer_xml.cpp
src/writer_xml.cpp
namespace lcf {
-XmlWriter::XmlWriter(std::ostream& filestream) :
+XmlWriter::XmlWriter(std::ostream& filestream, bool is2k3) :
stream(filestream),
indent(0),
- at_bol(true)
+ at_bol(true),
+ is2k3(is2k3)
{
stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}

Move lcf::Data access out of parsing code