path: add a common mp_is_url() function

Remove the duplicated code.
This commit is contained in:
wm4 2013-09-04 14:04:35 +02:00
parent dbff29c81d
commit efc5ac17bf
5 changed files with 17 additions and 16 deletions

View File

@ -682,6 +682,8 @@ static void load_per_protocol_config(m_config_t *conf, const char * const file)
m_profile_t *p;
/* does filename actually uses a protocol ? */
if (!mp_is_url(bstr0(file)))
return;
str = strstr(file, "://");
if (!str)
return;
@ -784,11 +786,6 @@ static void load_per_file_config(m_config_t *conf, const char * const file,
}
}
static bool might_be_an_url(bstr f)
{
return bstr_find0(f, "://") >= 0;
}
#define MP_WATCH_LATER_CONF "watch_later"
static char *get_playback_resume_config_filename(const char *fname)
@ -796,7 +793,7 @@ static char *get_playback_resume_config_filename(const char *fname)
char *res = NULL;
void *tmp = talloc_new(NULL);
const char *realpath = fname;
if (!might_be_an_url(bstr0(fname))) {
if (!mp_is_url(bstr0(fname))) {
char *cwd = mp_getcwd(tmp);
if (!cwd)
goto exit;

View File

@ -227,3 +227,11 @@ bool mp_path_isdir(const char *path)
struct stat st;
return mp_stat(path, &st) == 0 && S_ISDIR(st.st_mode);
}
// Return false if it's considered a normal local filesystem path.
bool mp_is_url(bstr path)
{
// The URL check is a bit murky, but "/path" and "./path" are never URLs.
return path.len && path.start[0] != '/' && path.start[0] != '.' &&
bstr_find0(path, "://") >= 0;
}

View File

@ -63,4 +63,6 @@ char *mp_getcwd(void *talloc_ctx);
bool mp_path_exists(const char *path);
bool mp_path_isdir(const char *path);
bool mp_is_url(bstr path);
#endif /* MPLAYER_PATH_H */

View File

@ -176,17 +176,12 @@ struct playlist_entry *playlist_get_next(struct playlist *pl, int direction)
return pl->current_was_replaced ? pl->current : pl->current->next;
}
static bool might_be_an_url(bstr f)
{
return bstr_find0(f, "://") >= 0;
}
void playlist_add_base_path(struct playlist *pl, bstr base_path)
{
if (base_path.len == 0 || bstrcmp0(base_path, ".") == 0)
return;
for (struct playlist_entry *e = pl->first; e; e = e->next) {
if (!might_be_an_url(bstr0(e->filename))) {
if (!mp_is_url(bstr0(e->filename))) {
char *new_file = mp_path_join(e, base_path, bstr0(e->filename));
talloc_free(e->filename);
e->filename = new_file;

View File

@ -40,6 +40,7 @@
#include "mpvcore/mp_common.h"
#include "mpvcore/bstr.h"
#include "mpvcore/mp_msg.h"
#include "mpvcore/path.h"
#include "osdep/timer.h"
#include "stream.h"
#include "demux/demux.h"
@ -250,10 +251,8 @@ static const char *match_proto(const char *url, const char *proto)
if (l > 0) {
if (strncasecmp(url, proto, l) == 0 && strncmp("://", url + l, 3) == 0)
return url + l + 3;
} else {
// pure filenames (including "/path" and "./path")
if (url[0] == '/' || url[0] == '.' || !strstr(url, "://"))
return url;
} else if (!mp_is_url(bstr0(url))) {
return url; // pure filenames
}
return NULL;
}