From fedaff82d822f15abd9c0f1dd4ef6b5afbef29b7 Mon Sep 17 00:00:00 2001 From: Nostress767 <72526817+Nostress767@users.noreply.github.com> Date: Sat, 10 Feb 2024 01:52:08 -0300 Subject: [PATCH] Fix build on Windows (MinGW) CR's from the template file need to also be stripped, since tinyxml2 strips them and we use their parser. Then, when writing back the file, use text mode (not binary). --- source/glbind_build.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/glbind_build.cpp b/source/glbind_build.cpp index 54fa11a..3d22562 100644 --- a/source/glbind_build.cpp +++ b/source/glbind_build.cpp @@ -125,9 +125,10 @@ glbResult glbOpenAndReadFileWithExtraData(const char* filePath, size_t* pFileSiz { glbResult result; FILE* pFile; - uint64_t fileSize; + uint64_t fileSize, originalFileSize; void* pFileData; - size_t bytesRead; + char* pFileCharData; + size_t bytesRead, readerIndex, writerIndex; /* Safety. */ if (pFileSizeOut) *pFileSizeOut = 0; @@ -166,6 +167,21 @@ glbResult glbOpenAndReadFileWithExtraData(const char* filePath, size_t* pFileSiz fclose(pFile); + originalFileSize = fileSize; + readerIndex = 0, writerIndex = 0; + pFileCharData = (char*)pFileData; /* (void*) to (char*) cast is well-defined */ + while(readerIndex < originalFileSize){ /* tinyxml2 strips CR's, so this is required for mixing with the parsed code */ + if(pFileCharData[readerIndex] == '\r' && pFileCharData[readerIndex + 1] == '\n'){ /* CR followed by LF */ + fileSize--; + } else{ + pFileCharData[writerIndex] = pFileCharData[readerIndex]; + writerIndex++; + } + readerIndex++; + } + + pFileData = realloc(pFileData, (size_t)fileSize + extraBytes); /* Reduce malloc'd size if necessary */ + if (pFileSizeOut) { *pFileSizeOut = (size_t)fileSize; } @@ -217,7 +233,7 @@ glbResult glbOpenAndWriteFile(const char* filePath, const void* pData, size_t da return GLB_INVALID_ARGS; } - result = glbFOpen(filePath, "wb", &pFile); + result = glbFOpen(filePath, "w", &pFile); /* Windows needs CRLF */ if (result != GLB_SUCCESS) { return GLB_FAILED_TO_OPEN_FILE; }