Skip to content
2 changes: 1 addition & 1 deletion common/av_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void resolve_positional_arg(void *avobj, char **name)

char *end = NULL;
int pos = strtol(*name + 1, &end, 10);
if (!end || *end || pos < 0)
if (*end || pos < 0)
return;

const AVOption *opt = NULL;
Expand Down
2 changes: 1 addition & 1 deletion demux/demux.c
Original file line number Diff line number Diff line change
Expand Up @@ -2869,7 +2869,7 @@ static int decode_float(char *str, float *out)
float dec_val;

dec_val = strtod(str, &rest);
if (!rest || (rest == str) || !isfinite(dec_val))
if (rest == str || !isfinite(dec_val))
return -1;

*out = dec_val;
Expand Down
2 changes: 1 addition & 1 deletion demux/demux_lavf.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static int dict_get_decimal(AVDictionary *dict, const char *entry, int def)
if (e && e->value) {
char *end = NULL;
long int r = strtol(e->value, &end, 10);
if (end && !end[0] && r >= INT_MIN && r <= INT_MAX)
if (!end[0] && r >= INT_MIN && r <= INT_MAX)
return r;
}
return def;
Expand Down
2 changes: 1 addition & 1 deletion demux/demux_mkv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ static void parse_vorbis_chmap(struct mp_chmap *channels, unsigned char *data,
snprintf(smask, sizeof(smask), "%.*s", (int)(len - 34), data + 34);
char *end = NULL;
uint32_t mask = strtol(smask, &end, 0);
if (!end || end[0])
if (end[0])
Comment thread
kasper93 marked this conversation as resolved.
mask = 0;
struct mp_chmap chmask = {0};
mp_chmap_from_waveext(&chmask, mask);
Expand Down
30 changes: 15 additions & 15 deletions demux/demux_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,15 @@ static bool test_path(struct pl_parser *p, char *path, int autocreate)
return true;

bstr ext = bstr_get_ext(bstr0(path));
if (autocreate & AUTO_VIDEO && str_in_list(ext, p->mp_opts->video_exts))
if (autocreate & AUTO_VIDEO && bstr_in_list0(ext, p->mp_opts->video_exts))
return true;
if (autocreate & AUTO_AUDIO && str_in_list(ext, p->mp_opts->audio_exts))
if (autocreate & AUTO_AUDIO && bstr_in_list0(ext, p->mp_opts->audio_exts))
return true;
if (autocreate & AUTO_IMAGE && str_in_list(ext, p->mp_opts->image_exts))
if (autocreate & AUTO_IMAGE && bstr_in_list0(ext, p->mp_opts->image_exts))
return true;
if (autocreate & AUTO_ARCHIVE && str_in_list(ext, p->mp_opts->archive_exts))
if (autocreate & AUTO_ARCHIVE && bstr_in_list0(ext, p->mp_opts->archive_exts))
return true;
if (autocreate & AUTO_PLAYLIST && str_in_list(ext, p->mp_opts->playlist_exts))
if (autocreate & AUTO_PLAYLIST && bstr_in_list0(ext, p->mp_opts->playlist_exts))
return true;

return false;
Expand Down Expand Up @@ -519,15 +519,15 @@ static enum autocreate_mode get_directory_filter(struct pl_parser *p)
enum autocreate_mode autocreate = AUTO_NONE;
if (!p->opts->directory_filter || !p->opts->directory_filter[0])
autocreate = AUTO_ANY;
if (str_in_list(bstr0("video"), p->opts->directory_filter))
if (bstr_in_list0(bstr0("video"), p->opts->directory_filter))
autocreate |= AUTO_VIDEO;
if (str_in_list(bstr0("audio"), p->opts->directory_filter))
if (bstr_in_list0(bstr0("audio"), p->opts->directory_filter))
autocreate |= AUTO_AUDIO;
if (str_in_list(bstr0("image"), p->opts->directory_filter))
if (bstr_in_list0(bstr0("image"), p->opts->directory_filter))
autocreate |= AUTO_IMAGE;
if (str_in_list(bstr0("archive"), p->opts->directory_filter))
if (bstr_in_list0(bstr0("archive"), p->opts->directory_filter))
autocreate |= AUTO_ARCHIVE;
if (str_in_list(bstr0("playlist"), p->opts->directory_filter))
if (bstr_in_list0(bstr0("playlist"), p->opts->directory_filter))
autocreate |= AUTO_PLAYLIST;
return autocreate;
}
Expand All @@ -545,15 +545,15 @@ static int parse_dir(struct pl_parser *p)
autocreate = get_directory_filter(p);
break;
case 2: // same
if (str_in_list(ext, p->mp_opts->video_exts)) {
if (bstr_in_list0(ext, p->mp_opts->video_exts)) {
autocreate = AUTO_VIDEO;
} else if (str_in_list(ext, p->mp_opts->audio_exts)) {
} else if (bstr_in_list0(ext, p->mp_opts->audio_exts)) {
autocreate = AUTO_AUDIO;
} else if (str_in_list(ext, p->mp_opts->image_exts)) {
} else if (bstr_in_list0(ext, p->mp_opts->image_exts)) {
autocreate = AUTO_IMAGE;
} else if (str_in_list(ext, p->mp_opts->archive_exts)) {
} else if (bstr_in_list0(ext, p->mp_opts->archive_exts)) {
autocreate = AUTO_ARCHIVE;
} else if (str_in_list(ext, p->mp_opts->playlist_exts)) {
} else if (bstr_in_list0(ext, p->mp_opts->playlist_exts)) {
autocreate = AUTO_PLAYLIST;
}
break;
Expand Down
14 changes: 14 additions & 0 deletions misc/bstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@ bool bstr_decode_hex(void *talloc_ctx, struct bstr hex, struct bstr *out)
return true;
}

