shithub: choc

Download patch

ref: 63ca127bf99b58295454bbfd29ba5fa644285847
parent: 3524a67dd3c7d48a172c83d3ae170a7176fb3cfa
author: Michael Day <mikeguy42@gmail.com>
date: Thu Aug 5 19:42:27 EDT 2021

deh: Handle orphan carriage returns

When encountering carriage returns during DeHacked parsing, sniff the
next character to see if it's a linefeed. If so, filter out the \r and
return the \n as before. If it's not a linefeed, return the "orphan" \r.

Fixes #1375.

--- a/src/deh_io.c
+++ b/src/deh_io.c
@@ -175,10 +175,17 @@
 int DEH_GetChar(deh_context_t *context)
 {
     int result = 0;
+    boolean last_was_cr = false;
 
-    // Read characters, but ignore carriage returns
-    // Essentially this is a DOS->Unix conversion
+    // Track the current line number
 
+    if (context->last_was_newline)
+    {
+        ++context->linenum;
+    }
+
+    // Read characters, converting CRLF to LF
+
     do
     {
         switch (context->type)
@@ -191,14 +198,27 @@
                 result = DEH_GetCharLump(context);
                 break;
         }
-    } while (result == '\r');
 
-    // Track the current line number
+        // Handle \r characters not paired with \n
+        if (last_was_cr && result != '\n')
+        {
+            switch (context->type)
+            {
+                case DEH_INPUT_FILE:
+                    ungetc(result, context->stream);
+                    break;
 
-    if (context->last_was_newline)
-    {
-        ++context->linenum;
-    }
+                case DEH_INPUT_LUMP:
+                    --context->input_buffer_pos;
+                    break;
+            }
+
+            return '\r';
+        }
+
+        last_was_cr = result == '\r';
+
+    } while (last_was_cr);
 
     context->last_was_newline = result == '\n';