ref: f3ce0c7fdd434b100fd0aa51066602c1a8a7599d
parent: e03c281d8363e0f7ee82b89de3196869ae3ee9f0
parent: 56d1487bd65daefced1fb2eaab54ff79c9295585
author: Simon Howard <[email protected]>
date: Sat Mar 17 19:44:15 EDT 2018
Merge pull request #986 from turol/musicsubst Read whole music substition config file at once
--- 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;
}
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -217,7 +217,7 @@
length = M_FileLength(handle);
- buf = Z_Malloc (length, PU_STATIC, NULL);
+ buf = Z_Malloc (length + 1, PU_STATIC, NULL);
count = fread(buf, 1, length, handle);
fclose (handle);
@@ -224,6 +224,7 @@
if (count < length)
I_Error ("Couldn't read file %s", name);
+ buf[length] = '\0';
*buffer = buf;
return length;
}