changeset 32751:8e7537586f0b

Add mp_dir_join function.
author cboesch
date Tue, 01 Feb 2011 19:17:33 +0000
parents 8d6e84f970be
children 5c39b0060c2c
files path.c path.h
diffstat 2 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/path.c	Mon Jan 31 15:45:46 2011 +0000
+++ b/path.c	Tue Feb 01 19:17:33 2011 +0000
@@ -272,3 +272,34 @@
     strcat(ret, path);
     return ret;
 }
+
+/**
+ * @brief Same as mp_path_join but always treat the first parameter as a
+ *        directory.
+ * @param dir Directory base path.
+ * @param append Right part to append to dir.
+ * @return New allocated string with the path, or NULL in case of error.
+ */
+char *mp_dir_join(const char *dir, const char *append)
+{
+    char *tmp, *ret;
+    size_t dirlen = strlen(dir);
+    size_t i      = dirlen - 1;
+
+#if HAVE_DOS_PATHS
+    if ((dirlen == 2 && dir[0] && dir[1] == ':') // "X:" only
+        || dirlen == 0 || dir[i] == '\\' || dir[i] == '/')
+#else
+    if (dirlen == 0 || dir[i] == '/')
+#endif
+        return mp_path_join(dir, append);
+
+    tmp = malloc(dirlen + 2);
+    if (!tmp)
+        return NULL;
+    strcpy(tmp, dir);
+    strcpy(tmp + dirlen, "/");
+    ret = mp_path_join(tmp, append);
+    free(tmp);
+    return ret;
+}
--- a/path.h	Mon Jan 31 15:45:46 2011 +0000
+++ b/path.h	Tue Feb 01 19:17:33 2011 +0000
@@ -29,5 +29,6 @@
 const char *mp_basename(const char *path);
 char *mp_dirname(const char *path);
 char *mp_path_join(const char *base, const char *new_path);
+char *mp_dir_join(const char *dir, const char *append);
 
 #endif /* MPLAYER_PATH_H */