From d8fd06c37de94d78eb37af93177de7c3040cf1f2 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 23 Dec 2012 21:25:24 +0100 Subject: avstring: add av_basename and av_dirname Thread safe version of the common basename and dirname. --- libavutil/avstring.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'libavutil/avstring.c') diff --git a/libavutil/avstring.c b/libavutil/avstring.c index c14832e7b9..2c88bd3d5b 100644 --- a/libavutil/avstring.c +++ b/libavutil/avstring.c @@ -25,6 +25,8 @@ #include #include #include "avstring.h" +#include "config.h" +#include "common.h" #include "mem.h" int av_strstart(const char *str, const char *pfx, const char **ptr) @@ -156,6 +158,45 @@ int av_strncasecmp(const char *a, const char *b, size_t n) return c1 - c2; } +const char *av_basename(const char *path) +{ + char *p = strrchr(path, '/'); + +#if HAVE_DOS_PATHS + char *q = strrchr(path, '\\'); + char *d = strchr(path, ':'); + + p = FFMAX3(p, q, d); +#endif + + if (!p) + return path; + + return p + 1; +} + +const char *av_dirname(char *path) +{ + char *p = strrchr(path, '/'); + +#if HAVE_DOS_PATHS + char *q = strrchr(path, '\\'); + char *d = strchr(path, ':'); + + d = d ? d + 1 : d; + + p = FFMAX3(p, q, d); +#endif + + if (!p) + return "."; + + *p = '\0'; + + return path; +} + + #ifdef TEST #include "common.h" -- cgit v1.2.3