Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Dec 4, 2023
1 parent 7d5ce3d commit 11bdd9b
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 110 deletions.
11 changes: 5 additions & 6 deletions src/dbstring_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "lcf/dbstring.h"
#include "log.h"
#include "reader_struct.h"
#include <iostream>

Expand Down Expand Up @@ -43,7 +44,7 @@ struct RawStruct<std::vector<DBString> > {
void RawStruct<DBString>::ReadLcf(DBString& ref, LcfReader& stream, uint32_t length) {
stream.ReadString(ref, length);
#ifdef LCF_DEBUG_TRACE
printf(" %s\n", ref.c_str());
fprintf(stderr, " %s\n", ref.c_str());
#endif
}

Expand Down Expand Up @@ -106,9 +107,7 @@ void RawStruct<std::vector<DBString>>::ReadLcf(std::vector<DBString>& ref, LcfRe
}

if (stream.Tell() != endpos) {
#ifdef LCF_DEBUG_TRACE
fprintf(stderr, "Misaligned!\n");
#endif
Log::Warning("vector<string> Misaligned at 0x" PRIx32 "", stream.Tell());
stream.Seek(endpos);
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ class DbStringVectorXmlHandler : public XmlHandler {

void StartElement(XmlReader& stream, const char* name, const char** atts) {
if (strcmp(name, "item") != 0) {
stream.Error("Expecting %s but got %s", "item", name);
Log::Error("XML: Expecting %s but got %s", "item", name);
return;
}

Expand All @@ -191,7 +190,7 @@ class DbStringVectorXmlHandler : public XmlHandler {
}

if (id <= last_id || id < -1) {
stream.Error("Bad Id %d / %d", id, last_id);
Log::Error("XML: Bad Id %d / %d", id, last_id);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "lcf/encoder.h"
#include "lcf/reader_util.h"
#include "lcf/scope_guard.h"
#include "log.h"
#include <cstdio>
#include <cstdlib>

Expand Down Expand Up @@ -87,7 +88,7 @@ void Encoder::Init() {
auto conv_runtime = ucnv_open(runtime_encoding, &status);

if (conv_runtime == nullptr) {
fprintf(stderr, "liblcf: ucnv_open() error for encoding \"%s\": %s\n", runtime_encoding, u_errorName(status));
Log::Error("ucnv_open() error for encoding \"%s\": %s\n", runtime_encoding, u_errorName(status));
return;
}
status = U_ZERO_ERROR;
Expand All @@ -96,7 +97,7 @@ void Encoder::Init() {
auto conv_storage = ucnv_open(storage_encoding.c_str(), &status);

if (conv_storage == nullptr) {
fprintf(stderr, "liblcf: ucnv_open() error for dest encoding \"%s\": %s\n", storage_encoding.c_str(), u_errorName(status));
Log::Error("ucnv_open() error for dest encoding \"%s\": %s\n", storage_encoding.c_str(), u_errorName(status));
return;
}

Expand Down Expand Up @@ -144,7 +145,7 @@ void Encoder::Convert(std::string& str, UConverter* conv_dst, UConverter* conv_s
&status);

if (U_FAILURE(status)) {
fprintf(stderr, "liblcf: ucnv_convertEx() error when encoding \"%s\": %s\n", src.c_str(), u_errorName(status));
Log::Error("ucnv_convertEx() error when encoding \"%s\": %s\n", src.c_str(), u_errorName(status));
_buffer.clear();
}

Expand Down
5 changes: 0 additions & 5 deletions src/lcf/reader_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ class XmlReader {
*/
bool IsOk() const;

/**
* Reports a parsing error.
*/
void Error(const char* fmt, ...);

/**
* Parses the XML file.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/ldb_equipment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "lcf/ldb/reader.h"
#include "lcf/ldb/chunks.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand All @@ -27,7 +28,7 @@ struct RawStruct<rpg::Equipment> {
*/
void RawStruct<rpg::Equipment>::ReadLcf(rpg::Equipment& ref, LcfReader& stream, uint32_t length) {
if (length != 10) {
fprintf(stderr, "Equipment has incorrect size %" PRIu32 " (expected 10)\n", length);
Log::Warning("Equipment has incorrect size %" PRIu32 " (expected 10)\n", length);

LcfReader::Chunk chunk_info;
chunk_info.ID = 0x33;
Expand Down Expand Up @@ -85,7 +86,7 @@ class EquipmentXmlHandler : public XmlHandler {
else if (strcmp(name, "accessory_id") == 0)
field = &ref.accessory_id;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/ldb_eventcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>
#include <vector>
#include "log.h"
#include "reader_struct.h"
#include "lcf/rpg/eventcommand.h"

Expand Down Expand Up @@ -108,7 +109,7 @@ class EventCommandXmlHandler : public XmlHandler {
else if (strcmp(name, "parameters") == 0)
field = Parameters;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = None;
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ void RawStruct<std::vector<rpg::EventCommand> >::ReadLcf(

if (stream.Tell() >= endpos) {
stream.Seek(endpos, LcfReader::FromStart);
fprintf(stderr, "Event command corrupted at %" PRIu32 "\n", stream.Tell());
Log::Warning("Event command corrupted at %" PRIu32 "\n", stream.Tell());
for (;;) {
// Try finding the real end of the event command (4 0-bytes)
int i = 0;
Expand Down Expand Up @@ -216,7 +217,7 @@ class EventCommandVectorXmlHandler : public XmlHandler {

void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
if (strcmp(name, "EventCommand") != 0)
stream.Error("Expecting %s but got %s", "EventCommand", name);
Log::Error("XML: Expecting %s but got %s", "EventCommand", name);
ref.resize(ref.size() + 1);
rpg::EventCommand& obj = ref.back();
stream.SetHandler(new EventCommandXmlHandler(obj));
Expand Down
2 changes: 1 addition & 1 deletion src/ldb_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ParametersXmlHandler : public XmlHandler {
else if (strcmp(name, "agility") == 0)
field = &ref.agility;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/ldb_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lcf/ldb/reader.h"
#include "lcf/ldb/chunks.h"
#include "lcf/reader_util.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand All @@ -25,7 +26,7 @@ void LDB_Reader::PrepareSave(rpg::Database& db) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(StringView filename, StringView encoding) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LDB_Reader::Load(stream, encoding);
Expand All @@ -34,7 +35,7 @@ std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(StringView filename, String
bool LDB_Reader::Save(StringView filename, const lcf::rpg::Database& db, StringView encoding, SaveOpt opt) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LDB_Reader::Save(stream, db, encoding, opt);
Expand All @@ -43,7 +44,7 @@ bool LDB_Reader::Save(StringView filename, const lcf::rpg::Database& db, StringV
bool LDB_Reader::SaveXml(StringView filename, const lcf::rpg::Database& db) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB XML file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LDB_Reader::SaveXml(stream, db);
Expand All @@ -52,7 +53,7 @@ bool LDB_Reader::SaveXml(StringView filename, const lcf::rpg::Database& db) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(StringView filename) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LDB XML file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LDB_Reader::LoadXml(stream);
Expand All @@ -61,17 +62,17 @@ std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(StringView filename) {
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(std::istream& filestream, StringView encoding) {
LcfReader reader(filestream, ToString(encoding));
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
LcfReader::SetError("Couldn't parse database file.");
return nullptr;
}
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 11) {
LcfReader::SetError("This is not a valid RPG2000 database.\n");
LcfReader::SetError("This is not a valid RPG2000 database.");
return nullptr;
}
if (header != "LcfDataBase") {
fprintf(stderr, "Warning: This header is not LcfDataBase and might not be a valid RPG2000 database.\n");
Log::Warning("Header %s != LcfDataBase and might not be a valid RPG2000 database.", header.c_str());
}
auto db = std::make_unique<lcf::rpg::Database>();
db->ldb_header = header;
Expand All @@ -91,7 +92,7 @@ bool LDB_Reader::Save(std::ostream& filestream, const lcf::rpg::Database& db, St
const auto engine = GetEngineVersion(db);
LcfWriter writer(filestream, engine, ToString(encoding));
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse database file.\n");
LcfReader::SetError("Couldn't parse database file.");
return false;
}
std::string header;
Expand Down
21 changes: 11 additions & 10 deletions src/lmt_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
#include "lcf/lmt/reader.h"
#include "lcf/lmt/chunks.h"
#include "lcf/reader_util.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {

std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(StringView filename, StringView encoding) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LMT file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LMT file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LMT_Reader::Load(stream, encoding);
Expand All @@ -30,7 +31,7 @@ std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(StringView filename, StringV
bool LMT_Reader::Save(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding, SaveOpt opt) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LMT file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LMT file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LMT_Reader::Save(stream, tmap, engine, encoding, opt);
Expand All @@ -39,7 +40,7 @@ bool LMT_Reader::Save(StringView filename, const lcf::rpg::TreeMap& tmap, Engine
bool LMT_Reader::SaveXml(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
std::ofstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LMT XML file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LMT XML file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
return false;
}
return LMT_Reader::SaveXml(stream, tmap, engine);
Expand All @@ -48,7 +49,7 @@ bool LMT_Reader::SaveXml(StringView filename, const lcf::rpg::TreeMap& tmap, Eng
std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(StringView filename) {
std::ifstream stream(ToString(filename), std::ios::binary);
if (!stream.is_open()) {
fprintf(stderr, "Failed to open LMT XML file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
Log::Error("Failed to open LMT XML file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
return nullptr;
}
return LMT_Reader::LoadXml(stream);
Expand All @@ -57,17 +58,17 @@ std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(StringView filename) {
std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(std::istream& filestream, StringView encoding) {
LcfReader reader(filestream, ToString(encoding));
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
LcfReader::SetError("Couldn't parse map tree file.");
return nullptr;
}
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 10) {
LcfReader::SetError("This is not a valid RPG2000 map tree.\n");
LcfReader::SetError("This is not a valid RPG2000 map tree.");
return nullptr;
}
if (header != "LcfMapTree") {
fprintf(stderr, "Warning: This header is not LcfMapTree and might not be a valid RPG2000 map tree.\n");
Log::Warning("Header %s != LcfMapTree and might not be a valid RPG2000 map tree.", header.c_str());
}
auto tmap = std::make_unique<lcf::rpg::TreeMap>();
tmap->lmt_header = std::move(header);
Expand All @@ -78,7 +79,7 @@ std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(std::istream& filestream, St
bool LMT_Reader::Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding, SaveOpt opt) {
LcfWriter writer(filestream, engine, ToString(encoding));
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
LcfReader::SetError("Couldn't parse map tree file.");
return false;
}
std::string header;
Expand All @@ -96,7 +97,7 @@ bool LMT_Reader::Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, E
bool LMT_Reader::SaveXml(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
XmlWriter writer(filestream, engine);
if (!writer.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
LcfReader::SetError("Couldn't parse map tree file.");
return false;
}
writer.BeginElement("LMT");
Expand All @@ -108,7 +109,7 @@ bool LMT_Reader::SaveXml(std::ostream& filestream, const lcf::rpg::TreeMap& tmap
std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(std::istream& filestream) {
XmlReader reader(filestream);
if (!reader.IsOk()) {
LcfReader::SetError("Couldn't parse map tree file.\n");
LcfReader::SetError("Couldn't parse map tree file.");
return nullptr;
}
auto tmap = std::make_unique<lcf::rpg::TreeMap>();
Expand Down
2 changes: 1 addition & 1 deletion src/lmt_rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class RectXmlHandler : public XmlHandler {
else if (strcmp(name, "b") == 0)
field = &ref.b;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lmt_treemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "lcf/lmt/reader.h"
#include "lcf/lmt/chunks.h"
#include "log.h"
#include "reader_struct.h"

namespace lcf {
Expand Down Expand Up @@ -89,7 +90,7 @@ class TreeMapXmlHandler : public XmlHandler {
else if (strcmp(name, "start") == 0)
Struct<rpg::Start>::BeginXml(ref.start, stream);
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
}
}
void EndElement(XmlReader& /* stream */, const char* /* name */) {
Expand Down
4 changes: 2 additions & 2 deletions src/lmu_movecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class MoveCommandXmlHandler : public XmlHandler {
else if (strcmp(name, "parameter_string") == 0)
parameter_string = true;
else {
stream.Error("Unrecognized field '%s'", name);
Log::Error("XML: Unrecognized field '%s'", name);
field = NULL;
parameter_string = false;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ class MoveCommandVectorXmlHandler : public XmlHandler {

void StartElement(XmlReader& stream, const char* name, const char** /* atts */) {
if (strcmp(name, "MoveCommand") != 0)
stream.Error("Expecting %s but got %s", "MoveCommand", name);
Log::Error("XML: Expecting %s but got %s", "MoveCommand", name);
ref.resize(ref.size() + 1);
rpg::MoveCommand& obj = ref.back();
stream.SetHandler(new MoveCommandXmlHandler(obj));
Expand Down
Loading

0 comments on commit 11bdd9b

Please sign in to comment.