vo_vdpau: remove wrong mp_image usage_count change (no visible effect)
[mplayer/greg.git] / subopt-helper.c
blob5a2f19ea40e2c8db97c0853282dcd7dbf19531c5
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 /**
20 * \file subopt-helper.c
22 * \brief Compensates the suboption parsing code duplication a bit.
24 * The routines defined below are there to help you with the
25 * suboption parsing. Meaning extracting the options and their
26 * values for you and also outputting generic help message if
27 * a parse error is encountered.
29 * Most stuff happens in the subopt_parse function: if you call it
30 * it parses for the passed opts in the passed string. It calls some
31 * extra functions for explicit argument parsing ( where the option
32 * itself isn't the argument but a value given after the argument
33 * delimiter ('='). It also calls your test function if you supplied
34 * one.
38 #include "subopt-helper.h"
39 #include "mp_msg.h"
41 #include <stdlib.h>
42 #include <string.h>
43 #include <limits.h>
44 #include <assert.h>
46 #ifndef MPDEBUG
47 #define NDEBUG
48 #endif
51 static char const * parse_int( char const * const str, int * const valp )
53 char * endp;
55 assert( str && "parse_int(): str == NULL" );
57 *valp = (int)strtol( str, &endp, 0 );
59 /* nothing was converted */
60 if ( str == endp ) { return NULL; }
62 return endp;
65 static char const * parse_float( char const * const str, float * const valp )
67 char * endp;
69 assert( str && "parse_float(): str == NULL" );
71 *valp = strtod( str, &endp );
73 /* nothing was converted */
74 if ( str == endp ) { return NULL; }
76 return endp;
79 #define QUOTE_CHAR '%'
80 static char const * parse_str( char const * str, strarg_t * const valp )
82 char const * match = strchr( str, ':' );
84 if (str[0] == QUOTE_CHAR) {
85 int len = 0;
86 str = &str[1];
87 len = (int)strtol(str, (char **)&str, 0);
88 if (!str || str[0] != QUOTE_CHAR || (len > strlen(str) - 1))
89 return NULL;
90 str = &str[1];
91 match = &str[len];
93 else
94 if (str[0] == '"') {
95 str = &str[1];
96 match = strchr(str, '"');
97 if (!match)
98 return NULL;
99 valp->len = match - str;
100 valp->str = str;
101 return &match[1];
103 if ( !match )
104 match = &str[strlen(str)];
106 // empty string or too long
107 if ((match == str) || (match - str > INT_MAX))
108 return NULL;
110 valp->len = match - str;
111 valp->str = str;
113 return match;
118 * \brief Try to parse all options in str and fail if it was not possible.
120 * \param str Pointer to the zero terminated string to be parsed.
121 * \param opts Pointer to a options array. The array must be terminated
122 * with an element having set name to NULL in its opt_t structure.
124 * \return The return value is zero if the string could be parsed
125 * else a non-zero value is returned.
128 int subopt_parse( char const * const str, const opt_t * opts )
130 int parse_err = 0, idx;
131 unsigned int parse_pos = 0;
133 if ( str )
135 while ( str[parse_pos] && !parse_err )
137 int next = 0;
139 idx = 0; // reset index for the below loop
140 while ( opts[idx].name )
142 int opt_len;
143 int substr_len;
145 // get length of the option we test against */
146 opt_len = strlen( opts[idx].name );
148 // get length of the current substring of str */
150 char * delim, * arg_delim;
152 /* search nearest delimiter ( option or argument delimiter ) */
153 delim = strchr( &str[parse_pos], ':' );
154 arg_delim = strchr( &str[parse_pos], '=' );
156 if ( ( delim && arg_delim && delim > arg_delim ) ||
157 delim == NULL )
159 delim = strchr( &str[parse_pos], '=' );
162 substr_len = delim ? // is a delim present
163 delim - &str[parse_pos] : // yes
164 strlen( &str[parse_pos] ); // no, end of string
167 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
169 /* Check if the length of the current option matches the *
170 * length of the option we want to test again. */
171 if ( substr_len == opt_len )
173 /* check if option was activated/deactivated */
174 if( strncmp( &str[parse_pos], opts[idx].name, opt_len ) == 0 )
176 /* option was found */
177 next = 1;
179 assert( opts[idx].valp && "Need a pointer to store the arg!" );
181 /* type specific code */
182 if ( opts[idx].type == OPT_ARG_BOOL )
184 /* Handle OPT_ARG_BOOL separately so *
185 * the others can share code. */
187 /* set option to true */
188 *((int *)(opts[idx].valp)) = 1;
190 /* increment position */
191 parse_pos += opt_len;
193 else
195 /* Type is not OPT_ARG_BOOL, means we have to parse *
196 * for the arg delimiter character and eventually *
197 * call a test function. */
198 char const * last;
200 /* increment position to check for arg */
201 parse_pos += opt_len;
203 if ( str[parse_pos] != '=' )
205 parse_err = 1; break;
208 /* '=' char was there, so let's move after it */
209 ++parse_pos;
211 switch ( opts[idx].type )
213 case OPT_ARG_INT:
214 last = parse_int( &str[parse_pos],
215 (int *)opts[idx].valp );
217 break;
218 case OPT_ARG_STR:
219 last = parse_str( &str[parse_pos],
220 (strarg_t *)opts[idx].valp );
221 break;
222 case OPT_ARG_MSTRZ:
224 char **valp = opts[idx].valp;
225 strarg_t tmp;
226 tmp.str = NULL;
227 tmp.len = 0;
228 last = parse_str( &str[parse_pos], &tmp );
229 free(*valp);
230 *valp = NULL;
231 if (tmp.str && tmp.len > 0) {
232 *valp = malloc(tmp.len + 1);
233 memcpy(*valp, tmp.str, tmp.len);
234 (*valp)[tmp.len] = 0;
236 break;
238 case OPT_ARG_FLOAT:
239 last = parse_float( &str[parse_pos],
240 (float *)opts[idx].valp );
241 break;
242 default:
243 assert( 0 && "Arg type of suboption doesn't exist!" );
244 last = NULL; // break parsing!
247 /* was the conversion succesful? */
248 if ( !last )
250 parse_err = 1; break;
253 /* make test if supplied */
254 if ( opts[idx].test && !opts[idx].test( opts[idx].valp ) )
256 parse_err = 1; break;
259 /* we succeded, set position */
260 parse_pos = last - str;
264 else if ( substr_len == opt_len+2 )
266 if ( opts[idx].type == OPT_ARG_BOOL && // check for no<opt>
267 strncmp( &str[parse_pos], "no", 2 ) == 0 &&
268 strncmp( &str[parse_pos+2], opts[idx].name, opt_len ) == 0 )
270 /* option was found but negated */
271 next = 1;
273 /* set arg to false */
274 *((int *)(opts[idx].valp)) = 0;
276 /* increment position */
277 parse_pos += opt_len+2;
281 ++idx; // test against next option
283 /* break out of the loop, if this subopt is processed */
284 if ( next ) { break; }
287 /* if we had a valid suboption the current pos should *
288 * equal the delimiter char, which should be ':' for *
289 * suboptions. */
290 if ( !parse_err && str[parse_pos] == ':' ) { ++parse_pos; }
291 else if ( str[parse_pos] ) { parse_err = 1; }
295 /* if an error was encountered */
296 if (parse_err)
298 unsigned int i;
299 mp_msg( MSGT_VO, MSGL_FATAL, "Could not parse arguments at the position indicated below:\n%s\n", str );
300 for ( i = 0; i < parse_pos; ++i )
302 mp_msg(MSGT_VO, MSGL_FATAL, " ");
304 mp_msg(MSGT_VO, MSGL_FATAL, "^\n");
306 return -1;
309 /* we could parse everything */
310 return 0;
314 /*** common test functions ***/
316 /** \brief Test if i is not negative */
317 int int_non_neg(void *iptr)
319 int *i = iptr;
320 return *i >= 0;
322 /** \brief Test if i is positive. */
323 int int_pos(void *iptr)
325 int *i = iptr;
326 return *i > 0;
329 /*** little helpers */
331 /** \brief compare the stings just as strcmp does */
332 int strargcmp(strarg_t *arg, const char *str) {
333 int res = strncmp(arg->str, str, arg->len);
334 if (!res && arg->len != strlen(str))
335 res = arg->len - strlen(str);
336 return res;
339 /** \brief compare the stings just as strcasecmp does */
340 int strargcasecmp(strarg_t *arg, char *str) {
341 int res = strncasecmp(arg->str, str, arg->len);
342 if (!res && arg->len != strlen(str))
343 res = arg->len - strlen(str);
344 return res;