From 9b2834ef7e67b81b894d4c6844e487f3cac04d54 Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 18 Jul 2004 08:46:12 +0000 Subject: [PATCH] Improve handling of files with foreign newline sequences. git-svn-id: svn://svn.cc65.org/cc65/trunk@3163 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/scanner.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 981b43a68..1b87b78b4 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -466,6 +466,8 @@ static void NextChar (void) /* Check for end of line, read the next line if needed */ while (IFile->Line [IFile->Pos.Col] == '\0') { + unsigned Len, Removed; + /* End of current line reached, read next line */ if (fgets (IFile->Line, sizeof (IFile->Line), IFile->F) == 0) { /* End of file. Add an empty line to the listing. This is a @@ -476,6 +478,22 @@ static void NextChar (void) return; } + /* For better handling of files with unusual line endings (DOS + * files that are accidently translated on Unix for example), + * first remove all whitespace at the end, then add a single + * newline. + */ + Len = strlen (IFile->Line); + Removed = 0; + while (Len > 0 && IsSpace (IFile->Line[Len-1])) { + ++Removed; + --Len; + } + if (Removed) { + IFile->Line[Len+0] = '\n'; + IFile->Line[Len+1] = '\0'; + } + /* One more line */ IFile->Pos.Line++; IFile->Pos.Col = 0; -- 2.39.5