Rewriting functions to obtain cross platform compatibility

This commit is contained in:
k3tamina 2022-05-28 23:56:34 +02:00
parent 72e8dba4e3
commit a9fb6b8aec
3 changed files with 34 additions and 27 deletions

View File

@ -599,6 +599,19 @@ int fileWriteUInt32List(File* stream, unsigned int* arr, int count)
return fileWriteInt32List(stream, (int*)arr, count);
}
std::vector<fs::path> fileNameList(const fs::path& path, const std::regex& pattern)
{
std::vector<fs::path> 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)
{

View File

@ -4,8 +4,13 @@
#include "memory_defs.h"
#include "xfile.h"
#include <vector>
#include <filesystem>
#include <regex>
#include <stddef.h>
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<fs::path> 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);

View File

@ -40,12 +40,15 @@
#include "word_wrap.h"
#include "world_map.h"
#include <algorithm>
#include <filesystem>
#include <vector>
#include <assert.h>
#include <direct.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
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<fs::path> 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;
}