1 /*
2 * This file is part of MPlayer.
3 *
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 /// \file
20 /// \ingroup OptionsStruct
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libavutil/avstring.h"
28 #include "m_option.h"
29 #include "m_struct.h"
30 #include "mp_msg.h"
31
32 const m_option_t*
m_struct_get_field(const m_struct_t * st,const char * f)33 m_struct_get_field(const m_struct_t* st,const char* f) {
34 int i;
35
36 for(i = 0 ; st->fields[i].name ; i++) {
37 if(av_strcasecmp(st->fields[i].name,f) == 0)
38 return &st->fields[i];
39 }
40 return NULL;
41 }
42
43 void*
m_struct_alloc(const m_struct_t * st)44 m_struct_alloc(const m_struct_t* st) {
45 int i;
46 void* r;
47
48 if(!st->defaults) {
49 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
50 return NULL;
51 }
52 // Check the struct fields
53 for(i = 0 ; st->fields[i].name ; i++) {
54 if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
55 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: Option types with the indirect flag are forbidden.\n",st->name,st->fields[i].name);
56 return NULL;
57 }
58 }
59
60 r = calloc(1,st->size);
61 memcpy(r,st->defaults,st->size);
62
63 for(i = 0 ; st->fields[i].name ; i++) {
64 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
65 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
66 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));
67 }
68 return r;
69 }
70
71 int
m_struct_set(const m_struct_t * st,void * obj,const char * field,const char * param)72 m_struct_set(const m_struct_t* st, void* obj, const char* field, const char* param) {
73 const m_option_t* f = m_struct_get_field(st,field);
74
75 if(!f) {
76 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
77 st->name,field);
78 return 0;
79 }
80
81 if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
82 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %s\n",
83 st->name,field,param);
84 return 0;
85 }
86
87 return 1;
88 }
89
90 void
m_struct_reset(const m_struct_t * st,void * obj,const char * field)91 m_struct_reset(const m_struct_t* st, void* obj, const char* field) {
92 const m_option_t* f;
93
94 if(!field) { // Reset all options
95 int i;
96 for(i = 0 ; st->fields[i].name ; i++)
97 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));
98 return;
99 }
100
101 // Only one
102 f = m_struct_get_field(st,field);
103 if(!f) {
104 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
105 st->name,field);
106 return;
107 }
108 m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
109 }
110
111 /// Free an allocated struct
112 void
m_struct_free(const m_struct_t * st,void * obj)113 m_struct_free(const m_struct_t* st, void* obj) {
114 int i;
115
116 for(i = 0 ; st->fields[i].name ; i++)
117 m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
118 free(obj);
119 }
120
121 void*
m_struct_copy(const m_struct_t * st,void * obj)122 m_struct_copy(const m_struct_t* st, void* obj) {
123 void* r = malloc(st->size);
124 int i;
125
126 memcpy(r,obj,st->size);
127 for(i = 0 ; st->fields[i].name ; i++) {
128 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
129 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
130 m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p));
131 }
132
133 return r;
134 }
135