annotate m_struct.c @ 32972:fbaae7fe1a13

Fix several issues with Translate(). 1. The "Unsafe!" comment has been removed, because the strings passed to the function are strcpy'd. 2. The needless memsets (one of which with wrong size) have been removed in favor of a sufficiently simple initialization of trbuf. 3. The array indices are unsigned now, and the manual optimization of having strlen() outside the for loop has been removed in favor of optimization performed by the compiler. 4. There is a check now to prevent an out-of-bounds array access.
author ib
date Tue, 08 Mar 2011 20:56:51 +0000
parents a26f6577d338
children 389d43c448b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30429
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
1 /*
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
2 * This file is part of MPlayer.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
3 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
5 * it under the terms of the GNU General Public License as published by
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
7 * (at your option) any later version.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
8 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
12 * GNU General Public License for more details.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
13 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
14 * You should have received a copy of the GNU General Public License along
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 29263
diff changeset
17 */
18258
96568be4bfdc Doxygen attack!
albeu
parents: 10718
diff changeset
18
96568be4bfdc Doxygen attack!
albeu
parents: 10718
diff changeset
19 /// \file
96568be4bfdc Doxygen attack!
albeu
parents: 10718
diff changeset
20 /// \ingroup OptionsStruct
96568be4bfdc Doxygen attack!
albeu
parents: 10718
diff changeset
21
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
22 #include "config.h"
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
23
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
24 #include <stdlib.h>
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
25 #include <string.h>
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
26
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
27 #include "m_option.h"
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
28 #include "m_struct.h"
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
29 #include "mp_msg.h"
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
30
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
31 const m_option_t*
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
32 m_struct_get_field(const m_struct_t* st,const char* f) {
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
33 int i;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
34
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
35 for(i = 0 ; st->fields[i].name ; i++) {
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
36 if(strcasecmp(st->fields[i].name,f) == 0)
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
37 return &st->fields[i];
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
38 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
39 return NULL;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
40 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
41
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
42 void*
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
43 m_struct_alloc(const m_struct_t* st) {
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
44 int i;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
45 void* r;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
46
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
47 if(!st->defaults) {
10718
alex
parents: 10594
diff changeset
48 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s needs defaults\n",st->name);
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
49 return NULL;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
50 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
51 // Check the struct fields
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
52 for(i = 0 ; st->fields[i].name ; i++) {
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
53 if(st->fields[i].type->flags & M_OPT_TYPE_INDIRECT) {
23873
49a433e2e78f cosmetics: misc typo fixes
diego
parents: 19104
diff changeset
54 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s->%s: Option types with the indirect flag are forbidden.\n",st->name,st->fields[i].name);
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
55 return NULL;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
56 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
57 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
58
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
59 r = calloc(1,st->size);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
60 memcpy(r,st->defaults,st->size);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 24966
diff changeset
61
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
62 for(i = 0 ; st->fields[i].name ; i++) {
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
63 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
64 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
65 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));
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
66 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
67 return r;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
68 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
69
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
70 int
30695
a26f6577d338 Make more option-parsing related function arguments const.
reimar
parents: 30429
diff changeset
71 m_struct_set(const m_struct_t* st, void* obj, const char* field, const char* param) {
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
72 const m_option_t* f = m_struct_get_field(st,field);
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
73
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
74 if(!f) {
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
75 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
76 st->name,field);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
77 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 24966
diff changeset
78 }
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
79
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
80 if(f->type->parse(f,field,param,M_ST_MB_P(obj,f->p),M_CONFIG_FILE) < 0) {
9790
864cdb2debb0 Typo fix
albeu
parents: 8169
diff changeset
81 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s, field %s parsing error: %s\n",
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
82 st->name,field,param);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
83 return 0;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
84 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 24966
diff changeset
85
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
86 return 1;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
87 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
88
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
89 void
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
90 m_struct_reset(const m_struct_t* st, void* obj, const char* field) {
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
91 const m_option_t* f;
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
92
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
93 if(!field) { // Reset all options
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
94 int i;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
95 for(i = 0 ; st->fields[i].name ; i++)
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
96 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));
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
97 return;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
98 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
99
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
100 // Only one
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
101 f = m_struct_get_field(st,field);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 24966
diff changeset
102 if(!f) {
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
103 mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Struct %s doesn't have any %s field\n",
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
104 st->name,field);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
105 return;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
106 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
107 m_option_copy(f,M_ST_MB_P(obj,f->p),M_ST_MB_P(st->defaults,f->p));
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
108 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
109
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
110 /// Free an allocated struct
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
111 void
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
112 m_struct_free(const m_struct_t* st, void* obj) {
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
113 int i;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
114
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
115 for(i = 0 ; st->fields[i].name ; i++)
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
116 m_option_free(&st->fields[i],M_ST_MB_P(obj,st->fields[i].p));
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
117 free(obj);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
118 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
119
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
120 void*
24966
cc170348a763 correct const usage in the option handling code so that tables can be
rfelker
parents: 23873
diff changeset
121 m_struct_copy(const m_struct_t* st, void* obj) {
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
122 void* r = malloc(st->size);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
123 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 24966
diff changeset
124
8169
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
125 memcpy(r,obj,st->size);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
126 for(i = 0 ; st->fields[i].name ; i++) {
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
127 if(st->fields[i].type->flags & M_OPT_TYPE_DYNAMIC)
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
128 memset(M_ST_MB_P(r,st->fields[i].p),0,st->fields[i].type->size);
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
129 m_option_copy(&st->fields[i],M_ST_MB_P(r,st->fields[i].p),M_ST_MB_P(obj,st->fields[i].p));
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
130 }
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
131
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
132 return r;
7c9253521f9c A struct setter. It allow you to setup struct from some user
albeu
parents:
diff changeset
133 }