1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-28 18:12:22 +00:00
mpv/core/m_struct.c

92 lines
2.4 KiB
C
Raw Normal View History

/*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/// \file
/// \ingroup OptionsStruct
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "core/m_option.h"
#include "core/m_struct.h"
#include "core/mp_msg.h"
const m_option_t*
m_struct_get_field(const m_struct_t* st,const char* f) {
int i;
for(i = 0 ; st->fields[i].name ; i++) {
if(strcasecmp(st->fields[i].name,f) == 0)
return &st->fields[i];
}
return NULL;
}
void*
m_struct_alloc(const m_struct_t* st) {
int i;
void* r;
if(!st->defaults) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
return NULL;
}
r = calloc(1,st->size);
memcpy(r,st->defaults,st->size);
for(i = 0 ; st->fields[i].name ; i++) {
if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(st->defaults,st->fields[i].p));
}
return r;
}
int m_struct_set(const m_struct_t *st, void *obj, const char *field,
struct bstr param)
{
const m_option_t* f = m_struct_get_field(st,field);
if(!f) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
st->name,field);
return 0;
}
options: get rid of ambiguous option parsing Options parsing used to be ambiguous, as in the splitting into option and values pairs was ambiguous. Example: -option -something It wasn't clear whether -option actually takes an argument or not. The string "-something" could either be a separate option, or an argument to "-option". The code had to call the option specific parser function to resolve this. This made everything complicated and didn't even have a real use. There was only one case where this was actually used: string lists (m_option_type_string_list) and options based on it. That is because this option type actually turns a single option into a proxy for several real arguments, e.g. "vf*" can handle "-vf-add" and "-vf-clr". Options suffixed with "-clr" are the only options of this group which take no arguments. This is ambiguous only with the "old syntax" (as shown above). The "new" option syntax always puts option name and value into same argument. (E.g. "--option=--something" or "--option" "--something".) Simplify the code by making it statically known whether an option takes a parameter or not with the flag M_OPT_TYPE_OLD_SYNTAX_NO_PARAM. If it's set, the option parser assumes the option takes no argument. The only real ambiguity left, string list options that end on "-clr", are special cased in the parser. Remove some duplication of the logic in the command line parser by moving all argument splitting logic into split_opt(). (It's arguable whether that can be considered code duplication, but now the code is a bit simpler anyway. This might be subjective.) Remove the "ambiguous" parameter from all option parsing related code. Make m_config unaware of the pre-parsing concept. Make most CONF_NOCFG options also CONF_GLOBAL (except those explicitly usable as per-file options.)
2012-08-05 21:34:28 +00:00
if(f->type->parse(f, bstr0(field), param, M_ST_MB_P(obj,f->p)) < 0) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %.*s\n",
st->name, field, BSTR_P(param));
return 0;
}
return 1;
}
/// Free an allocated struct
void
m_struct_free(const m_struct_t* st, void* obj) {
int i;
for(i = 0 ; st->fields[i].name ; i++)
m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
free(obj);
}