changeset 32554:8fffd26d06ae

Add a mp_dirname function (unused at the moment)
author cboesch
date Sun, 21 Nov 2010 17:07:34 +0000
parents c44141d4e443
children 8a556b3aff79
files path.c path.h
diffstat 2 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/path.c	Sun Nov 21 16:52:22 2010 +0000
+++ b/path.c	Sun Nov 21 17:07:34 2010 +0000
@@ -206,3 +206,27 @@
     s = strrchr(path, '/');
     return s ? s + 1 : path;
 }
+
+/**
+ * \brief Allocates a new buffer containing the directory name
+ * \param path Original path. Must be a valid string.
+ *
+ * The path returned always contains a trailing slash '/'.
+ * On systems supporting DOS paths, '\' is also considered as a directory
+ * separator in addition to the '/'.
+ */
+char *mp_dirname(const char *path)
+{
+    const char *base = mp_basename(path);
+    size_t len = base - path;
+    char *dirname;
+
+    if (len == 0)
+        return strdup("./");
+    dirname = malloc(len + 1);
+    if (!dirname)
+        return NULL;
+    strncpy(dirname, path, len);
+    dirname[len] = '\0';
+    return dirname;
+}
--- a/path.h	Sun Nov 21 16:52:22 2010 +0000
+++ b/path.h	Sun Nov 21 17:07:34 2010 +0000
@@ -27,5 +27,6 @@
 void set_path_env(void);
 void set_codec_path(const char *path);
 const char *mp_basename(const char *path);
+char *mp_dirname(const char *path);
 
 #endif /* MPLAYER_PATH_H */