annotate subopt-helper.c @ 30526:68e260df472b

Just use goto instead of reimplementing it badly with a do { } while (0) and break.
author reimar
date Sun, 14 Feb 2010 11:17:12 +0000
parents c1a3f1bbba26
children 32725ca88fed
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: 30123
diff changeset
1 /*
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
diff changeset
2 * This file is part of MPlayer.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
diff changeset
3 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
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: 30123
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: 30123
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: 30123
diff changeset
7 * (at your option) any later version.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
diff changeset
8 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
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: 30123
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: 30123
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: 30123
diff changeset
12 * GNU General Public License for more details.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
diff changeset
13 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
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: 30123
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: 30123
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: 30123
diff changeset
17 */
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 30123
diff changeset
18
29252
3b9429f763e2 Get rid of some more trailing whitespace
reynaldo
parents: 28827
diff changeset
19 /**
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
20 * \file subopt-helper.c
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
21 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
22 * \brief Compensates the suboption parsing code duplication a bit.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
23 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
24 * The routines defined below are there to help you with the
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
25 * suboption parsing. Meaning extracting the options and their
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
26 * values for you and also outputting generic help message if
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
27 * a parse error is encountered.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
28 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
29 * Most stuff happens in the subopt_parse function: if you call it
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
30 * it parses for the passed opts in the passed string. It calls some
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
31 * extra functions for explicit argument parsing ( where the option
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
32 * itself isn't the argument but a value given after the argument
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
33 * delimiter ('='). It also calls your test function if you supplied
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
34 * one.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
35 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
36 */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
37
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
38 #include "subopt-helper.h"
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
39 #include "mp_msg.h"
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
40
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
41 #include <stdlib.h>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
42 #include <string.h>
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
43 #include <limits.h>
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
44 #include <assert.h>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
45
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
46 #ifndef MPDEBUG
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
47 #define NDEBUG
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
48 #endif
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
49
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
50 /* prototypes for argument parsing */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
51 static char const * parse_int( char const * const str, int * const valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
52 static char const * parse_str( char const * const str, strarg_t * const valp );
16720
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
53 static char const * parse_float( char const * const str, float * const valp );
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
54
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
55 /**
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
56 * \brief Try to parse all options in str and fail if it was not possible.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
57 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
58 * \param str Pointer to the zero terminated string to be parsed.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
59 * \param opts Pointer to a options array. The array must be terminated
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
60 * with an element having set name to NULL in its opt_t structure.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
61 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
62 * \return The return value is zero if the string could be parsed
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
63 * else a non-zero value is returned.
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
64 *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
65 */
28827
2b021e3e1000 Get rid of the "set" member of the subopt-parser struct, it made
reimar
parents: 22283
diff changeset
66 int subopt_parse( char const * const str, const opt_t * opts )
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
67 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
68 int parse_err = 0, idx;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
69 unsigned int parse_pos = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
70
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
71 if ( str )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
72 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
73 while ( str[parse_pos] && !parse_err )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
74 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
75 int next = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
76
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
77 idx = 0; // reset index for the below loop
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
78 while ( opts[idx].name )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
79 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
80 int opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
81 int substr_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
82
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
83 // get length of the option we test against */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
84 opt_len = strlen( opts[idx].name );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
85
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
86 // get length of the current substring of str */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
87 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
88 char * delim, * arg_delim;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
89
29252
3b9429f763e2 Get rid of some more trailing whitespace
reynaldo
parents: 28827
diff changeset
90 /* search nearest delimiter ( option or argument delimiter ) */
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
91 delim = strchr( &str[parse_pos], ':' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
92 arg_delim = strchr( &str[parse_pos], '=' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
93
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
94 if ( ( delim && arg_delim && delim > arg_delim ) ||
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
95 delim == NULL )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
96 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
97 delim = strchr( &str[parse_pos], '=' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
98 }
29252
3b9429f763e2 Get rid of some more trailing whitespace
reynaldo
parents: 28827
diff changeset
99
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
100 substr_len = delim ? // is a delim present
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
101 delim - &str[parse_pos] : // yes
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
102 strlen( &str[parse_pos] ); // no, end of string
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
103 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
104
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
105 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
106
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
107 /* Check if the length of the current option matches the *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
108 * length of the option we want to test again. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
109 if ( substr_len == opt_len )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
110 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
111 /* check if option was activated/deactivated */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
112 if( strncmp( &str[parse_pos], opts[idx].name, opt_len ) == 0 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
113 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
114 /* option was found */
28827
2b021e3e1000 Get rid of the "set" member of the subopt-parser struct, it made
reimar
parents: 22283
diff changeset
115 next = 1;
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
116
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
117 assert( opts[idx].valp && "Need a pointer to store the arg!" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
118
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
119 /* type specific code */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
120 if ( opts[idx].type == OPT_ARG_BOOL )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
121 {
22283
bc9e95184521 cosmetics: Fix some common typos, sepErate --> sepArate, deciSSion --> deciSion.
diego
parents: 19104
diff changeset
122 /* Handle OPT_ARG_BOOL separately so *
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
123 * the others can share code. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
124
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
125 /* set option to true */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
126 *((int *)(opts[idx].valp)) = 1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
127
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
128 /* increment position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
129 parse_pos += opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
130 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
131 else
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
132 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
133 /* Type is not OPT_ARG_BOOL, means we have to parse *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
134 * for the arg delimiter character and eventually *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
135 * call a test function. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
136 char const * last;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
137
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
138 /* increment position to check for arg */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
139 parse_pos += opt_len;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
140
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
141 if ( str[parse_pos] != '=' )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
142 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
143 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
144 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
145
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
146 /* '=' char was there, so let's move after it */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
147 ++parse_pos;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
148
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
149 switch ( opts[idx].type )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
150 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
151 case OPT_ARG_INT:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
152 last = parse_int( &str[parse_pos],
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
153 (int *)opts[idx].valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
154
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
155 break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
156 case OPT_ARG_STR:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
157 last = parse_str( &str[parse_pos],
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
158 (strarg_t *)opts[idx].valp );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
159 break;
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
160 case OPT_ARG_MSTRZ:
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
161 {
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
162 char **valp = opts[idx].valp;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
163 strarg_t tmp;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
164 tmp.str = NULL;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
165 tmp.len = 0;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
166 last = parse_str( &str[parse_pos], &tmp );
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
167 if (*valp)
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
168 free(*valp);
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
169 *valp = NULL;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
170 if (tmp.str && tmp.len > 0) {
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
171 *valp = malloc(tmp.len + 1);
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
172 memcpy(*valp, tmp.str, tmp.len);
14572
cfe2bbf96000 100l, missing () around *valp
reimar
parents: 14538
diff changeset
173 (*valp)[tmp.len] = 0;
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
174 }
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
175 break;
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
176 }
16720
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
177 case OPT_ARG_FLOAT:
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
178 last = parse_float( &str[parse_pos],
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
179 (float *)opts[idx].valp );
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
180 break;
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
181 default:
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
182 assert( 0 && "Arg type of suboption doesn't exist!" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
183 last = NULL; // break parsing!
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
184 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
185
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
186 /* was the conversion succesful? */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
187 if ( !last )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
188 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
189 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
190 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
191
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
192 /* make test if supplied */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
193 if ( opts[idx].test && !opts[idx].test( opts[idx].valp ) )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
194 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
195 parse_err = 1; break;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
196 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
197
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
198 /* we succeded, set position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
199 parse_pos = last - str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
200 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
201 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
202 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
203 else if ( substr_len == opt_len+2 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
204 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
205 if ( opts[idx].type == OPT_ARG_BOOL && // check for no<opt>
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
206 strncmp( &str[parse_pos], "no", 2 ) == 0 &&
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
207 strncmp( &str[parse_pos+2], opts[idx].name, opt_len ) == 0 )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
208 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
209 /* option was found but negated */
28827
2b021e3e1000 Get rid of the "set" member of the subopt-parser struct, it made
reimar
parents: 22283
diff changeset
210 next = 1;
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
211
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
212 /* set arg to false */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
213 *((int *)(opts[idx].valp)) = 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
214
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
215 /* increment position */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
216 parse_pos += opt_len+2;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
217 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
218 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
219
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
220 ++idx; // test against next option
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
221
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
222 /* break out of the loop, if this subopt is processed */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
223 if ( next ) { break; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
224 }
29252
3b9429f763e2 Get rid of some more trailing whitespace
reynaldo
parents: 28827
diff changeset
225
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
226 /* if we had a valid suboption the current pos should *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
227 * equal the delimiter char, which should be ':' for *
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
228 * suboptions. */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
229 if ( !parse_err && str[parse_pos] == ':' ) { ++parse_pos; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
230 else if ( str[parse_pos] ) { parse_err = 1; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
231 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
232 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
233
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
234 /* if an error was encountered */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
235 if (parse_err)
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
236 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
237 unsigned int i;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
238 mp_msg( MSGT_VO, MSGL_FATAL, "Could not parse arguments at the position indicated below:\n%s\n", str );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
239 for ( i = 0; i < parse_pos; ++i )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
240 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
241 mp_msg(MSGT_VO, MSGL_FATAL, " ");
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
242 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
243 mp_msg(MSGT_VO, MSGL_FATAL, "^\n");
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
244
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
245 return -1;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
246 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
247
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
248 /* we could parse everything */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
249 return 0;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
250 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
251
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
252 static char const * parse_int( char const * const str, int * const valp )
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
253 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
254 char * endp;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
255
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
256 assert( str && "parse_int(): str == NULL" );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
257
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
258 *valp = (int)strtol( str, &endp, 0 );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
259
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
260 /* nothing was converted */
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
261 if ( str == endp ) { return NULL; }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
262
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
263 return endp;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
264 }
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
265
16720
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
266 static char const * parse_float( char const * const str, float * const valp )
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
267 {
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
268 char * endp;
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
269
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
270 assert( str && "parse_float(): str == NULL" );
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
271
16725
2a7220c457a7 1l, strtof is only C99, strtod is ANSI C, so use that instead.
reimar
parents: 16720
diff changeset
272 *valp = strtod( str, &endp );
16720
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
273
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
274 /* nothing was converted */
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
275 if ( str == endp ) { return NULL; }
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
276
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
277 return endp;
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
278 }
f73adf296f1e support float arguments in subopt helper.
joey
parents: 16609
diff changeset
279
15733
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
280 #define QUOTE_CHAR '%'
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
281 static char const * parse_str( char const * str, strarg_t * const valp )
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
282 {
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
283 char const * match = strchr( str, ':' );
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
284
15733
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
285 if (str[0] == QUOTE_CHAR) {
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
286 int len = 0;
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
287 str = &str[1];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
288 len = (int)strtol(str, (char **)&str, 0);
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
289 if (!str || str[0] != QUOTE_CHAR || (len > strlen(str) - 1))
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
290 return NULL;
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
291 str = &str[1];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
292 match = &str[len];
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
293 }
e678e306068e support lenght-quoting of strings in subopt parser.
reimar
parents: 14736
diff changeset
294 else
16609
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
295 if (str[0] == '"') {
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
296 str = &str[1];
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
297 match = strchr(str, '"');
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
298 if (!match)
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
299 return NULL;
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
300 valp->len = match - str;
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
301 valp->str = str;
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
302 return &match[1];
061d6e09ad62 Allow string escaping via "".
reimar
parents: 15734
diff changeset
303 }
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
304 if ( !match )
14294
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
305 match = &str[strlen(str)];
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
306
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
307 // empty string or too long
14538
00c3c4111017 New suboption type: malloc'ed, zero terminated string
reimar
parents: 14294
diff changeset
308 if ((match == str) || (match - str > INT_MAX))
14294
90bcd37dba7f fix string argument parsing (e.g. one char strings were not accepted)
reimar
parents: 14281
diff changeset
309 return NULL;
14281
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
310
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
311 valp->len = match - str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
312 valp->str = str;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
313
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
314 return match;
577c16f551ad suboption parser for vo and ao modules
al
parents:
diff changeset
315 }
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
316
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
317
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
318 /*** common test functions ***/
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
319
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
320 /** \brief Test if i is not negative */
30122
1772a5171ac7 Fix function declarations to avoid casting function pointers.
reimar
parents: 29252
diff changeset
321 int int_non_neg(void *iptr)
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
322 {
30122
1772a5171ac7 Fix function declarations to avoid casting function pointers.
reimar
parents: 29252
diff changeset
323 int *i = iptr;
30123
0f5f75b4a015 Simplify range-checking functions for subopt parsing.
reimar
parents: 30122
diff changeset
324 return *i >= 0;
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
325 }
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
326 /** \brief Test if i is positive. */
30122
1772a5171ac7 Fix function declarations to avoid casting function pointers.
reimar
parents: 29252
diff changeset
327 int int_pos(void *iptr)
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
328 {
30122
1772a5171ac7 Fix function declarations to avoid casting function pointers.
reimar
parents: 29252
diff changeset
329 int *i = iptr;
30123
0f5f75b4a015 Simplify range-checking functions for subopt parsing.
reimar
parents: 30122
diff changeset
330 return *i > 0;
14736
2ef20aa3623b Move generic tests to a common place.
al
parents: 14572
diff changeset
331 }
15734
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
332
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
333 /*** little helpers */
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
334
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
335 /** \brief compare the stings just as strcmp does */
19104
2ec2301183cd marks several read-only string parameters which aren't modified inside the called function as const. Patch by Stefan Huehner, stefan AT huehner-org
reynaldo
parents: 16725
diff changeset
336 int strargcmp(strarg_t *arg, const char *str) {
15734
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
337 int res = strncmp(arg->str, str, arg->len);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
338 if (!res && arg->len != strlen(str))
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
339 res = arg->len - strlen(str);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
340 return res;
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
341 }
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
342
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
343 /** \brief compare the stings just as strcasecmp does */
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
344 int strargcasecmp(strarg_t *arg, char *str) {
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
345 int res = strncasecmp(arg->str, str, arg->len);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
346 if (!res && arg->len != strlen(str))
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
347 res = arg->len - strlen(str);
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
348 return res;
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
349 }
7e4fa8fc255c helper functions for comparing strarg_t "strings".
reimar
parents: 15733
diff changeset
350