From 34c5da39e25a4ecffea860d08086861c31b93d8b Mon Sep 17 00:00:00 2001 From: Louis-Antoine Date: Tue, 19 May 2020 20:00:58 +0200 Subject: [PATCH] Create FIL_ConvertTextFileToBinary --- src/m_misc.c | 38 ++++++++++++++++++++++++++++++++++++++ src/m_misc.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/src/m_misc.c b/src/m_misc.c index 920a13198..c527d2296 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -297,6 +297,44 @@ size_t FIL_ReadFileTag(char const *name, UINT8 **buffer, INT32 tag) return length; } +/** Makes a copy of a text file with all newlines converted into LF newlines. + * + * \param textfilename The name of the source file + * \param binfilename The name of the destination file + */ +boolean FIL_ConvertTextFileToBinary(const char *textfilename, const char *binfilename) +{ + FILE *textfile; + FILE *binfile; + UINT8 buffer[1024]; + size_t count; + boolean success; + + textfile = fopen(textfilename, "r"); + if (!textfile) + return false; + + binfile = fopen(binfilename, "wb"); + if (!binfile) + { + fclose(textfile); + return false; + } + + do + { + count = fread(buffer, 1, sizeof(buffer), textfile); + fwrite(buffer, 1, count, binfile); + } while (count); + + success = !(ferror(textfile) || ferror(binfile)); + + fclose(textfile); + fclose(binfile); + + return success; +} + /** Check if the filename exists * * \param name Filename to check. diff --git a/src/m_misc.h b/src/m_misc.h index d64faea59..dbded37d0 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -48,6 +48,8 @@ boolean FIL_WriteFile(char const *name, const void *source, size_t length); size_t FIL_ReadFileTag(char const *name, UINT8 **buffer, INT32 tag); #define FIL_ReadFile(n, b) FIL_ReadFileTag(n, b, PU_STATIC) +boolean FIL_ConvertTextFileToBinary(const char *textfilename, const char *binfilename); + boolean FIL_FileExists(const char *name); boolean FIL_WriteFileOK(char const *name); boolean FIL_ReadFileOK(char const *name);