]> git.sesse.net Git - ffmpeg/commitdiff
avutil/avstring: support input path as a null pointer or empty string
authorLimin Wang <lance.lmwang@gmail.com>
Tue, 24 Sep 2019 11:23:58 +0000 (19:23 +0800)
committerSteven Liu <lq@chinaffmpeg.org>
Tue, 8 Oct 2019 06:12:43 +0000 (14:12 +0800)
Linux and OSX systems support basename and dirname via <libgen.h>, I plan to
make the wrapper interface conform to the standard interface first.
If it is feasible, I will continue to modify it to call the system interface
if there is already a system call interface.

You can get more description about the system interface by below command:
 "man 3 basename"

Reviewed-by: Marton Balint <cus@passwd.hu>
Reviewed-by: Tomas Härdin <tjoppen@acc.umu.se>
Reviewed-by: Steven Liu <lq@chinaffmpeg.org>
Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
libavutil/avstring.c
libavutil/avstring.h

index 4c068f5bc57d30a0e44da0f46ad1e62c052faca9..76a13ba3b51d67f2c39b814631b88c0965e9c428 100644 (file)
@@ -257,8 +257,12 @@ char *av_strireplace(const char *str, const char *from, const char *to)
 
 const char *av_basename(const char *path)
 {
-    char *p = strrchr(path, '/');
+    char *p;
 
+    if (!path || *path == '\0')
+        return ".";
+
+    p = strrchr(path, '/');
 #if HAVE_DOS_PATHS
     char *q = strrchr(path, '\\');
     char *d = strchr(path, ':');
@@ -274,11 +278,11 @@ const char *av_basename(const char *path)
 
 const char *av_dirname(char *path)
 {
-    char *p = strrchr(path, '/');
+    char *p = path ? strrchr(path, '/') : NULL;
 
 #if HAVE_DOS_PATHS
-    char *q = strrchr(path, '\\');
-    char *d = strchr(path, ':');
+    char *q = path ? strrchr(path, '\\') : NULL;
+    char *d = path ? strchr(path, ':')  : NULL;
 
     d = d ? d + 1 : d;
 
index 37dd4e2da0fc8b63ccffd4731ca486e7ca1ccda6..274335cfb9c5a4edf5ccab031081d98b6238a618 100644 (file)
@@ -274,16 +274,21 @@ char *av_strireplace(const char *str, const char *from, const char *to);
 
 /**
  * Thread safe basename.
- * @param path the path, on DOS both \ and / are considered separators.
+ * @param path the string to parse, on DOS both \ and / are considered separators.
  * @return pointer to the basename substring.
+ * If path does not contain a slash, the function returns a copy of path.
+ * If path is a NULL pointer or points to an empty string, a pointer
+ * to a string "." is returned.
  */
 const char *av_basename(const char *path);
 
 /**
  * Thread safe dirname.
- * @param path the path, on DOS both \ and / are considered separators.
- * @return the path with the separator replaced by the string terminator or ".".
- * @note the function may change the input string.
+ * @param path the string to parse, on DOS both \ and / are considered separators.
+ * @return A pointer to a string that's the parent directory of path.
+ * If path is a NULL pointer or points to an empty string, a pointer
+ * to a string "." is returned.
+ * @note the function may modify the contents of the path, so copies should be passed.
  */
 const char *av_dirname(char *path);