Skip to content

Commit a56e910

Browse files
committedJun 6, 2023
lcf2xml: Support --encoding command line argument
1 parent d9a191a commit a56e910

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed
 

‎tools/lcf2xml.cpp

+29-25
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ std::string GetFilename(const std::string& str);
5050
FileCategories GetFilecategory(const std::string& in_file);
5151
FileTypes GetFiletype(const std::string& in_file, std::string& out_extension);
5252
void PrintReaderError(const std::string data);
53-
int ReaderWriteToFile(const std::string& in, const std::string& out, FileTypes in_type, lcf::EngineVersion engine);
53+
int ReaderWriteToFile(const std::string& in, const std::string& out, FileTypes in_type, lcf::EngineVersion engine, std::string encoding);
5454

5555
int main(int argc, char** argv)
5656
{
@@ -60,12 +60,14 @@ int main(int argc, char** argv)
6060
std::cerr << "Usage: " << argv[0] << "[--2k] [--2k3] file1 [... fileN]" << std::endl;
6161
std::cerr << "\t--2k: Treat files as RPG 2000" << std::endl;
6262
std::cerr << "\t--2k3: Treat files as RPG 2003 (default)" << std::endl;
63+
std::cerr << "\t--encoding N: Use encoding N as the file encoding" << std::endl;
6364

6465
return 1;
6566
}
6667

6768
std::vector<std::string> infiles;
6869
std::string outfile;
70+
std::string encoding;
6971

7072
FileCategories category = FileCategory_Invalid;
7173
FileTypes type;
@@ -82,6 +84,10 @@ int main(int argc, char** argv)
8284
engine = lcf::EngineVersion::e2k3;
8385
continue;
8486
}
87+
if (!std::strcmp(argv[i], "--encoding") && (++i < argc)) {
88+
encoding = argv[i];
89+
continue;
90+
}
8591
if (category == FileCategory_Invalid) {
8692
category = GetFilecategory(argv[i]);
8793
if (category == FileCategory_Invalid) {
@@ -100,7 +106,7 @@ int main(int argc, char** argv)
100106
outfile = GetFilename(*it);
101107
type = GetFiletype(*it, extension);
102108
outfile += extension;
103-
if (ReaderWriteToFile(*it, outfile, type, engine) != 0) {
109+
if (ReaderWriteToFile(*it, outfile, type, engine, encoding) != 0) {
104110
errors++;
105111
}
106112
}
@@ -241,41 +247,39 @@ void PrintReaderError(const std::string data)
241247
}
242248

243249
/** Takes data from in and writes converted data into out using liblcf. */
244-
int ReaderWriteToFile(const std::string& in, const std::string& out, FileTypes in_type, lcf::EngineVersion engine)
250+
int ReaderWriteToFile(const std::string& in, const std::string& out, FileTypes in_type, lcf::EngineVersion engine, std::string encoding)
245251
{
246252
std::string path = GetPath(in) + "/";
247-
std::string encoding = "";
248253

254+
if (encoding.empty()) {
249255
#ifdef _WIN32
250-
encoding = lcf::ReaderUtil::GetEncoding(path + "RPG_RT.ini");
256+
encoding = lcf::ReaderUtil::GetEncoding(path + "RPG_RT.ini");
251257
#else
252-
DIR* dir = opendir(path.c_str());
253-
if (dir) {
254-
struct dirent* ent;
255-
while ((ent = ::readdir(dir)) != NULL) {
256-
if (ent->d_name[0] == '.') { continue; }
257-
std::string name = ent->d_name;
258-
259-
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
260-
261-
if (name == "rpg_rt.ini") {
262-
encoding = lcf::ReaderUtil::GetEncoding(path + ent->d_name);
263-
closedir(dir);
264-
goto dirsuccess;
265-
break;
258+
DIR* dir = opendir(path.c_str());
259+
if (dir) {
260+
struct dirent* ent;
261+
while ((ent = ::readdir(dir)) != NULL) {
262+
if (ent->d_name[0] == '.') { continue; }
263+
std::string name = ent->d_name;
264+
265+
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
266+
267+
if (name == "rpg_rt.ini") {
268+
encoding = lcf::ReaderUtil::GetEncoding(path + ent->d_name);
269+
break;
270+
}
266271
}
272+
closedir(dir);
267273
}
268-
closedir(dir);
269-
}
270-
else {
271-
std::cerr << "Failed opening directory " << path << std::endl;
274+
else {
275+
std::cerr << "Failed opening directory " << path << std::endl;
276+
}
277+
#endif
272278
}
273279

274280
if (encoding.empty()) {
275281
encoding = lcf::ReaderUtil::GetLocaleEncoding();
276282
}
277-
dirsuccess:
278-
#endif
279283

280284
switch (in_type)
281285
{

0 commit comments

Comments
 (0)
Please sign in to comment.