foxygit / doom Log in
commit 51d5fc29b0b7cda79b593e499ff67a5960f6055b
Author:     Fabian Greffrath <fabian@greffrath.com>
AuthorDate: Tue Apr 21 07:46:16 2015 +0200
Commit:     Fabian Greffrath <fabian@greffrath.com>
CommitDate: Tue Apr 21 07:46:16 2015 +0200

    deh: fix parsing of last lines in files without newline at EOF

    Chocolate Doom fails to properly parse the last line in DEHACKED files
    that are missing a newline character at the end of the file (which
    might have been a common sickness to DOS editors).

    This is, because DEH_GetChar() returns -1 at EOF and DEH_ReadLine() in
    turn immediately returns NULL if the result of (DEH_GetChar() < 0),
    regardless of the content of the readbuffer. It only returns a pointer
    to the content of the readbuffer if DEH_GetChar() returns '\n' which
    is, however, impossible if there is no such character before the file
    ends.

    Now DEH_ReadLine() only returns NULL if the readbuffer is empty at the
    end of the file and returns a valid pointer else.

    Fixes #531
---
 src/deh_io.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/deh_io.c b/src/deh_io.c
index c5e81b0a..e8beb438 100644
--- a/src/deh_io.c
+++ b/src/deh_io.c
@@ -235,7 +235,7 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)
     {
         c = DEH_GetChar(context);

-        if (c < 0)
+        if (c < 0 && pos == 0)
         {
             // end of file

@@ -273,7 +273,7 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)

         // blanks before the backslash are included in the string
         // but indentation after the linefeed is not
-        if (escaped && isspace(c) && c != '\n')
+        if (escaped && c >= 0 && isspace(c) && c != '\n')
         {
             continue;
         }
@@ -282,7 +282,7 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)
             escaped = false;
         }

-        if (c == '\n')
+        if (c == '\n' || c < 0)
         {
             // end of line: a full line has been read