shithub: choc

Download patch

ref: 67e05587dd06ec18a1d1a9c6e877a3e9f78f5b23
parent: b5b2cea39c0650ed34f5385ed1fe175d431f604e
author: Simon Howard <fraggle@gmail.com>
date: Sat Dec 26 19:11:18 EST 2009

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

--- a/src/d_iwad.c
+++ b/src/d_iwad.c
@@ -338,40 +338,83 @@
     }
 }
 
+// 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<arrlen(iwads); ++i) 
     {
-        char *filename; 
-        char *iwadname;
+        filename = CheckDirectoryHasIWAD(dir, DEH_String(iwads[i].name));
 
-        iwadname = DEH_String(iwads[i].name);
-        
-        filename = malloc(strlen(dir) + strlen(iwadname) + 3);
-
-        if (!strcmp(dir, "."))
+        if (filename != NULL)
         {
-            strcpy(filename, iwadname);
-        }
-        else
-        {
-            sprintf(filename, "%s%c%s", dir, DIR_SEPARATOR, iwadname);
-        }
-
-        if (M_FileExists(filename))
-        {
             CheckChex(iwads[i].name);
             gamemission = iwads[i].mission;
 
             return filename;
         }
-
-        free(filename);
     }
 
     return NULL;
@@ -525,7 +568,6 @@
 {
     char *buf;
     int i;
-    boolean exists;
     
     // Absolute path?
 
@@ -540,14 +582,21 @@
 
     for (i=0; i<num_iwad_dirs; ++i)
     {
+        // As a special case, if this is in DOOMWADDIR or DOOMWADPATH,
+        // the "directory" may actually refer directly to an IWAD
+        // file.
+
+        if (DirIsFile(iwad_dirs[i], name) && M_FileExists(iwad_dirs[i]))
+        {
+            return strdup(iwad_dirs[i]);
+        }
+
         // Construct a string for the full path
 
         buf = malloc(strlen(iwad_dirs[i]) + strlen(name) + 5);
         sprintf(buf, "%s%c%s", iwad_dirs[i], DIR_SEPARATOR, name);
 
-        exists = M_FileExists(buf);
-
-        if (exists)
+        if (M_FileExists(buf))
         {
             return buf;
         }