fftools: return errors from parse_number_or_die() instead of aborting

Rename the function to just parse_number().
This commit is contained in:
Anton Khirnov 2023-07-14 13:47:23 +02:00
parent 4adbe2d219
commit 9cb47c78d6
6 changed files with 83 additions and 27 deletions

View File

@ -104,8 +104,8 @@ void exit_program(int ret)
exit(ret);
}
double parse_number_or_die(const char *context, const char *numstr, int type,
double min, double max)
int parse_number(const char *context, const char *numstr, int type,
double min, double max, double *dst)
{
char *tail;
const char *error;
@ -118,11 +118,13 @@ double parse_number_or_die(const char *context, const char *numstr, int type,
error = "Expected int64 for %s but found %s\n";
else if (type == OPT_INT && (int)d != d)
error = "Expected int for %s but found %s\n";
else
return d;
else {
*dst = d;
return 0;
}
av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);
exit_program(1);
return 0;
return AVERROR(EINVAL);
}
int64_t parse_time_or_die(const char *context, const char *timestr,
@ -262,6 +264,7 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt,
void *dst = po->flags & (OPT_OFFSET | OPT_SPEC) ?
(uint8_t *)optctx + po->u.off : po->u.dst_ptr;
int *dstcount;
double num;
int ret;
if (po->flags & OPT_SPEC) {
@ -289,15 +292,31 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt,
return AVERROR(ENOMEM);
*(char **)dst = str;
} else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
*(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
ret = parse_number(opt, arg, OPT_INT64, INT_MIN, INT_MAX, &num);
if (ret < 0)
return ret;
*(int *)dst = num;
} else if (po->flags & OPT_INT64) {
*(int64_t *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
ret = parse_number(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX, &num);
if (ret < 0)
return ret;
*(int64_t *)dst = num;
} else if (po->flags & OPT_TIME) {
*(int64_t *)dst = parse_time_or_die(opt, arg, 1);
} else if (po->flags & OPT_FLOAT) {
*(float *)dst = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
ret = parse_number(opt, arg, OPT_FLOAT, -INFINITY, INFINITY, &num);
if (ret < 0)
return ret;
*(float *)dst = num;
} else if (po->flags & OPT_DOUBLE) {
*(double *)dst = parse_number_or_die(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY);
ret = parse_number(opt, arg, OPT_DOUBLE, -INFINITY, INFINITY, &num);
if (ret < 0)
return ret;
*(double *)dst = num;
} else if (po->u.func_arg) {
int ret = po->u.func_arg(optctx, opt, arg);
if (ret < 0) {

View File

@ -100,8 +100,6 @@ int opt_timelimit(void *optctx, const char *opt, const char *arg);
/**
* Parse a string and return its corresponding value as a double.
* Exit from the application if the string cannot be correctly
* parsed or the corresponding value is invalid.
*
* @param context the context of the value to be set (e.g. the
* corresponding command line option name)
@ -111,8 +109,8 @@ int opt_timelimit(void *optctx, const char *opt, const char *arg);
* @param min the minimum valid accepted value
* @param max the maximum valid accepted value
*/
double parse_number_or_die(const char *context, const char *numstr, int type,
double min, double max);
int parse_number(const char *context, const char *numstr, int type,
double min, double max, double *dst);
/**
* Parse a string specifying a time and return its corresponding

View File

@ -784,8 +784,11 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o,
ost->vsync_method = video_sync_method;
MATCH_PER_STREAM_OPT(fps_mode, str, fps_mode, oc, st);
if (fps_mode)
parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
if (fps_mode) {
ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file_index, ost->index, 0);
if (ret < 0)
return ret;
}
if ((ost->frame_rate.num || ost->max_frame_rate.num) &&
!(ost->vsync_method == VSYNC_AUTO ||

View File

@ -194,7 +194,14 @@ int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id
}
if (is_global && *vsync_var == VSYNC_AUTO) {
video_sync_method = parse_number_or_die("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR);
int ret;
double num;
ret = parse_number("vsync", arg, OPT_INT, VSYNC_AUTO, VSYNC_VFR, &num);
if (ret < 0)
return ret;
video_sync_method = num;
av_log(NULL, AV_LOG_WARNING, "Passing a number to -vsync is deprecated,"
" use a string argument as described in the manual.\n");
}
@ -1104,8 +1111,7 @@ static int opt_audio_filters(void *optctx, const char *opt, const char *arg)
static int opt_vsync(void *optctx, const char *opt, const char *arg)
{
av_log(NULL, AV_LOG_WARNING, "-vsync is deprecated. Use -fps_mode\n");
parse_and_set_vsync(arg, &video_sync_method, -1, -1, 1);
return 0;
return parse_and_set_vsync(arg, &video_sync_method, -1, -1, 1);
}
static int opt_timecode(void *optctx, const char *opt, const char *arg)
@ -1353,8 +1359,15 @@ static int opt_progress(void *optctx, const char *opt, const char *arg)
int opt_timelimit(void *optctx, const char *opt, const char *arg)
{
#if HAVE_SETRLIMIT
int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
struct rlimit rl = { lim, lim + 1 };
int ret;
double lim;
struct rlimit rl;
ret = parse_number(opt, arg, OPT_INT64, 0, INT_MAX, &lim);
if (ret < 0)
return ret;
rl = (struct rlimit){ lim, lim + 1 };
if (setrlimit(RLIMIT_CPU, &rl))
perror("setrlimit");
#else

View File

@ -3440,13 +3440,23 @@ static void event_loop(VideoState *cur_stream)
static int opt_width(void *optctx, const char *opt, const char *arg)
{
screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
double num;
int ret = parse_number(opt, arg, OPT_INT64, 1, INT_MAX, &num);
if (ret < 0)
return ret;
screen_width = num;
return 0;
}
static int opt_height(void *optctx, const char *opt, const char *arg)
{
screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
double num;
int ret = parse_number(opt, arg, OPT_INT64, 1, INT_MAX, &num);
if (ret < 0)
return ret;
screen_height = num;
return 0;
}
@ -3491,8 +3501,15 @@ static int opt_show_mode(void *optctx, const char *opt, const char *arg)
{
show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
!strcmp(arg, "waves") ? SHOW_MODE_WAVES :
!strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
!strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT : SHOW_MODE_NONE;
if (show_mode == SHOW_MODE_NONE) {
double num;
int ret = parse_number(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1, &num);
if (ret < 0)
return ret;
show_mode = num;
}
return 0;
}

View File

@ -3665,8 +3665,14 @@ static int opt_show_optional_fields(void *optctx, const char *opt, const char *a
else if (!av_strcasecmp(arg, "never")) show_optional_fields = SHOW_OPTIONAL_FIELDS_NEVER;
else if (!av_strcasecmp(arg, "auto")) show_optional_fields = SHOW_OPTIONAL_FIELDS_AUTO;
if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto"))
show_optional_fields = parse_number_or_die("show_optional_fields", arg, OPT_INT, SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS);
if (show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO && av_strcasecmp(arg, "auto")) {
double num;
int ret = parse_number("show_optional_fields", arg, OPT_INT,
SHOW_OPTIONAL_FIELDS_AUTO, SHOW_OPTIONAL_FIELDS_ALWAYS, &num);
if (ret < 0)
return ret;
show_optional_fields = num;
}
return 0;
}