comparison m_struct.c @ 8169:7c9253521f9c

A struct setter. It allow you to setup struct from some user settings.
author albeu
date Tue, 12 Nov 2002 14:16:30 +0000
parents
children 864cdb2debb0
comparison
equal deleted inserted replaced
8168:ff6a98628e6c 8169:7c9253521f9c
1
2 #include "config.h"
3
4 #ifdef NEW_CONFIG
5
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "m_option.h"
10 #include "m_struct.h"
11 #include "mp_msg.h"
12
13 m_option_t*
14 m_struct_get_field(m_struct_t* st,char* f) {
15 int i;
16
17 for(i = 0 ; st->fields[i].name ; i++) {
18 if(strcasecmp(st->fields[i].name,f) == 0)
19 return &st->fields[i];
20 }
21 return NULL;
22 }
23
24 void*
25 m_struct_alloc(m_struct_t* st) {
26 int i;
27 void* r;
28
29 if(!st->defaults) {
30 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s need defaults\n");
31 return NULL;
32 }
33 // Check the struct fields
34 for(i = 0 ; st->fields[i].name ; i++) {
35 if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
36 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: option type with the indirect flag are forbiden\n",st->name,st->fields[i].name);
37 return NULL;
38 }
39 }
40
41 r = calloc(1,st->size);
42 memcpy(r,st->defaults,st->size);
43
44 for(i = 0 ; st->fields[i].name ; i++) {
45 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
46 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
47 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));
48 }
49 return r;
50 }
51
52 int
53 m_struct_set(m_struct_t* st, void* obj, char* field, char* param) {
54 m_option_t* f = m_struct_get_field(st,field);
55
56 if(!f) {
57 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
58 st->name,field);
59 return 0;
60 }
61
62 if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
63 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Strut %s, field %s parsing error: %s\n",
64 st->name,field,param);
65 return 0;
66 }
67
68 return 1;
69 }
70
71 void
72 m_struct_reset(m_struct_t* st, void* obj, char* field) {
73 m_option_t* f;
74
75 if(!field) { // Reset all options
76 int i;
77 for(i = 0 ; st->fields[i].name ; i++)
78 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));
79 return;
80 }
81
82 // Only one
83 f = m_struct_get_field(st,field);
84 if(!f) {
85 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
86 st->name,field);
87 return;
88 }
89 m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
90 }
91
92 /// Free an allocated struct
93 void
94 m_struct_free(m_struct_t* st, void* obj) {
95 int i;
96
97 for(i = 0 ; st->fields[i].name ; i++)
98 m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
99 free(obj);
100 }
101
102 void*
103 m_struct_copy(m_struct_t* st, void* obj) {
104 void* r = malloc(st->size);
105 int i;
106
107 memcpy(r,obj,st->size);
108 for(i = 0 ; st->fields[i].name ; i++) {
109 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
110 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
111 m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p));
112 }
113
114 return r;
115 }
116
117
118
119 #endif // NEW_CONFIG