foxygit / doom Log in
commit 56d1487bd65daefced1fb2eaab54ff79c9295585
Author:     Turo Lamminen <turotl@gmail.com>
AuthorDate: Tue Jan 30 19:04:04 2018 +0200
Commit:     Turo Lamminen <turotl@gmail.com>
CommitDate: Sat Mar 17 16:22:04 2018 +0200

    i_sdlmusic: Read whole config file at once and then parse
---
 src/i_sdlmusic.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index 412fbf9d..4b8574ea 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -632,31 +632,37 @@ static char *ParseSubstituteLine(char *filename, char *line)

 static boolean ReadSubstituteConfig(char *filename)
 {
-    char line[128];
-    FILE *fs;
-    char *error;
+    char *buffer;
+    char *line;
     int linenum = 1;
-//    int old_subst_music_len;
-
-    fs = fopen(filename, "r");

-    if (fs == NULL)
+    // This unnecessarily opens the file twice...
+    if (!M_FileExists(filename))
     {
         return false;
     }

-//    old_subst_music_len = subst_music_len;
+    M_ReadFile(filename, (byte **) &buffer);
+
+    line = buffer;

-    while (!feof(fs))
+    while (line != NULL)
     {
-        char *retval;
-        M_StringCopy(line, "", sizeof(line));
-        retval = fgets(line, sizeof(line), fs);
+        char *error;
+        char *next;

-        if (retval == NULL)
+        // find end of line
+        char *eol = strchr(line, '\n');
+        if (eol != NULL)
         {
-            fprintf(stderr, "%s:%i: Unexpected end of file\n", filename, linenum);
-            break;
+            // change the newline into NUL
+            *eol = '\0';
+            next = eol + 1;
+        }
+        else
+        {
+            // end of buffer
+            next = NULL;
         }

         error = ParseSubstituteLine(filename, line);
@@ -667,9 +673,10 @@ static boolean ReadSubstituteConfig(char *filename)
         }

         ++linenum;
+        line = next;
     }

-    fclose(fs);
+    Z_Free(buffer);

     return true;
 }