1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* 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 "m_option.h"
#include "m_struct.h"
#include "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;
}
if(f->type->parse(f, bstr(field), param, false, M_ST_MB_P(obj,f->p), NULL) < 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;
}
void
m_struct_reset(const m_struct_t* st, void* obj, const char* field) {
const m_option_t* f;
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
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);
}
void*
m_struct_copy(const m_struct_t* st, void* obj) {
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;
}
|