Some cleanup in general.

This commit is contained in:
Jaime Passos 2019-12-30 02:07:39 -03:00
parent c303b1a435
commit 757480f219

View file

@ -1464,9 +1464,9 @@ static UINT16 W_CheckForMusicDefInPwad(UINT16 wadid)
void S_LoadMusicDefs(UINT16 wadnum) void S_LoadMusicDefs(UINT16 wadnum)
{ {
UINT16 lump; UINT16 lumpnum;
char *buf; char *lump, *buf;
char *buf2; char *musdeftext;
char *stoken; char *stoken;
char *value; char *value;
char *textline; char *textline;
@ -1475,21 +1475,27 @@ void S_LoadMusicDefs(UINT16 wadnum)
musicdef_t *def = NULL; musicdef_t *def = NULL;
UINT16 line = 1; // for better error msgs UINT16 line = 1; // for better error msgs
lump = W_CheckForMusicDefInPwad(wadnum); lumpnum = W_CheckForMusicDefInPwad(wadnum);
if (lump == INT16_MAX) if (lumpnum == INT16_MAX)
return; return;
buf = W_CacheLumpNumPwad(wadnum, lump, PU_CACHE); lump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE);
size = W_LumpLengthPwad(wadnum, lump); size = W_LumpLengthPwad(wadnum, lumpnum);
// Null-terminated MUSICDEF lump.
musdeftext = malloc(size+1);
if (!musdeftext)
I_Error("S_LoadMusicDefs: No more free memory for the parser\n");
M_Memcpy(musdeftext, lump, size);
musdeftext[size] = '\0';
// for strtok // for strtok
buf2 = malloc(size+1); buf = malloc(size+1);
if (!buf2) if (!buf)
I_Error("S_LoadMusicDefs: No more free memory\n"); I_Error("S_LoadMusicDefs: No more free memory for the parser\n");
M_Memcpy(buf2,buf,size); M_Memcpy(buf, musdeftext, size+1);
buf2[size] = '\0';
stoken = strtok (buf2, "\r\n "); stoken = strtok(buf, "\r\n ");
// Find music def // Find music def
while (stoken) while (stoken)
{ {
@ -1589,17 +1595,10 @@ skip_lump:
if (!brokenline) if (!brokenline)
{ {
// strtok returns memory that already belongs to the input string. // strtok returns memory that already belongs to the input string.
value = buf + (value - buf2); value = musdeftext + (value - buf);
// Find the length of the line. // Find the length of the line.
size = 0; size = strcspn(value, "\r\n");
for (;;)
{
char c = value[size];
if (c == '\n' || c == '\r' || c == '\0')
break;
size++;
}
// Copy the line. // Copy the line.
textline = malloc(size+1); textline = malloc(size+1);
@ -1619,7 +1618,8 @@ skip_lump:
{ {
CONS_Alert(CONS_ERROR, "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); CONS_Alert(CONS_ERROR, "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line);
free(textline); free(textline);
free(buf2); free(buf);
free(musdeftext);
return; return;
} }
@ -1663,7 +1663,7 @@ skip_lump:
CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line);
} }
// Free the temporary line from memory. // Free the temporary text line from memory.
free(textline); free(textline);
skip_field: skip_field:
@ -1672,7 +1672,8 @@ skip_field:
} }
} }
free(buf2); free(buf);
free(musdeftext);
return; return;
} }