From 86c0585efc2f0bc7096965da938306eda269f742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0imek?= Date: Sat, 28 May 2022 23:22:40 +0200 Subject: [PATCH] Provide filepath compatibility layer (#22) Co-authored-by: Alexander Batalov --- src/audio_file.cc | 3 +-- src/cache.cc | 1 + src/dfile.cc | 3 +-- src/game.cc | 5 +++++ src/platform_compat.cc | 16 ++++++++++++++++ src/platform_compat.h | 1 + src/sound.cc | 3 ++- src/xfile.cc | 2 +- 8 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/audio_file.cc b/src/audio_file.cc index d9001de..d4a507d 100644 --- a/src/audio_file.cc +++ b/src/audio_file.cc @@ -6,7 +6,6 @@ #include "sound.h" #include -#include #include #include @@ -100,7 +99,7 @@ int audioFileOpen(const char* fname, int flags, ...) audioFile->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->fileHandle, &(audioFile->field_14), &(audioFile->field_10), &(audioFile->fileSize)); audioFile->fileSize *= 2; } else { - audioFile->fileSize = filelength(fileno(stream)); + audioFile->fileSize = compat_filelength(fileno(stream)); } audioFile->position = 0; diff --git a/src/cache.cc b/src/cache.cc index e6ed0b3..e2c77f3 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -5,6 +5,7 @@ #include "sound.h" #include +#include // qsort #include #include diff --git a/src/dfile.cc b/src/dfile.cc index 2332c46..9d71446 100644 --- a/src/dfile.cc +++ b/src/dfile.cc @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -32,7 +31,7 @@ DBase* dbaseOpen(const char* filePath) // Get file size, and reposition stream to read footer, which contains two // 32-bits ints. - int fileSize = filelength(fileno(stream)); + int fileSize = compat_filelength(fileno(stream)); if (fseek(stream, fileSize - sizeof(int) * 2, SEEK_SET) != 0) { goto err; } diff --git a/src/game.cc b/src/game.cc index eeeb359..1cc632f 100644 --- a/src/game.cc +++ b/src/game.cc @@ -56,7 +56,12 @@ #include "window_manager.h" #include "world_map.h" +#ifdef _WIN32 #include +#else +#include // access +#endif + #include #define HELP_SCREEN_WIDTH (640) diff --git a/src/platform_compat.cc b/src/platform_compat.cc index 7429292..2b9623f 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -5,6 +5,13 @@ #include +#ifdef _WIN32 +#include +#include +#else +#include +#endif + int compat_stricmp(const char* string1, const char* string2) { return SDL_strcasecmp(string1, string2); @@ -81,3 +88,12 @@ void compat_makepath(char* path, const char* drive, const char* dir, const char* strcpy(path, p.string().substr(0, COMPAT_MAX_PATH - 1).c_str()); #endif } + +long compat_filelength(int fd) +{ + long originalOffset = lseek(fd, 0, SEEK_CUR); + lseek(fd, 0, SEEK_SET); + long filesize = lseek(fd, 0, SEEK_END); + lseek(fd, originalOffset, SEEK_SET); + return filesize; +} diff --git a/src/platform_compat.h b/src/platform_compat.h index a46bfd7..1b63744 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -24,5 +24,6 @@ char* compat_strlwr(char* string); char* compat_itoa(int value, char* buffer, int radix); void compat_splitpath(const char* path, char* drive, char* dir, char* fname, char* ext); void compat_makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext); +long compat_filelength(int fd); #endif /* PLATFORM_COMPAT_H */ diff --git a/src/sound.cc b/src/sound.cc index 00ec98f..6718ccb 100644 --- a/src/sound.cc +++ b/src/sound.cc @@ -2,6 +2,7 @@ #include "debug.h" #include "memory.h" +#include "platform_compat.h" #include #include @@ -37,7 +38,7 @@ SoundFileIO gSoundDefaultFileIO = { write, lseek, tell, - filelength, + compat_filelength, -1, }; diff --git a/src/xfile.cc b/src/xfile.cc index 5524b91..3a2f6e3 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -418,7 +418,7 @@ long xfileGetSize(XFile* stream) fileSize = 0; break; default: - fileSize = filelength(fileno(stream->file)); + fileSize = compat_filelength(fileno(stream->file)); break; }