From 039ad6555730e590cc5b4e00a5303b14e1431e6e Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Thu, 1 Sep 2022 07:37:00 +0300 Subject: [PATCH] Add getFileSize --- src/audio_file.cc | 2 +- src/dfile.cc | 2 +- src/platform_compat.cc | 11 +++++++++++ src/platform_compat.h | 1 + src/xfile.cc | 2 +- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/audio_file.cc b/src/audio_file.cc index cbc7293..044c8ce 100644 --- a/src/audio_file.cc +++ b/src/audio_file.cc @@ -103,7 +103,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 = compat_filelength(fileno(stream)); + audioFile->fileSize = getFileSize(stream); } audioFile->position = 0; diff --git a/src/dfile.cc b/src/dfile.cc index 569ba54..39024b6 100644 --- a/src/dfile.cc +++ b/src/dfile.cc @@ -61,7 +61,7 @@ DBase* dbaseOpen(const char* filePath) // Get file size, and reposition stream to read footer, which contains two // 32-bits ints. - int fileSize = compat_filelength(fileno(stream)); + int fileSize = getFileSize(stream); if (fseek(stream, fileSize - sizeof(int) * 2, SEEK_SET) != 0) { goto err; } diff --git a/src/platform_compat.cc b/src/platform_compat.cc index 5f15704..5ead0cf 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -291,3 +291,14 @@ char* compat_strdup(const char* string) { return SDL_strdup(string); } + +// It's a replacement for compat_filelength(fileno(stream)) on platforms without +// fileno defined. +long getFileSize(FILE* stream) +{ + long originalOffset = ftell(stream); + fseek(stream, 0, SEEK_END); + long filesize = ftell(stream); + fseek(stream, originalOffset, SEEK_SET); + return filesize; +} diff --git a/src/platform_compat.h b/src/platform_compat.h index 5bb4f57..70807d4 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -40,5 +40,6 @@ int compat_remove(const char* path); int compat_rename(const char* oldFileName, const char* newFileName); void compat_windows_path_to_native(char* path); char* compat_strdup(const char* string); +long getFileSize(FILE* stream); #endif /* PLATFORM_COMPAT_H */ diff --git a/src/xfile.cc b/src/xfile.cc index 40ce71d..57cd82b 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -441,7 +441,7 @@ long xfileGetSize(XFile* stream) fileSize = 0; break; default: - fileSize = compat_filelength(fileno(stream->file)); + fileSize = getFileSize(stream->file); break; }