rework AddDefaultExtension() (#2124)

* rework AddDefaultExtension()

* simplify: assume `ext` must start with a dot
This commit is contained in:
Fabian Greffrath 2025-01-07 17:02:17 +01:00 committed by GitHub
parent af76ea901c
commit bfe338f8fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 31 deletions

View File

@ -754,8 +754,7 @@ char *D_FindIWADFile(void)
char *iwadfile = myargv[iwadparm + 1]; char *iwadfile = myargv[iwadparm + 1];
char *file = malloc(strlen(iwadfile) + 5); char *file = AddDefaultExtension(iwadfile, ".wad");
AddDefaultExtension(strcpy(file, iwadfile), ".wad");
result = D_FindWADByName(file); result = D_FindWADByName(file);

View File

@ -973,8 +973,7 @@ void FindResponseFile (void)
// READ THE RESPONSE FILE INTO MEMORY // READ THE RESPONSE FILE INTO MEMORY
// killough 10/98: add default .rsp extension // killough 10/98: add default .rsp extension
char *filename = malloc(strlen(myargv[i])+5); char *filename = AddDefaultExtension(&myargv[i][1],".rsp");
AddDefaultExtension(strcpy(filename,&myargv[i][1]),".rsp");
handle = M_fopen(filename,"rb"); handle = M_fopen(filename,"rb");
if (!handle) if (!handle)
@ -1336,14 +1335,15 @@ static void D_ProcessDehCommandLine(void)
if (deh) if (deh)
{ {
char *probe; char *probe;
char *file = malloc(strlen(myargv[p]) + 5); // killough char *file = AddDefaultExtension(myargv[p], ".bex");
AddDefaultExtension(strcpy(file, myargv[p]), ".bex");
probe = D_TryFindWADByName(file); probe = D_TryFindWADByName(file);
free(file);
if (M_access(probe, F_OK)) // nope if (M_access(probe, F_OK)) // nope
{ {
free(probe); free(probe);
AddDefaultExtension(strcpy(file, myargv[p]), ".deh"); file = AddDefaultExtension(myargv[p], ".deh");
probe = D_TryFindWADByName(file); probe = D_TryFindWADByName(file);
free(file);
if (M_access(probe, F_OK)) // still nope if (M_access(probe, F_OK)) // still nope
{ {
free(probe); free(probe);
@ -1355,7 +1355,6 @@ static void D_ProcessDehCommandLine(void)
// (apparently, this was never removed after Boom beta-killough) // (apparently, this was never removed after Boom beta-killough)
ProcessDehFile(probe, D_dehout(), 0); // killough 10/98 ProcessDehFile(probe, D_dehout(), 0); // killough 10/98
free(probe); free(probe);
free(file);
} }
} }
// ty 03/09/98 end of do dehacked stuff // ty 03/09/98 end of do dehacked stuff

View File

@ -3981,9 +3981,13 @@ void G_RecordDemo(char *name)
demo_insurance = 0; demo_insurance = 0;
usergame = false; usergame = false;
demoname_size = strlen(name) + 5 + 6; // [crispy] + 6 for "-00000" if (demoname)
{
free(demoname);
}
demoname = AddDefaultExtension(name, ".lmp"); // 1/18/98 killough
demoname_size = strlen(demoname) + 6; // [crispy] + 6 for "-00000"
demoname = I_Realloc(demoname, demoname_size); demoname = I_Realloc(demoname, demoname_size);
AddDefaultExtension(strcpy(demoname, name), ".lmp"); // 1/18/98 killough
for(; j <= 99999 && !M_access(demoname, F_OK); ++j) for(; j <= 99999 && !M_access(demoname, F_OK); ++j)
{ {

View File

@ -562,30 +562,19 @@ void M_CopyLumpName(char *dest, const char *src)
// //
// 1/18/98 killough: adds a default extension to a path // 1/18/98 killough: adds a default extension to a path
// Note: Backslashes are treated specially, for MS-DOS.
// //
char *AddDefaultExtension(char *path, const char *ext) char *AddDefaultExtension(const char *path, const char *ext)
{ {
char *p = path; if (strrchr(M_BaseName(path), '.') != NULL)
while (*p++)
;
while (p-- > path && *p != '/' && *p != '\\')
{ {
if (*p == '.') // path already has an extension
{ return M_StringDuplicate(path);
return path;
}
} }
else
if (*ext != '.')
{ {
strcat(path, "."); return M_StringJoin(path, ext);
} }
return strcat(path, ext);
} }
// //

View File

@ -56,7 +56,7 @@ int M_vsnprintf(char *buf, size_t buf_len, const char *s, va_list args)
int M_snprintf(char *buf, size_t buf_len, const char *s, ...) PRINTF_ATTR(3, 4); int M_snprintf(char *buf, size_t buf_len, const char *s, ...) PRINTF_ATTR(3, 4);
void M_CopyLumpName(char *dest, const char *src); void M_CopyLumpName(char *dest, const char *src);
char *AddDefaultExtension(char *path, const char *ext); char *AddDefaultExtension(const char *path, const char *ext);
boolean M_WriteFile(const char *name, void *source, int length); boolean M_WriteFile(const char *name, void *source, int length);
int M_ReadFile(const char *name, byte **buffer); int M_ReadFile(const char *name, byte **buffer);
boolean M_StringToDigest(const char *string, byte *digest, int size); boolean M_StringToDigest(const char *string, byte *digest, int size);

View File

@ -1055,9 +1055,7 @@ void R_InitTranMap(int progress)
int p = M_CheckParmWithArgs("-dumptranmap", 1); int p = M_CheckParmWithArgs("-dumptranmap", 1);
if (p > 0) if (p > 0)
{ {
char *path = malloc(strlen(myargv[p + 1]) + 5); char *path = AddDefaultExtension(myargv[p + 1], ".lmp");
strcpy(path, myargv[p + 1]);
AddDefaultExtension(path, ".lmp");
M_WriteFile(path, main_tranmap, 256 * 256); M_WriteFile(path, main_tranmap, 256 * 256);