From 8a643c92bbac9460ab0516d9b438b3b93f531262 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Thu, 23 Jun 2022 13:36:43 +0500 Subject: [PATCH] file loading: LoadFileText reimplementation --- src/raudio.c | 2 +- src/utils.c | 40 ++++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index 1a2186ade..f13eb786d 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -2438,9 +2438,9 @@ static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead if (ferror(file)) { - TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); RL_FREE(data); data = NULL; + TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); break; } } diff --git a/src/utils.c b/src/utils.c index be6adceaf..c74da2ea1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -210,9 +210,9 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead) if (ferror(file)) { - TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); RL_FREE(data); data = NULL; + TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); break; } } @@ -334,28 +334,32 @@ char *LoadFileText(const char *fileName) if (file != NULL) { - // WARNING: When reading a file as 'text' file, - // text mode causes carriage return-linefeed translation... - // ...but using fseek() should return correct byte-offset - fseek(file, 0, SEEK_END); - unsigned int size = (unsigned int)ftell(file); - fseek(file, 0, SEEK_SET); + size_t charsRead = 0, charsAllocated = 0; - if (size > 0) + while (!feof(file)) { - text = (char *)RL_MALLOC((size + 1)*sizeof(char)); - unsigned int count = (unsigned int)fread(text, sizeof(char), size, file); + // TODO: Should granuality of forward allocation/reading be assumed to be 32 + // or user should be given the ability to set it in config? + if (charsAllocated <= charsRead) text = (char *)RL_REALLOC(text, charsAllocated += 32); - // WARNING: \r\n is converted to \n on reading, so, - // read bytes count gets reduced by the number of lines - if (count < size) text = RL_REALLOC(text, count + 1); + // NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements] + // NOTE: Platform specific newlines are handled in implementation specific manner + charsRead += fread(&text[charsRead], sizeof(char), 32, file); - // Zero-terminate the string - text[count] = '\0'; - - TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName); + if (ferror(file)) + { + RL_FREE(text); + text = NULL; + TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); + break; + } + } + if (text != NULL) + { + if (charsRead == charsAllocated) text = (char *)RL_REALLOC(text, charsAllocated += 1); + text[charsRead] = '\0'; + TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName); } - else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName); fclose(file); }