shithub: choc

Download patch

ref: b34edc6d3ee93b5ea6ffc6c6994ad38e54384221
parent: ae8c1ed2e0eb1a9292692caf0b5af82217228b3d
author: Simon Howard <fraggle@soulsphere.org>
date: Sun Sep 9 14:44:18 EDT 2018

Convert various bits of code to M_{Dir,Base}Name.

The new functions significantly improve readability and I'm pretty
sure that most of these changes produce logic that is equivalent to
the existing logic.

--- a/src/d_iwad.c
+++ b/src/d_iwad.c
@@ -450,15 +450,8 @@
 
 static boolean DirIsFile(const char *path, const char *filename)
 {
-    size_t path_len;
-    size_t filename_len;
-
-    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);
+    return strchr(path, DIR_SEPARATOR) != NULL
+        && !strcasecmp(M_BaseName(path), filename);
 }
 
 // Check if the specified directory contains the specified IWAD
@@ -536,15 +529,8 @@
 {
     size_t i;
     GameMission_t mission;
-    char *p;
 
-    p = strrchr(name, DIR_SEPARATOR);
-
-    if (p != NULL)
-    {
-        name = p + 1;
-    }
-
+    name = M_BaseName(name);
     mission = none;
 
     for (i=0; i<arrlen(iwads); ++i)
--- a/src/doom/d_main.c
+++ b/src/doom/d_main.c
@@ -1162,23 +1162,12 @@
     if (gameversion == exe_chex)
     {
         char *chex_deh = NULL;
-        char *sep;
+        char *dirname;
 
         // Look for chex.deh in the same directory as the IWAD file.
-        sep = strrchr(iwadfile, DIR_SEPARATOR);
-
-        if (sep != NULL)
-        {
-            size_t chex_deh_len = strlen(iwadfile) + 9;
-            chex_deh = malloc(chex_deh_len);
-            M_StringCopy(chex_deh, iwadfile, chex_deh_len);
-            chex_deh[sep - iwadfile + 1] = '\0';
-            M_StringConcat(chex_deh, "chex.deh", chex_deh_len);
-        }
-        else
-        {
-            chex_deh = M_StringDuplicate("chex.deh");
-        }
+        dirname = M_DirName(iwadfile);
+        chex_deh = M_StringJoin(dirname, DIR_SEPARATOR_S, "chex.deh");
+        free(dirname);
 
         // If the dehacked patch isn't found, try searching the WAD
         // search path instead.  We might find it...
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -521,17 +521,8 @@
 
     // Copy config filename and cut off the filename to just get the
     // parent dir.
-    basedir = M_StringDuplicate(base_filename);
-    p = strrchr(basedir, DIR_SEPARATOR);
-    if (p != NULL)
-    {
-        p[1] = '\0';
-        result = M_StringJoin(basedir, path, NULL);
-    }
-    else
-    {
-        result = M_StringDuplicate(path);
-    }
+    basedir = M_DirName(base_filename);
+    result = M_StringJoin(basedir, DIR_SEPARATOR_S, path, NULL);
     free(basedir);
     free(path);
 
@@ -829,14 +820,9 @@
         return false;
     }
 
-    p = strrchr(timidity_cfg_path, DIR_SEPARATOR);
-    if (p != NULL)
-    {
-        path = M_StringDuplicate(timidity_cfg_path);
-        path[p - timidity_cfg_path] = '\0';
-        fprintf(fstream, "dir %s\n", path);
-        free(path);
-    }
+    path = M_DirName(timidity_cfg_path);
+    fprintf(fstream, "dir %s\n", path);
+    free(path);
 
     fprintf(fstream, "source %s\n", timidity_cfg_path);
     fclose(fstream);
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -245,17 +245,6 @@
 
 char *M_GetExecutableName(void)
 {
-    char *sep;
-
-    sep = strrchr(myargv[0], DIR_SEPARATOR);
-
-    if (sep == NULL)
-    {
-        return myargv[0];
-    }
-    else
-    {
-        return sep + 1;
-    }
+    return M_BaseName(myargv[0]);
 }