shithub: choc

Download patch

ref: 56d1487bd65daefced1fb2eaab54ff79c9295585
parent: 8b8b46cd3690fd3413b7b7b5902ff939a9843743
author: Turo Lamminen <turotl@gmail.com>
date: Tue Jan 30 14:04:04 EST 2018

i_sdlmusic: Read whole config file at once and then parse

--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -632,32 +632,38 @@
 
 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);
 
-    while (!feof(fs))
+    line = buffer;
+
+    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 @@
         }
 
         ++linenum;
+        line = next;
     }
 
-    fclose(fs);
+    Z_Free(buffer);
 
     return true;
 }