int bstr_find_in_list0(struct bstr str, char **list, bool case_sensitive)
{
for (int n = 0; list && list[n]; n++)
if (case_sensitive ? !bstrcmp0(str, list[n]) : !bstrcasecmp0(str, list[n]))
return n;

return -1;
}

bool bstr_in_list0(struct bstr str, char **list)
{
return bstr_find_in_list0(str, list, false) != -1;
}

#ifdef _WIN32

#include <windows.h>
Expand Down
3 changes: 3 additions & 0 deletions misc/bstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ double bstrtod(struct bstr str, struct bstr *rest);
void bstr_lower(struct bstr str);
int bstr_sscanf(struct bstr str, const char *format, ...) MP_SCANF_ATTRIBUTE(2, 3);

int bstr_find_in_list0(bstr str, char **list, bool case_sensitive);
bool bstr_in_list0(struct bstr str, char **list);

// Decode a string containing hexadecimal data. All whitespace will be silently
// ignored. When successful, this allocates a new array to store the output.
bool bstr_decode_hex(void *talloc_ctx, struct bstr hex, struct bstr *out);
Expand Down
13 changes: 2 additions & 11 deletions options/m_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ static int choice_get(const m_option_t *opt, void *ta_parent,
if (alt) {
char *end = NULL;
ival = strtol(alt->name, &end, 10);
if (end && !end[0])
if (!end[0])
alt = NULL;
}
if (alt) {
Expand Down Expand Up @@ -1400,15 +1400,6 @@ static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify)
return bstr_splice(orig, 0, str.start - orig.start);
}

static int find_list_bstr(char **list, bstr item)
{
for (int n = 0; list && list[n]; n++) {
if (bstr_equals0(item, list[n]))
return n;
}
return -1;
}

