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); 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 // 0x4C6628
int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a4) int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a4)
{ {

View File

@ -4,8 +4,13 @@
#include "memory_defs.h" #include "memory_defs.h"
#include "xfile.h" #include "xfile.h"
#include <vector>
#include <filesystem>
#include <regex>
#include <stddef.h> #include <stddef.h>
namespace fs = std::filesystem;
typedef XFile File; typedef XFile File;
typedef void FileReadProgressHandler(); typedef void FileReadProgressHandler();
typedef char* StrdupProc(const char* string); 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 fileWriteInt32List(File* stream, int* arr, int count);
int _db_fwriteLongCount(File* stream, int* arr, int count); int _db_fwriteLongCount(File* stream, int* arr, int count);
int fileWriteUInt32List(File* stream, unsigned 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); int fileNameListInit(const char* pattern, char*** fileNames, int a3, int a4);
void fileNameListFree(char*** fileNames, int a2); void fileNameListFree(char*** fileNames, int a2);
void _db_register_mem(MallocProc* mallocProc, StrdupProc* strdupProc, FreeProc* freeProc); void _db_register_mem(MallocProc* mallocProc, StrdupProc* strdupProc, FreeProc* freeProc);

View File

@ -40,12 +40,15 @@
#include "word_wrap.h" #include "word_wrap.h"
#include "world_map.h" #include "world_map.h"
#include <algorithm>
#include <filesystem>
#include <vector>
#include <assert.h> #include <assert.h>
#include <direct.h> #include <direct.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <algorithm> namespace fs = std::filesystem;
#define LS_WINDOW_WIDTH 640 #define LS_WINDOW_WIDTH 640
#define LS_WINDOW_HEIGHT 480 #define LS_WINDOW_HEIGHT 480
@ -2632,36 +2635,21 @@ int _EraseSave()
{ {
debugPrint("\nLOADSAVE: Erasing save(bad) slot...\n"); debugPrint("\nLOADSAVE: Erasing save(bad) slot...\n");
sprintf(_gmpath, "%s\\%s\\%s%.2d\\", _patches, "SAVEGAME", "SLOT", _slot_cursor + 1); std::stringstream slot;
strcpy(_str0, _gmpath); slot << "SLOT" << std::setw(2) << std::setfill( '0') << _slot_cursor + 1;
strcat(_str0, "SAVE.DAT");
remove(_str0);
sprintf(_gmpath, "%s\\%s%.2d\\", "SAVEGAME", "SLOT", _slot_cursor + 1); // TODO: What's _patches value? Needs to be optimized.
sprintf(_str0, "%s*.%s", _gmpath, "SAV"); fs::path savePath(fs::path(_patches) / fs::path("SAVEGAME") / fs::path(slot.str()));
char** fileList; fs::remove(fs::path(savePath).append("SAVE.DAT"));
int fileListLength = fileNameListInit(_str0, &fileList, 0, 0);
if (fileListLength == -1) { std::vector<fs::path> fileList = fileNameList(savePath, std::regex("^.+\\.SAV$"));
if (fileList.empty())
return -1; return -1;
}
sprintf(_gmpath, "%s\\%s\\%s%.2d\\", _patches, "SAVEGAME", "SLOT", _slot_cursor + 1); std::for_each(fileList.begin(), fileList.end(), [] (const fs::path& p) {
for (int index = fileListLength - 1; index >= 0; index--) { fs::remove(p);
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);
return 0; return 0;
} }