From 67e05587dd06ec18a1d1a9c6e877a3e9f78f5b23 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 27 Dec 2009 00:11:18 +0000 Subject: Allow DOOMWADDIR/DOOMWADPATH to contain the complete path to IWAD files, as well as directories in which to search for IWAD files. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1766 --- src/d_iwad.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 22 deletions(-) diff --git a/src/d_iwad.c b/src/d_iwad.c index 0e48420d..729aeee9 100644 --- a/src/d_iwad.c +++ b/src/d_iwad.c @@ -338,40 +338,83 @@ static void CheckChex(char *iwad_name) } } +// Returns true if the specified path is a path to a file +// of the specified name. + +static boolean DirIsFile(char *path, char *filename) +{ + size_t path_len; + size_t filename_len; + + printf("%s, %s\n", path, filename); + + path_len = strlen(path); + filename_len = strlen(filename); + + return path_len >= filename_len + 1 + && path[path_len - filename_len - 1] == DIR_SEPARATOR + && !strcasecmp(&path[path_len - filename_len], filename); +} + +// Check if the specified directory contains the specified IWAD +// file, returning the full path to the IWAD if found, or NULL +// if not found. + +static char *CheckDirectoryHasIWAD(char *dir, char *iwadname) +{ + char *filename; + + // As a special case, the "directory" may refer directly to an + // IWAD file if the path comes from DOOMWADDIR or DOOMWADPATH. + + if (DirIsFile(dir, iwadname) && M_FileExists(dir)) + { + return strdup(dir); + } + + // Construct the full path to the IWAD if it is located in + // this directory, and check if it exists. + + filename = malloc(strlen(dir) + strlen(iwadname) + 3); + + if (!strcmp(dir, ".")) + { + strcpy(filename, iwadname); + } + else + { + sprintf(filename, "%s%c%s", dir, DIR_SEPARATOR, iwadname); + } + + if (M_FileExists(filename)) + { + return filename; + } + + free(filename); + + return NULL; +} + // Search a directory to try to find an IWAD // Returns the location of the IWAD if found, otherwise NULL. static char *SearchDirectoryForIWAD(char *dir) { + char *filename; size_t i; for (i=0; i