Skip to content

Commit a55a9d4

Browse files
authored
Merge pull request #435 from Ghabry/encoding
Improve encoding detection
2 parents 3f5cbdc + 2134a2a commit a55a9d4

14 files changed

Lines changed: 189 additions & 208 deletions

src/lcf/ldb/reader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ namespace LDB_Reader {
3030
/**
3131
* Loads Database.
3232
*/
33-
std::unique_ptr<lcf::rpg::Database> Load(const std::string& filename, const std::string& encoding);
33+
std::unique_ptr<lcf::rpg::Database> Load(StringView filename, StringView encoding = "");
3434

3535
/**
3636
* Saves Database.
3737
*/
38-
bool Save(const std::string& filename, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
38+
bool Save(StringView filename, const lcf::rpg::Database& db, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
3939

4040
/**
4141
* Saves Database as XML.
4242
*/
43-
bool SaveXml(const std::string& filename, const lcf::rpg::Database& db);
43+
bool SaveXml(StringView filename, const lcf::rpg::Database& db);
4444

4545
/**
4646
* Load Database as XML.
4747
*/
48-
std::unique_ptr<lcf::rpg::Database> LoadXml(const std::string& filename);
48+
std::unique_ptr<lcf::rpg::Database> LoadXml(StringView filename);
4949

5050
/**
5151
* Loads Database.
5252
*/
53-
std::unique_ptr<lcf::rpg::Database> Load(std::istream& filestream, const std::string& encoding);
53+
std::unique_ptr<lcf::rpg::Database> Load(std::istream& filestream, StringView encoding = "");
5454

5555
/**
5656
* Saves Database.
5757
*/
58-
bool Save(std::ostream& filestream, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
58+
bool Save(std::ostream& filestream, const lcf::rpg::Database& db, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
5959

6060
/**
6161
* Saves Database as XML.

src/lcf/lmt/reader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,32 @@ namespace LMT_Reader {
2525
/**
2626
* Loads Map Tree.
2727
*/
28-
std::unique_ptr<lcf::rpg::TreeMap> Load(const std::string& filename, const std::string &encoding);
28+
std::unique_ptr<lcf::rpg::TreeMap> Load(StringView filename, StringView encoding = "");
2929

3030
/**
3131
* Saves Map Tree.
3232
*/
33-
bool Save(const std::string& filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
33+
bool Save(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
3434

3535
/**
3636
* Saves Map Tree as XML.
3737
*/
38-
bool SaveXml(const std::string& filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine);
38+
bool SaveXml(StringView filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine);
3939

4040
/**
4141
* Loads Map Tree as XML.
4242
*/
43-
std::unique_ptr<lcf::rpg::TreeMap> LoadXml(const std::string& filename);
43+
std::unique_ptr<lcf::rpg::TreeMap> LoadXml(StringView filename);
4444

4545
/**
4646
* Loads Map Tree.
4747
*/
48-
std::unique_ptr<lcf::rpg::TreeMap> Load(std::istream& filestream, const std::string &encoding);
48+
std::unique_ptr<lcf::rpg::TreeMap> Load(std::istream& filestream, StringView encoding = "");
4949

5050
/**
5151
* Saves Map Tree.
5252
*/
53-
bool Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine, const std::string &encoding, SaveOpt opt = SaveOpt::eNone);
53+
bool Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
5454

5555
/**
5656
* Saves Map Tree as XML.

src/lcf/lmu/reader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ namespace LMU_Reader {
3030
/**
3131
* Loads map.
3232
*/
33-
std::unique_ptr<rpg::Map> Load(const std::string& filename, const std::string& encoding);
33+
std::unique_ptr<rpg::Map> Load(StringView filename, StringView encoding = "");
3434

3535
/**
3636
* Saves map.
3737
*/
38-
bool Save(const std::string& filename, const rpg::Map& map, EngineVersion engine, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
38+
bool Save(StringView filename, const rpg::Map& map, EngineVersion engine, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
3939

4040
/**
4141
* Saves map as XML.
4242
*/
43-
bool SaveXml(const std::string& filename, const rpg::Map& map, EngineVersion engine);
43+
bool SaveXml(StringView filename, const rpg::Map& map, EngineVersion engine);
4444

4545
/**
4646
* Loads map as XML.
4747
*/
48-
std::unique_ptr<rpg::Map> LoadXml(const std::string& filename);
48+
std::unique_ptr<rpg::Map> LoadXml(StringView filename);
4949

5050
/**
5151
* Loads map.
5252
*/
53-
std::unique_ptr<rpg::Map> Load(std::istream& filestream, const std::string& encoding);
53+
std::unique_ptr<rpg::Map> Load(std::istream& filestream, StringView encoding = "");
5454

5555
/**
5656
* Saves map.
5757
*/
58-
bool Save(std::ostream& filestream, const rpg::Map& map, EngineVersion engine, const std::string& encoding, SaveOpt opt = SaveOpt::eNone);
58+
bool Save(std::ostream& filestream, const rpg::Map& map, EngineVersion engine, StringView encoding = "", SaveOpt opt = SaveOpt::eNone);
5959

6060
/**
6161
* Saves map as XML.

src/lcf/lsd/reader.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ namespace LSD_Reader {
2727
/**
2828
* Converts from UNIX timestamp to Delphi's TDateTime format.
2929
*/
30-
double ToTDateTime(std::time_t const t);
30+
double ToTDateTime(std::time_t t);
3131

3232
/**
3333
* Converts from Delphi's TDateTime format to UNIX timestamp.
3434
*/
35-
std::time_t ToUnixTimestamp(double const ms);
35+
std::time_t ToUnixTimestamp(double ms);
3636

3737
/**
3838
* Returns current system time encoded in Delphi's TDateTime format.
3939
*/
40-
double GenerateTimestamp(std::time_t const t = std::time(NULL));
40+
double GenerateTimestamp(std::time_t t = std::time(nullptr));
4141

4242
/**
4343
* Increment the save save_count and update the timestamp.
@@ -47,32 +47,32 @@ namespace LSD_Reader {
4747
/**
4848
* Loads Savegame.
4949
*/
50-
std::unique_ptr<rpg::Save> Load(const std::string& filename, const std::string &encoding);
50+
std::unique_ptr<rpg::Save> Load(StringView filename, StringView encoding = "");
5151

5252
/**
5353
* Saves Savegame.
5454
*/
55-
bool Save(const std::string& filename, const rpg::Save& save, EngineVersion engine, const std::string &encoding);
55+
bool Save(StringView filename, const rpg::Save& save, EngineVersion engine, StringView encoding = "");
5656

5757
/*
5858
* Saves Savegame as XML.
5959
*/
60-
bool SaveXml(const std::string& filename, const rpg::Save& save, EngineVersion engine);
60+
bool SaveXml(StringView filename, const rpg::Save& save, EngineVersion engine);
6161

6262
/**
6363
* Loads Savegame as XML.
6464
*/
65-
std::unique_ptr<rpg::Save> LoadXml(const std::string& filename);
65+
std::unique_ptr<rpg::Save> LoadXml(StringView filename);
6666

6767
/**
6868
* Loads Savegame.
6969
*/
70-
std::unique_ptr<rpg::Save> Load(std::istream& filestream, const std::string &encoding);
70+
std::unique_ptr<rpg::Save> Load(std::istream& filestream, StringView encoding = "");
7171

7272
/**
7373
* Saves Savegame.
7474
*/
75-
bool Save(std::ostream& filestream, const rpg::Save& save, EngineVersion engine, const std::string &encoding);
75+
bool Save(std::ostream& filestream, const rpg::Save& save, EngineVersion engine, StringView encoding = "");
7676

7777
/*
7878
* Saves Savegame as XML.

src/lcf/reader_lcf.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ class LcfReader {
3838
* @param filestream already opened filestream.
3939
* @param encoding name of the encoding.
4040
*/
41-
LcfReader(std::istream& filestream, std::string encoding = "");
42-
43-
/**
44-
* Destructor. Closes the opened file.
45-
*/
46-
~LcfReader();
41+
explicit LcfReader(std::istream& filestream, std::string encoding = "");
4742

4843
/**
4944
* Returns the last set error.

src/lcf/reader_util.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "lcf/string_view.h"
1616

1717
namespace lcf {
18+
namespace rpg {
19+
class Database;
20+
}
1821

1922
/**
2023
* ReaderUtil namespace.
@@ -29,42 +32,42 @@ namespace ReaderUtil {
2932
std::string CodepageToEncoding(int codepage);
3033

3134
/**
32-
* Detects the encoding based on text analysis.
35+
* Detects the encoding of the database based on text analysis.
3336
*
34-
* @param filestream stream containing the database file
37+
* @param db Database to process
3538
*
3639
* @return encoding or empty string if not detected.
3740
*/
38-
std::string DetectEncoding(std::istream& filestream);
41+
std::string DetectEncoding(lcf::rpg::Database& db);
3942

4043
/**
41-
* Detects the encoding based on text analysis.
44+
* Detects the encoding of the database based on text analysis.
45+
* Returns a vector of possible candidates, highest candidate being at the beginning.
4246
*
43-
* @param filestream stream containing the database file
47+
* @param db Database to process
4448
*
45-
* @return encoding or empty string if not detected.
49+
* @return list of encodings or empty if not detected
4650
*/
47-
std::string DetectEncoding(StringView data);
51+
std::vector<std::string> DetectEncodings(lcf::rpg::Database& db);
4852

4953
/**
50-
* Detects the encoding based on text analysis and returns a vector with
51-
* possible candidates, highest candidate being at the beginning.
54+
* Detects the encoding of a string based on text analysis.
5255
*
53-
* @param filestream stream containing the database file
56+
* @param string encoded data of a few hundred bytes
5457
*
55-
* @return list of encodings or empty if not detected
58+
* @return encoding or empty string if not detected.
5659
*/
57-
std::vector<std::string> DetectEncodings(std::istream& filestream);
60+
std::string DetectEncoding(StringView data);
5861

5962
/**
60-
* Detects the encoding based on text analysis and returns a vector with
61-
* possible candidates, highest candidate being at the beginning.
63+
* Detects the encoding of a string based on text analysis.
64+
* Returns a vector of possible candidates, highest candidate being at the beginning.
6265
*
6366
* @param string encoded data of a few hundred bytes
6467
*
6568
* @return list of encodings or empty if not detected
6669
*/
67-
std::vector<std::string> DetectEncodings(StringView data);
70+
std::vector<std::string> DetectEncodings(StringView string);
6871

6972
/**
7073
* Returns the encoding set in the ini file.
@@ -73,7 +76,7 @@ namespace ReaderUtil {
7376
*
7477
* @return encoding or empty string if not found.
7578
*/
76-
std::string GetEncoding(const std::string& ini_file);
79+
std::string GetEncoding(StringView ini_file);
7780

7881
/**
7982
* Returns the encoding set in the ini file.

src/lcf/writer_lcf.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ class LcfWriter {
4242
*/
4343
LcfWriter(std::ostream& filestream, EngineVersion engine, std::string encoding = "");
4444

45-
/**
46-
* Destructor. Closes the opened file.
47-
*/
48-
~LcfWriter();
49-
5045
/**
5146
* Writes raw data to the stream (fwrite() wrapper).
5247
*

src/ldb_reader.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,44 @@ void LDB_Reader::PrepareSave(rpg::Database& db) {
2222
++db.system.save_count;
2323
}
2424

25-
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(const std::string& filename, const std::string& encoding) {
26-
std::ifstream stream(filename.c_str(), std::ios::binary);
25+
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(StringView filename, StringView encoding) {
26+
std::ifstream stream(ToString(filename), std::ios::binary);
2727
if (!stream.is_open()) {
28-
fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
28+
fprintf(stderr, "Failed to open LDB file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
2929
return nullptr;
3030
}
3131
return LDB_Reader::Load(stream, encoding);
3232
}
3333

34-
bool LDB_Reader::Save(const std::string& filename, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt) {
35-
std::ofstream stream(filename.c_str(), std::ios::binary);
34+
bool LDB_Reader::Save(StringView filename, const lcf::rpg::Database& db, StringView encoding, SaveOpt opt) {
35+
std::ofstream stream(ToString(filename), std::ios::binary);
3636
if (!stream.is_open()) {
37-
fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
37+
fprintf(stderr, "Failed to open LDB file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
3838
return false;
3939
}
4040
return LDB_Reader::Save(stream, db, encoding, opt);
4141
}
4242

43-
bool LDB_Reader::SaveXml(const std::string& filename, const lcf::rpg::Database& db) {
44-
std::ofstream stream(filename.c_str(), std::ios::binary);
43+
bool LDB_Reader::SaveXml(StringView filename, const lcf::rpg::Database& db) {
44+
std::ofstream stream(ToString(filename), std::ios::binary);
4545
if (!stream.is_open()) {
46-
fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", filename.c_str(), strerror(errno));
46+
fprintf(stderr, "Failed to open LDB XML file `%s' for writing : %s\n", ToString(filename).c_str(), strerror(errno));
4747
return false;
4848
}
4949
return LDB_Reader::SaveXml(stream, db);
5050
}
5151

52-
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(const std::string& filename) {
53-
std::ifstream stream(filename.c_str(), std::ios::binary);
52+
std::unique_ptr<lcf::rpg::Database> LDB_Reader::LoadXml(StringView filename) {
53+
std::ifstream stream(ToString(filename), std::ios::binary);
5454
if (!stream.is_open()) {
55-
fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", filename.c_str(), strerror(errno));
55+
fprintf(stderr, "Failed to open LDB XML file `%s' for reading : %s\n", ToString(filename).c_str(), strerror(errno));
5656
return nullptr;
5757
}
5858
return LDB_Reader::LoadXml(stream);
5959
}
6060

61-
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(std::istream& filestream, const std::string& encoding) {
62-
LcfReader reader(filestream, encoding);
61+
std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(std::istream& filestream, StringView encoding) {
62+
LcfReader reader(filestream, ToString(encoding));
6363
if (!reader.IsOk()) {
6464
LcfReader::SetError("Couldn't parse database file.\n");
6565
return nullptr;
@@ -87,9 +87,9 @@ std::unique_ptr<lcf::rpg::Database> LDB_Reader::Load(std::istream& filestream, c
8787
return db;
8888
}
8989

90-
bool LDB_Reader::Save(std::ostream& filestream, const lcf::rpg::Database& db, const std::string& encoding, SaveOpt opt) {
90+
bool LDB_Reader::Save(std::ostream& filestream, const lcf::rpg::Database& db, StringView encoding, SaveOpt opt) {
9191
const auto engine = GetEngineVersion(db);
92-
LcfWriter writer(filestream, engine, encoding);
92+
LcfWriter writer(filestream, engine, ToString(encoding));
9393
if (!writer.IsOk()) {
9494
LcfReader::SetError("Couldn't parse database file.\n");
9595
return false;

0 commit comments

Comments
 (0)