static char **separate_input_param(const m_option_t *opt, bstr param,
int *len, int op)
{
Expand Down Expand Up @@ -1455,7 +1446,7 @@ static int str_list_remove(char **remove, int n, void *dst)
for (int i = 0; i < n; i++) {
int index = 0;
do {
index = find_list_bstr(list, bstr0(remove[i]));
index = bstr_find_in_list0(bstr0(remove[i]), list, true);
if (index >= 0) {
found = true;
char *old = list[index];
Expand Down
1 change: 0 additions & 1 deletion player/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ 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);
char *mp_format_track_metadata(void *ctx, struct track *t, bool add_lang);
const char *mp_find_non_filename_media_title(MPContext *mpctx);

Expand Down
81 changes: 38 additions & 43 deletions player/external_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "common/msg.h"
#include "misc/charset_conv.h"
#include "misc/language.h"
#include "misc/natural_sort.h"
#include "options/options.h"
#include "options/path.h"
#include "player/core.h"
Expand All @@ -36,59 +37,54 @@ char **sub_exts;

static int test_ext(MPOpts *opts, bstr ext)
{
if (str_in_list(ext, opts->sub_auto_exts))
if (bstr_in_list0(ext, opts->sub_auto_exts))
return STREAM_SUB;
if (str_in_list(ext, opts->audio_exts))
if (bstr_in_list0(ext, opts->audio_exts))
return STREAM_AUDIO;
if (str_in_list(ext, opts->image_exts))
if (bstr_in_list0(ext, opts->image_exts))
return STREAM_VIDEO;
return -1;
}

static int test_cover_filename(bstr fname, char **cover_files)
{
for (int n = 0; cover_files && cover_files[n]; n++) {
if (bstrcasecmp(bstr0(cover_files[n]), fname) == 0) {
size_t size = n;
while (cover_files[++size]);
return size - n;
}
}
return 0;
int idx = bstr_find_in_list0(fname, cover_files, false);
// This equals to 0 if not in list (idx == -1)
return -idx - 1;
}

bool mp_might_be_subtitle_file(const char *filename)
{
return str_in_list(bstr_get_ext(bstr0(filename)), sub_exts);
return bstr_in_list0(bstr_get_ext(bstr0(filename)), sub_exts);
}

void mp_update_subtitle_exts(struct MPOpts *opts)
{
sub_exts = opts->sub_auto_exts;
}

static int compare_sub_filename(const void *a, const void *b)
static int compare_filename(const void *a, const void *b)
{
const struct subfn *s1 = a;
const struct subfn *s2 = b;
return strcoll(s1->fname, s2->fname);
return mp_natural_sort_cmp(s1->fname, s2->fname);
}

static int compare_sub_priority(const void *a, const void *b)
static int compare_priority(const void *a, const void *b)
{
const struct subfn *s1 = a;
const struct subfn *s2 = b;
if (s1->priority > s2->priority)
return -1;
if (s1->priority < s2->priority)
return 1;
return strcoll(s1->fname, s2->fname);
return mp_natural_sort_cmp(s1->fname, s2->fname);
}

static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
struct subfn **slist, int *nsub,
struct bstr path, const char *fname,
int limit_fuzziness, int limit_type)
static void append_dir_external_files(struct mpv_global *global, struct MPOpts *opts,
struct subfn **slist, int *nsub,
struct bstr path, const char *fname,
int limit_fuzziness, int limit_type)
{
void *tmpmem = talloc_new(NULL);
struct mp_log *log = mp_log_new(tmpmem, global->log, "find_files");
Expand Down Expand Up @@ -144,7 +140,7 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
}

if (fuzz < 0 || (limit_type >= 0 && limit_type != type))
goto next_sub;
goto next_file;

// we have a (likely) subtitle file
// higher prio -> auto-selection may prefer it (0 = not loaded)
Expand Down Expand Up @@ -181,32 +177,31 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,

// doesn't contain the movie name
// don't try in the mplayer subtitle directory
if (!limit_fuzziness && fuzz >= 2)
prio |= 1;

mp_trace(log, "Potential external file: \"%s\" Priority: %d\n",
de->d_name, prio);
if (!limit_fuzziness && fuzz >= 2 && prio == 0)
prio = INT_MIN;

if (prio) {
char *subpath = mp_path_join_bstr(*slist, path, dename);
if (mp_path_exists(subpath)) {
mp_trace(log, "Potential external file: \"%s\" Priority: %d\n",
de->d_name, prio);
char *extpath = mp_path_join_bstr(*slist, path, dename);
if (mp_path_exists(extpath)) {
MP_TARRAY_GROW(NULL, *slist, *nsub);
struct subfn *sub = *slist + (*nsub)++;

// annoying and redundant
if (strncmp(subpath, "./", 2) == 0)
subpath += 2;
if (strncmp(extpath, "./", 2) == 0)
extpath += 2;

sub->type = type;
sub->priority = prio;
sub->fname = subpath;
sub->fname = extpath;
sub->lang = lang.len ? bstrdup0(*slist, lang) : NULL;
sub->flags = flags;
} else
talloc_free(subpath);
talloc_free(extpath);
}

next_sub:
next_file:
talloc_free(tmpmem2);
}
closedir(d);
Expand All @@ -223,7 +218,7 @@ static bool case_endswith(const char *s, const char *end)
}

// Drop .sub file if .idx file exists.
// Assumes slist is sorted by compare_sub_filename.
// Assumes slist is sorted by compare_filename.
static void filter_subidx(struct subfn **slist, int *nsub)
{
const char *prev = NULL;
Expand All @@ -233,11 +228,11 @@ static void filter_subidx(struct subfn **slist, int *nsub)
prev = fname;
} else if (case_endswith(fname, ".sub")) {
if (prev && strncmp(prev, fname, strlen(fname) - 4) == 0)
(*slist)[n].priority = -1;
(*slist)[n].priority = 0;
}
}
for (int n = *nsub - 1; n >= 0; n--) {
if ((*slist)[n].priority < 0)
if ((*slist)[n].priority == 0)
MP_TARRAY_REMOVE_AT(*slist, *nsub, n);
}
}
Expand All @@ -251,16 +246,16 @@ static void load_paths(struct mpv_global *global, struct MPOpts *opts,
char *path = mp_path_join_bstr(
*slist, mp_dirname(fname),
bstr0(expanded_path ? expanded_path : paths[i]));
append_dir_subtitles(global, opts, slist, nsubs, bstr0(path),
fname, 0, type);
append_dir_external_files(global, opts, slist, nsubs, bstr0(path),
fname, 0, type);
talloc_free(expanded_path);
}

