From a9fb6b8aec2bd561437213afed1c4977fa8031d6 Mon Sep 17 00:00:00 2001 From: k3tamina Date: Sat, 28 May 2022 23:56:34 +0200 Subject: [PATCH] Rewriting functions to obtain cross platform compatibility --- src/db.cc | 13 +++++++++++++ src/db.h | 6 ++++++ src/loadsave.cc | 42 +++++++++++++++--------------------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/db.cc b/src/db.cc index b00d767..5a54d8d 100644 --- a/src/db.cc +++ b/src/db.cc @@ -599,6 +599,19 @@ int fileWriteUInt32List(File* stream, unsigned int* arr, int count) return fileWriteInt32List(stream, (int*)arr, count); } +std::vector fileNameList(const fs::path& path, const std::regex& pattern) +{ + std::vector result; + fs::directory_iterator di(path); + + std::for_each(begin(di), end(di), [&](const fs::directory_entry& de) { + const fs::path& p(de.path()); + if (std::regex_match(p.string(), pattern)) + result.push_back(p); + }); + return result; +} + // 0x4C6628 int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a4) { diff --git a/src/db.h b/src/db.h index 6d4ce6e..857f95f 100644 --- a/src/db.h +++ b/src/db.h @@ -4,8 +4,13 @@ #include "memory_defs.h" #include "xfile.h" +#include +#include +#include #include +namespace fs = std::filesystem; + typedef XFile File; typedef void FileReadProgressHandler(); typedef char* StrdupProc(const char* string); @@ -69,6 +74,7 @@ int fileWriteUInt16List(File* stream, unsigned short* arr, int count); int fileWriteInt32List(File* stream, int* arr, int count); int _db_fwriteLongCount(File* stream, int* arr, int count); int fileWriteUInt32List(File* stream, unsigned int* arr, int count); +std::vector fileNameList(const fs::path& path, const std::regex& pattern); int fileNameListInit(const char* pattern, char*** fileNames, int a3, int a4); void fileNameListFree(char*** fileNames, int a2); void _db_register_mem(MallocProc* mallocProc, StrdupProc* strdupProc, FreeProc* freeProc); diff --git a/src/loadsave.cc b/src/loadsave.cc index a67d93f..440bc52 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -40,12 +40,15 @@ #include "word_wrap.h" #include "world_map.h" +#include +#include +#include #include #include #include #include -#include +namespace fs = std::filesystem; #define LS_WINDOW_WIDTH 640 #define LS_WINDOW_HEIGHT 480 @@ -2632,36 +2635,21 @@ int _EraseSave() { debugPrint("\nLOADSAVE: Erasing save(bad) slot...\n"); - sprintf(_gmpath, "%s\\%s\\%s%.2d\\", _patches, "SAVEGAME", "SLOT", _slot_cursor + 1); - strcpy(_str0, _gmpath); - strcat(_str0, "SAVE.DAT"); - remove(_str0); + std::stringstream slot; + slot << "SLOT" << std::setw(2) << std::setfill( '0') << _slot_cursor + 1; - sprintf(_gmpath, "%s\\%s%.2d\\", "SAVEGAME", "SLOT", _slot_cursor + 1); - sprintf(_str0, "%s*.%s", _gmpath, "SAV"); + // TODO: What's _patches value? Needs to be optimized. + fs::path savePath(fs::path(_patches) / fs::path("SAVEGAME") / fs::path(slot.str())); - char** fileList; - int fileListLength = fileNameListInit(_str0, &fileList, 0, 0); - if (fileListLength == -1) { + fs::remove(fs::path(savePath).append("SAVE.DAT")); + + std::vector fileList = fileNameList(savePath, std::regex("^.+\\.SAV$")); + if (fileList.empty()) return -1; - } - sprintf(_gmpath, "%s\\%s\\%s%.2d\\", _patches, "SAVEGAME", "SLOT", _slot_cursor + 1); - for (int index = fileListLength - 1; index >= 0; index--) { - strcpy(_str0, _gmpath); - strcat(_str0, fileList[index]); - remove(_str0); - } - - fileNameListFree(&fileList, 0); - - sprintf(_gmpath, "%s\\%s\\%s%.2d\\", _patches, "SAVEGAME", "SLOT", _slot_cursor + 1); - - char* v1 = _strmfe(_str1, "AUTOMAP.DB", "SAV"); - strcpy(_str0, _gmpath); - strcat(_str0, v1); - - remove(_str0); + std::for_each(fileList.begin(), fileList.end(), [] (const fs::path& p) { + fs::remove(p); + }); return 0; }