From 0b55e95c796ce94399c20e7d38fbe4bfab695137 Mon Sep 17 00:00:00 2001 From: ewasylishen Date: Tue, 9 May 2017 00:01:41 +0000 Subject: [PATCH] Host_Loadgame_f: fix for arrow characters appearing in multiline messages on Windows, a regression introduced in the Host_Loadgame_f rewrite r1398 git-svn-id: svn+ssh://svn.code.sf.net/p/quakespasm/code/trunk@1405 af15c1b1-3010-417e-b628-4374ebc0bcbd --- quakespasm/Quake/common.c | 19 +++++++++++++------ quakespasm/Quake/common.h | 3 ++- quakespasm/Quake/host_cmd.c | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/quakespasm/Quake/common.c b/quakespasm/Quake/common.c index 3899154d..84968aa4 100644 --- a/quakespasm/Quake/common.c +++ b/quakespasm/Quake/common.c @@ -1872,13 +1872,17 @@ byte *COM_LoadMallocFile (const char *path, unsigned int *path_id) return COM_LoadFile (path, LOADFILE_MALLOC, path_id); } -byte *COM_LoadMallocFile_OSPath (const char *path, long *len_out) +byte *COM_LoadMallocFile_TextMode_OSPath (const char *path, long *len_out) { FILE *f; byte *data; - long len; + long len, actuallen; - f = fopen (path, "rb"); + // ericw -- this is used by Host_Loadgame_f. Translate CRLF to LF on load games, + // othewise multiline messages have a garbage character at the end of each line. + // TODO: could handle in a way that allows loading CRLF savegames on mac/linux + // without the junk characters appearing. + f = fopen (path, "rt"); if (f == NULL) return NULL; @@ -1889,15 +1893,18 @@ byte *COM_LoadMallocFile_OSPath (const char *path, long *len_out) data = (byte *) malloc (len + 1); if (data == NULL) return NULL; - if (fread (data, 1, len, f) != len) + + // (actuallen < len) if CRLF to LF translation was performed + actuallen = fread (data, 1, len, f); + if (ferror(f)) { free (data); return NULL; } - data[len] = '\0'; + data[actuallen] = '\0'; if (len_out != NULL) - *len_out = len; + *len_out = actuallen; return data; } diff --git a/quakespasm/Quake/common.h b/quakespasm/Quake/common.h index 1edf88bc..f59ddbe9 100644 --- a/quakespasm/Quake/common.h +++ b/quakespasm/Quake/common.h @@ -257,7 +257,8 @@ byte *COM_LoadMallocFile (const char *path, unsigned int *path_id); // Opens the given path directly, ignoring search paths. // Returns NULL on failure, or else a '\0'-terminated malloc'ed buffer. -byte *COM_LoadMallocFile_OSPath (const char *path, long *len_out); +// Loads in "t" mode so CRLF to LF translation is performed on Windows. +byte *COM_LoadMallocFile_TextMode_OSPath (const char *path, long *len_out); // Attempts to parse an int, followed by a newline. // Returns advanced buffer position. diff --git a/quakespasm/Quake/host_cmd.c b/quakespasm/Quake/host_cmd.c index cfb428da..969749a3 100644 --- a/quakespasm/Quake/host_cmd.c +++ b/quakespasm/Quake/host_cmd.c @@ -1147,7 +1147,7 @@ void Host_Loadgame_f (void) if (start != NULL) free (start); - start = (char *) COM_LoadMallocFile_OSPath(name, NULL); + start = (char *) COM_LoadMallocFile_TextMode_OSPath(name, NULL); if (start == NULL) { Con_Printf ("ERROR: couldn't open.\n");