diff options
author | Uoti Urpala <uau@glyph.nonexistent.invalid> | 2011-02-23 16:18:09 +0200 |
---|---|---|
committer | Uoti Urpala <uau@glyph.nonexistent.invalid> | 2011-02-26 16:40:31 +0200 |
commit | 962eec0440ad65a71c09a9e206e110c62d9de3c8 (patch) | |
tree | 7e1f2b9406fa34859dfe67de8d4ea6b80bc09408 /path.c | |
parent | e8af22db818f839f5ac25a309e8838ea53e831b2 (diff) | |
download | mpv-962eec0440ad65a71c09a9e206e110c62d9de3c8.tar.bz2 mpv-962eec0440ad65a71c09a9e206e110c62d9de3c8.tar.xz |
bstr.[ch], path.[ch]: add string and path handling functions
Add some new string and path handling functions to be used in
following commits.
Use new path handling functions to simplify find_files().
Diffstat (limited to 'path.c')
-rw-r--r-- | path.c | 43 |
1 files changed, 41 insertions, 2 deletions
@@ -26,6 +26,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <stdbool.h> #include "config.h" #include "mp_msg.h" #include "path.h" @@ -42,6 +43,8 @@ #include <sys/cygwin.h> #endif +#include "talloc.h" + #include "osdep/osdep.h" char *get_path(const char *filename){ @@ -194,7 +197,7 @@ void set_codec_path(const char *path) needs_free = 1; } -const char *mp_basename(const char *path) +char *mp_basename(const char *path) { char *s; @@ -207,5 +210,41 @@ const char *mp_basename(const char *path) path = s + 1; #endif s = strrchr(path, '/'); - return s ? s + 1 : path; + return s ? s + 1 : (char *)path; +} + +struct bstr mp_dirname(const char *path) +{ + struct bstr ret = {(uint8_t *)path, mp_basename(path) - path}; + if (ret.len == 0) + return BSTR("."); + return ret; +} + +char *mp_path_join(void *talloc_ctx, struct bstr p1, struct bstr p2) +{ + if (p1.len == 0) + return bstrdup0(talloc_ctx, p2); + if (p2.len == 0) + return bstrdup0(talloc_ctx, p1); + +#if HAVE_DOS_PATHS + if (p2.len >= 2 && p2.start[1] == ':' + || p2.start[0] == '\\' || p2.start[0] == '/') +#else + if (p2.start[0] == '/') +#endif + return bstrdup0(talloc_ctx, p2); // absolute path + + bool have_separator; + int endchar1 = p1.start[p1.len - 1]; +#if HAVE_DOS_PATHS + have_separator = endchar1 == '/' || endchar1 == '\\' + || p1.len == 2 && endchar1 == ':'; // "X:" only +#else + have_separator = endchar1 == '/'; +#endif + + return talloc_asprintf(talloc_ctx, "%.*s%s%.*s", BSTR_P(p1), + have_separator ? "" : "/", BSTR_P(p2)); } |