From 25410c7aac7c632d7e43d72c4aa9aee951a6a0f9 Mon Sep 17 00:00:00 2001 From: Doyle T Date: Tue, 10 Jul 2018 15:52:12 +1000 Subject: [PATCH] Fix ReadEntireFile bug --- DqnUnitTest.cpp | 2 ++ dqn.h | 30 +++++++++++------------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/DqnUnitTest.cpp b/DqnUnitTest.cpp index cc187c5..07d6a4b 100644 --- a/DqnUnitTest.cpp +++ b/DqnUnitTest.cpp @@ -1,7 +1,9 @@ +#if defined(DQN_IS_WIN32) #define WIN32_MEAN_AND_LEAN #include #include #include +#endif #if defined(__linux__) #define HANDMADE_MATH_NO_SSE diff --git a/dqn.h b/dqn.h index 87979fb..e44a4e7 100644 --- a/dqn.h +++ b/dqn.h @@ -8766,28 +8766,19 @@ DQN_FILE__LIST_DIR(DqnFile__PlatformListDir) FILE_SCOPE bool DqnFile__UnixGetFileSize(char const *path, usize *size) { struct stat fileStat = {}; - if (stat(path, &fileStat)) - { - // TODO(doyle): Logging - return false; - } - + stat(path, &fileStat); *size = fileStat.st_size; + if (*size != 0) + return true; + // NOTE: Can occur in some instances where files are generated on demand, i.e. /proc/cpuinfo. // But there can also be zero-byte files, we can't be sure. So manual check by counting bytes - if (*size == 0) + if (FILE *file = fopen(path, "rb")) { - FILE *file = fopen(path, "r"); DQN_DEFER(fclose(file)); - u64 fileSizeInBytes = 0; - - char c = fgetc(file); - while (c != EOF) - { - fileSizeInBytes++; - c = fgetc(file); - } + while (fgetc(file) != EOF) + *size++; } return true; @@ -8849,6 +8840,7 @@ DqnFile__UnixOpen(char const *path, DqnFile *file, u32 flags, DqnFile::Action ac return false; } + file->handle = fopen(path, mode); file->flags = flags; return true; } @@ -9058,7 +9050,7 @@ u8 *DqnFile::ReadEntireFile(char const *path, usize *bufSize, DqnMemAPI *api) if (DqnFile::ReadEntireFile(path, buf, requiredSize, &bytesRead)) { *bufSize = requiredSize; - DQN_ASSERT(bytesRead == requiredSize); + DQN_ASSERTM(bytesRead == requiredSize, "%zu != %zu", bytesRead, requiredSize); return buf; } @@ -9108,7 +9100,7 @@ bool DqnFile::ReadEntireFile(const char *path, u8 *buf, usize bufSize, usize *by } *bytesRead = file.Read(buf, file.size); - DQN_ASSERT(*bytesRead == file.size); + DQN_ASSERTM(*bytesRead == file.size, "%zu != %zu", *bytesRead, file.size); cleanup: file.Close(); @@ -9677,7 +9669,7 @@ DQN_FILE_SCOPE i32 DqnAtomic_Add32(i32 volatile *src, i32 value) // XPlatform > #DqnOS // ================================================================================================= #if defined(DQN_IS_UNIX) -include +#include #endif void *DqnOS_VAlloc(isize size, void *baseAddress)