changeset 32663:83c490a2e9f3

Replace mp_path_is_absolute with mp_path_join.
author cboesch
date Mon, 03 Jan 2011 18:37:45 +0000
parents d71575a9c562
children 6d08af681d54
files path.c path.h
diffstat 2 files changed, 29 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/path.c	Sun Jan 02 19:04:35 2011 +0000
+++ b/path.c	Mon Jan 03 18:37:45 2011 +0000
@@ -238,13 +238,37 @@
 }
 
 /**
- * @brief Indicates weither the path is absolute or not.
+ * @brief Join two paths if path is not absolute.
+ * @param base File or directory base path.
+ * @param path Path to concatenate with the base.
+ * @return New allocated string with the path, or NULL in case of error.
+ * @warning Do not forget the trailing path separator at the end of the base
+ *          path if it is a directory: since file paths are also supported,
+ *          this separator will make the distinction.
+ * @note Paths of the form c:foo, /foo or \foo will still depends on the
+ *       current directory on Windows systems, even though they are considered
+ *       as absolute paths in this function.
  */
-int mp_path_is_absolute(const char *path)
+char *mp_path_join(const char *base, const char *path)
 {
+    char *ret, *tmp;
+
 #if HAVE_DOS_PATHS
-    return path[0] && path[1] == ':';
+    if ((path[0] && path[1] == ':') || path[0] == '\\' || path[0] == '/')
 #else
-    return path[0] == '/';
+    if (path[0] == '/')
 #endif
+        return strdup(path);
+
+    ret = mp_dirname(base);
+    if (!ret)
+        return NULL;
+    tmp = realloc(ret, strlen(ret) + strlen(path) + 1);
+    if (!tmp) {
+        free(ret);
+        return NULL;
+    }
+    ret = tmp;
+    strcat(ret, path);
+    return ret;
 }
--- a/path.h	Sun Jan 02 19:04:35 2011 +0000
+++ b/path.h	Mon Jan 03 18:37:45 2011 +0000
@@ -28,6 +28,6 @@
 void set_codec_path(const char *path);
 const char *mp_basename(const char *path);
 char *mp_dirname(const char *path);
-int mp_path_is_absolute(const char *path);
+char *mp_path_join(const char *base, const char *new_path);
 
 #endif /* MPLAYER_PATH_H */