diff options
author | Uoti Urpala <uau@mplayer2.org> | 2011-07-28 11:07:47 +0300 |
---|---|---|
committer | Uoti Urpala <uau@mplayer2.org> | 2011-07-29 05:50:38 +0300 |
commit | e873d703e956d3e2e68b9e18562983b029b5c7a8 (patch) | |
tree | 44a2c48ae4e94bd580ffd5833cd71a037af36043 /m_config.c | |
parent | d8374376c0d46ffab88b96eb32b52621c34f562c (diff) | |
download | mpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.bz2 mpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.xz |
options: change option parsing to use bstr
Using bstr allows simpler parsing code, especially because it avoids
the need to modify or copy strings just to terminate extracted
substrings.
Diffstat (limited to 'm_config.c')
-rw-r--r-- | m_config.c | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/m_config.c b/m_config.c index b278877c3c..3cf8c721ac 100644 --- a/m_config.c +++ b/m_config.c @@ -36,13 +36,13 @@ #define MAX_PROFILE_DEPTH 20 -static int parse_profile(const struct m_option *opt, const char *name, - const char *param, bool ambiguous_param, void *dst) +static int parse_profile(const struct m_option *opt, struct bstr name, + struct bstr param, bool ambiguous_param, void *dst) { struct m_config *config = opt->priv; char **list = NULL; int i, r; - if (param && !strcmp(param, "help")) { + if (!bstrcmp0(param, "help")) { struct m_profile *p; if (!config->profiles) { mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, @@ -387,36 +387,37 @@ int m_config_register_options(struct m_config *config, } static struct m_config_option *m_config_get_co(const struct m_config *config, - char *arg) + struct bstr name) { struct m_config_option *co; for (co = config->opts; co; co = co->next) { - int l = strlen(co->name) - 1; - if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD) && - (co->name[l] == '*')) { - if (strncasecmp(co->name, arg, l) == 0) + struct bstr coname = bstr(co->name); + if ((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD) + && bstr_endswith0(coname, "*")) { + coname.len--; + if (bstrcasecmp(bstr_splice(name, 0, coname.len), coname) == 0) return co; - } else if (strcasecmp(co->name, arg) == 0) + } else if (bstrcasecmp(coname, name) == 0) return co; } return NULL; } -static int m_config_parse_option(const struct m_config *config, char *arg, - char *param, bool ambiguous_param, bool set) +static int m_config_parse_option(const struct m_config *config, + struct bstr name, struct bstr param, + bool ambiguous_param, bool set) { struct m_config_option *co; int r = 0; assert(config != NULL); assert(config->lvl > 0); - assert(arg != NULL); + assert(name.len != 0); - co = m_config_get_co(config, arg); - if (!co) { + co = m_config_get_co(config, name); + if (!co) return M_OPT_UNKNOWN; - } // This is the only mandatory function assert(co->opt->type->parse); @@ -424,12 +425,14 @@ static int m_config_parse_option(const struct m_config *config, char *arg, // Check if this option isn't forbidden in the current mode if ((config->mode == M_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) { mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, - "The %s option can't be used in a config file.\n", arg); + "The %.*s option can't be used in a config file.\n", + BSTR_P(name)); return M_OPT_INVALID; } if ((config->mode == M_COMMAND_LINE) && (co->opt->flags & M_OPT_NOCMD)) { mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, - "The %s option can't be used on the command line.\n", arg); + "The %.*s option can't be used on the command line.\n", + BSTR_P(name)); return M_OPT_INVALID; } // During command line preparse set only pre-parse options @@ -445,7 +448,7 @@ static int m_config_parse_option(const struct m_config *config, char *arg, char **lst = NULL; int i, sr; // Parse the child options - r = m_option_parse(co->opt, arg, param, false, &lst); + r = m_option_parse(co->opt, name, param, false, &lst); // Set them now if (r >= 0) for (i = 0; lst && lst[2 * i]; i++) { @@ -454,8 +457,9 @@ static int m_config_parse_option(const struct m_config *config, char *arg, // Build the full name char n[l]; sprintf(n, "%s:%s", co->name, lst[2 * i]); - sr = m_config_parse_option(config, n, lst[2 * i + 1], - false, set); + sr = m_config_parse_option(config, bstr(n), + bstr(lst[2 * i + 1]), false, + set); if (sr < 0) { if (sr == M_OPT_UNKNOWN) { mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, @@ -476,7 +480,7 @@ static int m_config_parse_option(const struct m_config *config, char *arg, } talloc_free(lst); } else - r = m_option_parse(co->opt, arg, param, ambiguous_param, + r = m_option_parse(co->opt, name, param, ambiguous_param, set ? co->slots->data : NULL); // Parsing failed ? @@ -491,22 +495,24 @@ static int m_config_parse_option(const struct m_config *config, char *arg, return r; } -int m_config_set_option(struct m_config *config, char *arg, - char *param, bool ambiguous_param) +int m_config_set_option(struct m_config *config, struct bstr name, + struct bstr param, bool ambiguous_param) { - mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %s=%s\n", arg, param); - return m_config_parse_option(config, arg, param, ambiguous_param, 1); + mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Setting %.*s=%.*s\n", BSTR_P(name), + BSTR_P(param)); + return m_config_parse_option(config, name, param, ambiguous_param, 1); } -int m_config_check_option(const struct m_config *config, char *arg, - char *param, bool ambiguous_param) +int m_config_check_option(const struct m_config *config, struct bstr name, + struct bstr param, bool ambiguous_param) { int r; - mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %s=%s\n", arg, param); - r = m_config_parse_option(config, arg, param, ambiguous_param, 0); + mp_msg(MSGT_CFGPARSER, MSGL_DBG2, "Checking %.*s=%.*s\n", BSTR_P(name), + BSTR_P(param)); + r = m_config_parse_option(config, name, param, ambiguous_param, 0); if (r == M_OPT_MISSING_PARAM) { mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, - "Error: option '%s' must have a parameter!\n", arg); + "Error: option '%.*s' must have a parameter!\n", BSTR_P(name)); return M_OPT_INVALID; } return r; @@ -514,15 +520,14 @@ int m_config_check_option(const struct m_config *config, char *arg, const struct m_option *m_config_get_option(const struct m_config *config, - char *arg) + struct bstr name) { struct m_config_option *co; assert(config != NULL); assert(config->lvl > 0); - assert(arg != NULL); - co = m_config_get_co(config, arg); + co = m_config_get_co(config, name); if (co) return co->opt; else @@ -597,7 +602,7 @@ void m_profile_set_desc(struct m_profile *p, char *desc) int m_config_set_profile_option(struct m_config *config, struct m_profile *p, char *name, char *val) { - int i = m_config_check_option(config, name, val, false); + int i = m_config_check_option0(config, name, val, false); if (i < 0) return i; p->opts = talloc_realloc(p, p->opts, char *, 2 * (p->num_opts + 2)); @@ -620,7 +625,7 @@ void m_config_set_profile(struct m_config *config, struct m_profile *p) config->mode = M_CONFIG_FILE; config->profile_depth++; for (i = 0; i < p->num_opts; i++) - m_config_set_option(config, p->opts[2 * i], p->opts[2 * i + 1], false); + m_config_set_option0(config, p->opts[2 * i], p->opts[2 * i + 1], false); config->profile_depth--; config->mode = prev_mode; } |