[MPlayer-dev-eng] [PATCH 1/3] Add mp_dir_join function.

Clément Bœsch ubitux at gmail.com
Sun Jan 23 20:46:26 CET 2011


---
 path.c |   31 +++++++++++++++++++++++++++++++
 path.h |    1 +
 2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/path.c b/path.c
index d2be876..f0eaafb 100644
--- a/path.c
+++ b/path.c
@@ -272,3 +272,34 @@ char *mp_path_join(const char *base, const char *path)
     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;
+    int dirlen = strlen(dir);
+    int i      = dirlen - 1;
+
+#if HAVE_DOS_PATHS
+    if ((dirlen == 2 && dir[0] && dir[1] == ':') // "X:" only
+        || i < 0 || dir[i] == '\\' || dir[i] == '/')
+#else
+    if (i < 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;
+}
diff --git a/path.h b/path.h
index 1369836..1389fc4 100644
--- a/path.h
+++ b/path.h
@@ -29,5 +29,6 @@ void set_codec_path(const char *path);
 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 */
-- 
1.7.3.5



More information about the MPlayer-dev-eng mailing list