it's synced with r26258
[mplayer.git] / m_struct.c
blob9ffd356d319cc0ae08e225b8702f91c3628c2d30
2 /// \file
3 /// \ingroup OptionsStruct
5 #include "config.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "m_option.h"
11 #include "m_struct.h"
12 #include "mp_msg.h"
14 const m_option_t*
15 m_struct_get_field(const m_struct_t* st,const char* f) {
16 int i;
18 for(i = 0 ; st->fields[i].name ; i++) {
19 if(strcasecmp(st->fields[i].name,f) == 0)
20 return &st->fields[i];
22 return NULL;
25 void*
26 m_struct_alloc(const m_struct_t* st) {
27 int i;
28 void* r;
30 if(!st->defaults) {
31 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
32 return NULL;
34 // Check the struct fields
35 for(i = 0 ; st->fields[i].name ; i++) {
36 if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
37 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: Option types with the indirect flag are forbidden.\n",st->name,st->fields[i].name);
38 return NULL;
42 r = calloc(1,st->size);
43 memcpy(r,st->defaults,st->size);
45 for(i = 0 ; st->fields[i].name ; i++) {
46 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
47 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
48 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));
50 return r;
53 int
54 m_struct_set(const m_struct_t* st, void* obj, char* field, char* param) {
55 const m_option_t* f = m_struct_get_field(st,field);
57 if(!f) {
58 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
59 st->name,field);
60 return 0;
63 if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
64 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %s\n",
65 st->name,field,param);
66 return 0;
69 return 1;
72 void
73 m_struct_reset(const m_struct_t* st, void* obj, const char* field) {
74 const m_option_t* f;
76 if(!field) { // Reset all options
77 int i;
78 for(i = 0 ; st->fields[i].name ; i++)
79 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));
80 return;
83 // Only one
84 f = m_struct_get_field(st,field);
85 if(!f) {
86 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
87 st->name,field);
88 return;
90 m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
93 /// Free an allocated struct
94 void
95 m_struct_free(const m_struct_t* st, void* obj) {
96 int i;
98 for(i = 0 ; st->fields[i].name ; i++)
99 m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
100 free(obj);
103 void*
104 m_struct_copy(const m_struct_t* st, void* obj) {
105 void* r = malloc(st->size);
106 int i;
108 memcpy(r,obj,st->size);
109 for(i = 0 ; st->fields[i].name ; i++) {
110 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
111 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
112 m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p));
115 return r;