Skip to content

Commit 2ac2f64

Browse files
dynamically grow the iwad_dirs[] array (#2113)
* dynamically grow the iwad_dirs[] array * short-cut AddIWADDir(...) -> array_push(iwad_dirs, ...)
1 parent 3171589 commit 2ac2f64

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

src/d_iwad.c

+26-43
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,11 @@ static const char *const gamemode_str[] = {
5858
"Unknown mode"
5959
};
6060

61-
// "128 IWAD search directories should be enough for anybody".
62-
63-
#define MAX_IWAD_DIRS 128
64-
6561
// Array of locations to search for IWAD files
62+
#define M_ARRAY_INIT_CAPACITY 32
63+
#include "m_array.h"
6664

67-
static boolean iwad_dirs_built = false;
68-
char *iwad_dirs[MAX_IWAD_DIRS];
69-
int num_iwad_dirs = 0;
70-
71-
static void AddIWADDir(char *dir)
72-
{
73-
if (num_iwad_dirs < MAX_IWAD_DIRS)
74-
{
75-
iwad_dirs[num_iwad_dirs] = dir;
76-
++num_iwad_dirs;
77-
}
78-
}
65+
static char **iwad_dirs;
7966

8067
// Return the path where the executable lies -- Lee Killough
8168

@@ -355,7 +342,7 @@ static void CheckUninstallStrings(void)
355342
{
356343
path = unstr + strlen(UNINSTALLER_STRING);
357344

358-
AddIWADDir(path);
345+
array_push(iwad_dirs, path);
359346
}
360347
}
361348
}
@@ -383,7 +370,7 @@ static void CheckInstallRootPaths(void)
383370
{
384371
subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
385372
root_path_subdirs[j]);
386-
AddIWADDir(subpath);
373+
array_push(iwad_dirs, subpath);
387374
}
388375

389376
free(install_path);
@@ -410,7 +397,7 @@ static void CheckSteamEdition(void)
410397
subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
411398
steam_install_subdirs[i]);
412399

413-
AddIWADDir(subpath);
400+
array_push(iwad_dirs, subpath);
414401
}
415402

416403
free(install_path);
@@ -423,13 +410,13 @@ static void CheckDOSDefaults(void)
423410
// These are the default install directories used by the deice
424411
// installer program:
425412

426-
AddIWADDir("\\doom2"); // Doom II
427-
AddIWADDir("\\plutonia"); // Final Doom
428-
AddIWADDir("\\tnt");
429-
AddIWADDir("\\doom_se"); // Ultimate Doom
430-
AddIWADDir("\\doom"); // Shareware / Registered Doom
431-
AddIWADDir("\\dooms"); // Shareware versions
432-
AddIWADDir("\\doomsw");
413+
array_push(iwad_dirs, "\\doom2"); // Doom II
414+
array_push(iwad_dirs, "\\plutonia"); // Final Doom
415+
array_push(iwad_dirs, "\\tnt");
416+
array_push(iwad_dirs, "\\doom_se"); // Ultimate Doom
417+
array_push(iwad_dirs, "\\doom"); // Shareware / Registered Doom
418+
array_push(iwad_dirs, "\\dooms"); // Shareware versions
419+
array_push(iwad_dirs, "\\doomsw");
433420
}
434421

435422
#endif
@@ -464,7 +451,7 @@ static void AddIWADPath(const char *path, const char *suffix)
464451
// as another iwad dir
465452
*p = '\0';
466453

467-
AddIWADDir(M_StringJoin(left, suffix));
454+
array_push(iwad_dirs, M_StringJoin(left, suffix));
468455
left = p + 1;
469456
}
470457
else
@@ -473,7 +460,7 @@ static void AddIWADPath(const char *path, const char *suffix)
473460
}
474461
}
475462

476-
AddIWADDir(M_StringJoin(left, suffix));
463+
array_push(iwad_dirs, M_StringJoin(left, suffix));
477464

478465
free(dup_path);
479466
}
@@ -491,7 +478,7 @@ static void AddXdgDirs(void)
491478
// We support $XDG_DATA_HOME/games/doom (which will usually be
492479
// ~/.local/share/games/doom) as a user-writeable extension to
493480
// the usual /usr/share/games/doom location.
494-
AddIWADDir(M_StringJoin(env, "/games/doom"));
481+
array_push(iwad_dirs, M_StringJoin(env, "/games/doom"));
495482

496483
// Quote:
497484
// > $XDG_DATA_DIRS defines the preference-ordered set of base
@@ -550,23 +537,23 @@ void BuildIWADDirList(void)
550537
{
551538
char *env;
552539

553-
if (iwad_dirs_built)
540+
if (array_size(iwad_dirs) > 0)
554541
{
555542
return;
556543
}
557544

558545
// Look in the current directory. Doom always does this.
559-
AddIWADDir(".");
546+
array_push(iwad_dirs, ".");
560547

561548
// Next check the directory where the executable is located. This might
562549
// be different from the current directory.
563-
AddIWADDir(D_DoomExeDir());
550+
array_push(iwad_dirs, D_DoomExeDir());
564551

565552
// Add DOOMWADDIR if it is in the environment
566553
env = M_getenv("DOOMWADDIR");
567554
if (env != NULL)
568555
{
569-
AddIWADDir(env);
556+
array_push(iwad_dirs, env);
570557
}
571558

572559
// Add dirs from DOOMWADPATH:
@@ -578,7 +565,7 @@ void BuildIWADDirList(void)
578565

579566
// [FG] Add plain HOME directory
580567
env = M_HomeDir();
581-
AddIWADDir(env);
568+
array_push(iwad_dirs, env);
582569

583570
#ifdef _WIN32
584571

@@ -595,10 +582,6 @@ void BuildIWADDirList(void)
595582
AddSteamDirs();
596583
# endif
597584
#endif
598-
599-
// Don't run this function again.
600-
601-
iwad_dirs_built = true;
602585
}
603586

604587
//
@@ -609,7 +592,6 @@ char *D_FindWADByName(const char *name)
609592
{
610593
char *path;
611594
char *probe;
612-
int i;
613595

614596
// Absolute path?
615597

@@ -623,22 +605,23 @@ char *D_FindWADByName(const char *name)
623605

624606
// Search through all IWAD paths for a file with the given name.
625607

626-
for (i = 0; i < num_iwad_dirs; ++i)
608+
char **dir;
609+
array_foreach(dir, iwad_dirs)
627610
{
628611
// As a special case, if this is in DOOMWADDIR or DOOMWADPATH,
629612
// the "directory" may actually refer directly to an IWAD
630613
// file.
631614

632-
probe = M_FileCaseExists(iwad_dirs[i]);
633-
if (DirIsFile(iwad_dirs[i], name) && probe != NULL)
615+
probe = M_FileCaseExists(*dir);
616+
if (DirIsFile(*dir, name) && probe != NULL)
634617
{
635618
return probe;
636619
}
637620
free(probe);
638621

639622
// Construct a string for the full path
640623

641-
path = M_StringJoin(iwad_dirs[i], DIR_SEPARATOR_S, name);
624+
path = M_StringJoin(*dir, DIR_SEPARATOR_S, name);
642625

643626
probe = M_FileCaseExists(path);
644627
if (probe != NULL)

0 commit comments

Comments
 (0)