Add getFileSize

This commit is contained in:
Alexander Batalov 2022-09-01 07:37:00 +03:00
parent 26e5104a96
commit 039ad65557
5 changed files with 15 additions and 3 deletions

View File

@ -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->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->fileHandle, &(audioFile->field_14), &(audioFile->field_10), &(audioFile->fileSize));
audioFile->fileSize *= 2; audioFile->fileSize *= 2;
} else { } else {
audioFile->fileSize = compat_filelength(fileno(stream)); audioFile->fileSize = getFileSize(stream);
} }
audioFile->position = 0; audioFile->position = 0;

View File

@ -61,7 +61,7 @@ DBase* dbaseOpen(const char* filePath)
// Get file size, and reposition stream to read footer, which contains two // Get file size, and reposition stream to read footer, which contains two
// 32-bits ints. // 32-bits ints.
int fileSize = compat_filelength(fileno(stream)); int fileSize = getFileSize(stream);
if (fseek(stream, fileSize - sizeof(int) * 2, SEEK_SET) != 0) { if (fseek(stream, fileSize - sizeof(int) * 2, SEEK_SET) != 0) {
goto err; goto err;
} }

View File

@ -291,3 +291,14 @@ char* compat_strdup(const char* string)
{ {
return SDL_strdup(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;
}

View File

@ -40,5 +40,6 @@ int compat_remove(const char* path);
int compat_rename(const char* oldFileName, const char* newFileName); int compat_rename(const char* oldFileName, const char* newFileName);
void compat_windows_path_to_native(char* path); void compat_windows_path_to_native(char* path);
char* compat_strdup(const char* string); char* compat_strdup(const char* string);
long getFileSize(FILE* stream);
#endif /* PLATFORM_COMPAT_H */ #endif /* PLATFORM_COMPAT_H */

View File

@ -441,7 +441,7 @@ long xfileGetSize(XFile* stream)
fileSize = 0; fileSize = 0;
break; break;
default: default:
fileSize = compat_filelength(fileno(stream->file)); fileSize = getFileSize(stream->file);
break; break;
} }