player/misc: add str_in_list() and use it

This commit is contained in:
Kacper Michajłow 2024-07-29 20:54:06 +02:00
parent b73445dcf3
commit 73b58722e7
4 changed files with 27 additions and 36 deletions

View File

@ -28,6 +28,7 @@
#include "misc/charset_conv.h"
#include "misc/thread_tools.h"
#include "options/path.h"
#include "player/core.h"
#include "stream/stream.h"
#include "osdep/io.h"
#include "misc/natural_sort.h"
@ -410,28 +411,17 @@ static int cmp_dir_entry(const void *a, const void *b)
}
}
static bool has_str(bstr ext, char **list)
{
if (!list)
return false;
while (*list) {
if (!bstrcasecmp0(ext, *list++))
return true;
}
return false;
}
static bool test_path(struct pl_parser *p, char *path, int autocreate)
{
if (autocreate & AUTO_ANY)
return true;
bstr ext = bstr_get_ext(bstr0(path));
if (autocreate & AUTO_VIDEO && has_str(ext, p->mp_opts->video_exts))
if (autocreate & AUTO_VIDEO && str_in_list(ext, p->mp_opts->video_exts))
return true;
if (autocreate & AUTO_AUDIO && has_str(ext, p->mp_opts->audio_exts))
if (autocreate & AUTO_AUDIO && str_in_list(ext, p->mp_opts->audio_exts))
return true;
if (autocreate & AUTO_IMAGE && has_str(ext, p->mp_opts->image_exts))
if (autocreate & AUTO_IMAGE && str_in_list(ext, p->mp_opts->image_exts))
return true;
return false;
@ -521,11 +511,11 @@ static int parse_dir(struct pl_parser *p)
autocreate = AUTO_VIDEO | AUTO_AUDIO | AUTO_IMAGE;
break;
case 3: // same
if (has_str(ext, p->mp_opts->video_exts)) {
if (str_in_list(ext, p->mp_opts->video_exts)) {
autocreate = AUTO_VIDEO;
} else if (has_str(ext, p->mp_opts->audio_exts)) {
} else if (str_in_list(ext, p->mp_opts->audio_exts)) {
autocreate = AUTO_AUDIO;
} else if (has_str(ext, p->mp_opts->image_exts)) {
} else if (str_in_list(ext, p->mp_opts->image_exts)) {
autocreate = AUTO_IMAGE;
}
break;
@ -543,11 +533,11 @@ static int parse_dir(struct pl_parser *p)
autocreate = AUTO_NONE;
if (!p->opts->directory_filter || !p->opts->directory_filter[0])
autocreate = AUTO_ANY;
if (has_str(bstr0("video"), p->opts->directory_filter))
if (str_in_list(bstr0("video"), p->opts->directory_filter))
autocreate |= AUTO_VIDEO;
if (has_str(bstr0("audio"), p->opts->directory_filter))
if (str_in_list(bstr0("audio"), p->opts->directory_filter))
autocreate |= AUTO_AUDIO;
if (has_str(bstr0("image"), p->opts->directory_filter))
if (str_in_list(bstr0("image"), p->opts->directory_filter))
autocreate |= AUTO_IMAGE;
}
if (!stream->is_directory)

View File

@ -565,6 +565,7 @@ void update_window_title(struct MPContext *mpctx, bool force);
void error_on_track(struct MPContext *mpctx, struct track *track);
int stream_dump(struct MPContext *mpctx, const char *source_filename);
double get_track_seek_offset(struct MPContext *mpctx, struct track *track);
bool str_in_list(bstr str, char **list);
// osd.c
void set_osd_bar(struct MPContext *mpctx, int type,

View File

@ -28,30 +28,19 @@
#include "misc/language.h"
#include "options/options.h"
#include "options/path.h"
#include "player/core.h"
#include "external_files.h"
// Needed for mp_might_be_subtitle_file
char **sub_exts;
static bool test_ext_list(bstr ext, char **list)
{
if (!list)
goto done;
for (int n = 0; list[n]; n++) {
if (bstrcasecmp(bstr0(list[n]), ext) == 0)
return true;
}
done:
return false;
}
static int test_ext(MPOpts *opts, bstr ext)
{
if (test_ext_list(ext, opts->sub_auto_exts))
if (str_in_list(ext, opts->sub_auto_exts))
return STREAM_SUB;
if (test_ext_list(ext, opts->audio_exts))
if (str_in_list(ext, opts->audio_exts))
return STREAM_AUDIO;
if (test_ext_list(ext, opts->image_exts))
if (str_in_list(ext, opts->image_exts))
return STREAM_VIDEO;
return -1;
}
@ -70,7 +59,7 @@ static int test_cover_filename(bstr fname, char **cover_files)
bool mp_might_be_subtitle_file(const char *filename)
{
return test_ext_list(bstr_get_ext(bstr0(filename)), sub_exts);
return str_in_list(bstr_get_ext(bstr0(filename)), sub_exts);
}
void mp_update_subtitle_exts(struct MPOpts *opts)

View File

@ -343,3 +343,14 @@ const char *mp_status_str(enum playback_status st)
default: return "bug";
}
}
bool str_in_list(bstr str, char **list)
{
if (!list)
return false;
while (*list) {
if (!bstrcasecmp0(str, *list++))
return true;
}
return false;
}