// Load subtitles in ~/.mpv/sub (or similar) limiting sub fuzziness
char *mp_subdir = mp_find_config_file(NULL, global, cfg_path);
if (mp_subdir) {
append_dir_subtitles(global, opts, slist, nsubs, bstr0(mp_subdir),
fname, 1, type);
append_dir_external_files(global, opts, slist, nsubs, bstr0(mp_subdir),
fname, 1, type);
}
talloc_free(mp_subdir);
}
Expand All @@ -274,7 +269,7 @@ struct subfn *find_external_files(struct mpv_global *global, const char *fname,
int n = 0;

// Load subtitles from current media directory
append_dir_subtitles(global, opts, &slist, &n, mp_dirname(fname), fname, 0, -1);
append_dir_external_files(global, opts, &slist, &n, mp_dirname(fname), fname, 0, -1);

// Load subtitles in dirs specified by sub-paths option
if (opts->sub_auto >= 0) {
Expand All @@ -288,12 +283,12 @@ struct subfn *find_external_files(struct mpv_global *global, const char *fname,
}

// Sort by name for filter_subidx()
qsort(slist, n, sizeof(*slist), compare_sub_filename);
qsort(slist, n, sizeof(*slist), compare_filename);

filter_subidx(&slist, &n);

// Sort subs by priority and append them
qsort(slist, n, sizeof(*slist), compare_sub_priority);
qsort(slist, n, sizeof(*slist), compare_priority);

struct subfn z = {0};
MP_TARRAY_APPEND(NULL, slist, n, z);
Expand Down
2 changes: 1 addition & 1 deletion player/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ struct MPContext *mp_create(void)

char *verbose_env = getenv("MPV_VERBOSE");
if (verbose_env)
mpctx->opts->verbose = atoi(verbose_env);
mpctx->opts->verbose = strtol(verbose_env, NULL, 10);

mp_cancel_trigger(mpctx->playback_abort);

Expand Down
Loading
Loading