From 7e366113f75c696ae2b32f5faa5f80ec3fca96b8 Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Sat, 18 Dec 2010 01:00:28 +0200 Subject: options: add "choice" option type, use for -pts-association-mode Add a "choice" option type. Options of this type take a string as input and set an int option variable to the value corresponding to the string. The string->int mapping is option-specific and is given in the option definition. Strings not found in the mapping are rejected as invalid option values. Change the option -pts-association-mode to use this new option type and accept values "auto, decoder, sort" instead of "0, 1, 2". The change in accepted values shouldn't cause problems as this option is not appropriate to use in normal user config files. --- m_option.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'm_option.c') diff --git a/m_option.c b/m_option.c index daab955614..33c5e95a1b 100644 --- a/m_option.c +++ b/m_option.c @@ -267,6 +267,50 @@ const struct m_option_type m_option_type_intpair = { .set = copy_opt, }; +static int parse_choice(const struct m_option *opt, const char *name, + const char *param, void *dst, int src) +{ + if (param == NULL) + return M_OPT_MISSING_PARAM; + + struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) + if (!strcmp(param, alt->name)) + break; + if (!alt->name) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid value for option %s: %s\n", + name, param); + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Valid values are:"); + for (alt = opt->priv; alt->name; alt++) + mp_msg(MSGT_CFGPARSER, MSGL_ERR, " %s", alt->name); + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "\n"); + return M_OPT_INVALID; + } + if (dst) + *(int *)dst = alt->value; + + return 1; +} + +static char *print_choice(const m_option_t *opt, const void *val) +{ + int v = *(int *)val; + struct m_opt_choice_alternatives *alt; + for (alt = opt->priv; alt->name; alt++) + if (alt->value == v) + return strdup(alt->name); + abort(); +} + +const struct m_option_type m_option_type_choice = { + .name = "String", // same as arbitrary strings in option list for now + .size = sizeof(int), + .parse = parse_choice, + .print = print_choice, + .save = copy_opt, + .set = copy_opt, +}; + // Float #undef VAL -- cgit v1.2.3