foxygit / doom Log in
commit b34edc6d3ee93b5ea6ffc6c6994ad38e54384221
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sun Sep 9 18:44:18 2018 -0400
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sun Sep 9 18:44:18 2018 -0400

    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.
---
 src/d_iwad.c      | 20 +++-----------------
 src/doom/d_main.c | 19 ++++---------------
 src/i_sdlmusic.c  | 24 +++++-------------------
 src/m_argv.c      | 13 +------------
 4 files changed, 13 insertions(+), 63 deletions(-)

diff --git a/src/d_iwad.c b/src/d_iwad.c
index fd3a8bd5..02984cd7 100644
--- a/src/d_iwad.c
+++ b/src/d_iwad.c
@@ -450,15 +450,8 @@ static void CheckDOSDefaults(void)

 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 @@ static GameMission_t IdentifyIWADByName(char *name, int mask)
 {
     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)
diff --git a/src/doom/d_main.c b/src/doom/d_main.c
index d134831c..4f143e3b 100644
--- a/src/doom/d_main.c
+++ b/src/doom/d_main.c
@@ -1162,23 +1162,12 @@ static void LoadIwadDeh(void)
     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...
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index ef286f6e..f400bd7a 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -521,17 +521,8 @@ static char *GetFullPath(char *base_filename, char *path)

     // 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 @@ static boolean WriteWrapperTimidityConfig(char *write_path)
         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);
diff --git a/src/m_argv.c b/src/m_argv.c
index 43287b53..f22cd8c6 100644
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -245,17 +245,6 @@ void M_FindResponseFile(void)

 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]);
 }