ref: 634751002d27808420111cf5781ee8a269c55c44
parent: 5ccf89a00d7be33b1934d22a9eb8d82a2be153c4
parent: 63ca127bf99b58295454bbfd29ba5fa644285847
author: Turo Lamminen <turol@users.noreply.github.com>
date: Sat Sep 4 12:20:10 EDT 2021
Merge pull request #1386 from mikeday0/choc_orphan_cr deh: Handle orphan carriage returns
--- 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';