shithub: choc

Download patch

ref: ae8c1ed2e0eb1a9292692caf0b5af82217228b3d
parent: de9163ef5383c13c48178a3a6746d820ad14e6f1
author: Simon Howard <[email protected]>
date: Sun Sep 9 14:36:47 EDT 2018

misc: Add M_{Dir,Base}Name utility functions.

These extract the directory / base filename, equivalent to the Unix
`dirname` / `basename` commands. Repeated code throughout the codebase
seems to reimplement this particular functionality.

--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -265,6 +265,45 @@
         || sscanf(str, " %d", result) == 1;
 }
 
+// Returns the directory portion of the given path, without the trailing
+// slash separator character. If no directory is described in the path,
+// the string "." is returned. In either case, the result is newly allocated
+// and must be freed by the caller after use.
+char *M_DirName(const char *path)
+{
+    char *p, *result;
+
+    p = strrchr(path, DIR_SEPARATOR);
+    if (p == NULL)
+    {
+        return M_StringDuplicate(".");
+    }
+    else
+    {
+        result = M_StringDuplicate(path);
+        result[p - path] = '\0';
+        return result;
+    }
+}
+
+// Returns the base filename described by the given path (without the
+// directory name). The result points inside path and nothing new is
+// allocated.
+const char *M_BaseName(const char *path)
+{
+    char *p;
+
+    p = strrchr(path, DIR_SEPARATOR);
+    if (p == NULL)
+    {
+        return path;
+    }
+    else
+    {
+        return p + 1;
+    }
+}
+
 void M_ExtractFileBase(const char *path, char *dest)
 {
     const char *src;
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -33,6 +33,8 @@
 char *M_FileCaseExists(const char *file);
 long M_FileLength(FILE *handle);
 boolean M_StrToInt(const char *str, int *result);
+char *M_DirName(const char *path);
+const char *M_BaseName(const char *path);
 void M_ExtractFileBase(const char *path, char *dest);
 void M_ForceUppercase(char *text);
 void M_ForceLowercase(char *text);