switch_ratio may not work with some filter chains
[mplayer/greg.git] / m_struct.c
blobecee1ad3467859e474360fac3a744ed1d867b0da
1 #include "config.h"
3 #include <stdlib.h>
4 #include <string.h>
6 #include "m_option.h"
7 #include "m_struct.h"
8 #include "mp_msg.h"
10 m_option_t*
11 m_struct_get_field(m_struct_t* st,char* f) {
12 int i;
14 for(i = 0 ; st->fields[i].name ; i++) {
15 if(strcasecmp(st->fields[i].name,f) == 0)
16 return &st->fields[i];
18 return NULL;
21 void*
22 m_struct_alloc(m_struct_t* st) {
23 int i;
24 void* r;
26 if(!st->defaults) {
27 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
28 return NULL;
30 // Check the struct fields
31 for(i = 0 ; st->fields[i].name ; i++) {
32 if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
33 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: option type with the indirect flag are forbiden\n",st->name,st->fields[i].name);
34 return NULL;
38 r = calloc(1,st->size);
39 memcpy(r,st->defaults,st->size);
41 for(i = 0 ; st->fields[i].name ; i++) {
42 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
43 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
44 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));
46 return r;
49 int
50 m_struct_set(m_struct_t* st, void* obj, char* field, char* param) {
51 m_option_t* f = m_struct_get_field(st,field);
53 if(!f) {
54 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
55 st->name,field);
56 return 0;
59 if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
60 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %s\n",
61 st->name,field,param);
62 return 0;
65 return 1;
68 void
69 m_struct_reset(m_struct_t* st, void* obj, char* field) {
70 m_option_t* f;
72 if(!field) { // Reset all options
73 int i;
74 for(i = 0 ; st->fields[i].name ; i++)
75 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));
76 return;
79 // Only one
80 f = m_struct_get_field(st,field);
81 if(!f) {
82 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
83 st->name,field);
84 return;
86 m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
89 /// Free an allocated struct
90 void
91 m_struct_free(m_struct_t* st, void* obj) {
92 int i;
94 for(i = 0 ; st->fields[i].name ; i++)
95 m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
96 free(obj);
99 void*
100 m_struct_copy(m_struct_t* st, void* obj) {
101 void* r = malloc(st->size);
102 int i;
104 memcpy(r,obj,st->size);
105 for(i = 0 ; st->fields[i].name ; i++) {
106 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
107 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
108 m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p));
111 return r;