2006-04-24 19:20:04 +00:00
|
|
|
|
|
|
|
/// \file
|
|
|
|
/// \ingroup OptionsStruct
|
|
|
|
|
2002-11-12 14:16:30 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "m_option.h"
|
|
|
|
#include "m_struct.h"
|
|
|
|
#include "mp_msg.h"
|
|
|
|
|
2007-11-09 06:50:53 +00:00
|
|
|
const m_option_t*
|
|
|
|
m_struct_get_field(const m_struct_t* st,const char* f) {
|
2002-11-12 14:16:30 +00:00
|
|
|
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*
|
2007-11-09 06:50:53 +00:00
|
|
|
m_struct_alloc(const m_struct_t* st) {
|
2002-11-12 14:16:30 +00:00
|
|
|
int i;
|
|
|
|
void* r;
|
|
|
|
|
|
|
|
if(!st->defaults) {
|
2003-08-29 21:42:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
|
2002-11-12 14:16:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Check the struct fields
|
|
|
|
for(i = 0 ; st->fields[i].name ; i++) {
|
|
|
|
if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
|
2007-07-28 14:28:38 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: Option types with the indirect flag are forbidden.\n",st->name,st->fields[i].name);
|
2002-11-12 14:16:30 +00:00
|
|
|
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
|
2007-11-09 06:50:53 +00:00
|
|
|
m_struct_set(const m_struct_t* st, void* obj, char* field, char* param) {
|
|
|
|
const m_option_t* f = m_struct_get_field(st,field);
|
2002-11-12 14:16:30 +00:00
|
|
|
|
|
|
|
if(!f) {
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
|
|
|
|
st->name,field);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
|
2003-04-02 16:05:21 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %s\n",
|
2002-11-12 14:16:30 +00:00
|
|
|
st->name,field,param);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-09 06:50:53 +00:00
|
|
|
m_struct_reset(const m_struct_t* st, void* obj, const char* field) {
|
|
|
|
const m_option_t* f;
|
2002-11-12 14:16:30 +00:00
|
|
|
|
|
|
|
if(!field) { // Reset all options
|
|
|
|
int i;
|
|
|
|
for(i = 0 ; st->fields[i].name ; i++)
|
|
|
|
m_option_copy(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p),M_ST_MB_P(st->defaults,st->fields[i].p));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only one
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Free an allocated struct
|
|
|
|
void
|
2007-11-09 06:50:53 +00:00
|
|
|
m_struct_free(const m_struct_t* st, void* obj) {
|
2002-11-12 14:16:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2007-11-09 06:50:53 +00:00
|
|
|
m_struct_copy(const m_struct_t* st, void* obj) {
|
2002-11-12 14:16:30 +00:00
|
|
|
void* r = malloc(st->size);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(r,obj,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(obj,st->fields[i].p));
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|