Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / subopt-helper.c
blob50283cfbf3a9b0afbaa7565300d01b555daf061c
1 /**
2 * \file subopt-helper.c
4 * \brief Compensates the suboption parsing code duplication a bit.
6 * The routines defined below are there to help you with the
7 * suboption parsing. Meaning extracting the options and their
8 * values for you and also outputting generic help message if
9 * a parse error is encountered.
11 * Most stuff happens in the subopt_parse function: if you call it
12 * it parses for the passed opts in the passed string. It calls some
13 * extra functions for explicit argument parsing ( where the option
14 * itself isn't the argument but a value given after the argument
15 * delimiter ('='). It also calls your test function if you supplied
16 * one.
20 #include "subopt-helper.h"
21 #include "mp_msg.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <assert.h>
28 #ifndef MPDEBUG
29 #define NDEBUG
30 #endif
32 /* prototypes for argument parsing */
33 static char const * parse_int( char const * const str, int * const valp );
34 static char const * parse_str( char const * const str, strarg_t * const valp );
35 static char const * parse_float( char const * const str, float * const valp );
37 /**
38 * \brief Try to parse all options in str and fail if it was not possible.
40 * \param str Pointer to the zero terminated string to be parsed.
41 * \param opts Pointer to a options array. The array must be terminated
42 * with an element having set name to NULL in its opt_t structure.
44 * \return The return value is zero if the string could be parsed
45 * else a non-zero value is returned.
48 int subopt_parse( char const * const str, const opt_t * opts )
50 int parse_err = 0, idx;
51 unsigned int parse_pos = 0;
53 if ( str )
55 while ( str[parse_pos] && !parse_err )
57 int next = 0;
59 idx = 0; // reset index for the below loop
60 while ( opts[idx].name )
62 int opt_len;
63 int substr_len;
65 // get length of the option we test against */
66 opt_len = strlen( opts[idx].name );
68 // get length of the current substring of str */
70 char * delim, * arg_delim;
72 /* search nearest delimiter ( option or argument delimiter ) */
73 delim = strchr( &str[parse_pos], ':' );
74 arg_delim = strchr( &str[parse_pos], '=' );
76 if ( ( delim && arg_delim && delim > arg_delim ) ||
77 delim == NULL )
79 delim = strchr( &str[parse_pos], '=' );
82 substr_len = delim ? // is a delim present
83 delim - &str[parse_pos] : // yes
84 strlen( &str[parse_pos] ); // no, end of string
87 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
89 /* Check if the length of the current option matches the *
90 * length of the option we want to test again. */
91 if ( substr_len == opt_len )
93 /* check if option was activated/deactivated */
94 if( strncmp( &str[parse_pos], opts[idx].name, opt_len ) == 0 )
96 /* option was found */
97 next = 1;
99 assert( opts[idx].valp && "Need a pointer to store the arg!" );
101 /* type specific code */
102 if ( opts[idx].type == OPT_ARG_BOOL )
104 /* Handle OPT_ARG_BOOL separately so *
105 * the others can share code. */
107 /* set option to true */
108 *((int *)(opts[idx].valp)) = 1;
110 /* increment position */
111 parse_pos += opt_len;
113 else
115 /* Type is not OPT_ARG_BOOL, means we have to parse *
116 * for the arg delimiter character and eventually *
117 * call a test function. */
118 char const * last;
120 /* increment position to check for arg */
121 parse_pos += opt_len;
123 if ( str[parse_pos] != '=' )
125 parse_err = 1; break;
128 /* '=' char was there, so let's move after it */
129 ++parse_pos;
131 switch ( opts[idx].type )
133 case OPT_ARG_INT:
134 last = parse_int( &str[parse_pos],
135 (int *)opts[idx].valp );
137 break;
138 case OPT_ARG_STR:
139 last = parse_str( &str[parse_pos],
140 (strarg_t *)opts[idx].valp );
141 break;
142 case OPT_ARG_MSTRZ:
144 char **valp = opts[idx].valp;
145 strarg_t tmp;
146 tmp.str = NULL;
147 tmp.len = 0;
148 last = parse_str( &str[parse_pos], &tmp );
149 if (*valp)
150 free(*valp);
151 *valp = NULL;
152 if (tmp.str && tmp.len > 0) {
153 *valp = malloc(tmp.len + 1);
154 memcpy(*valp, tmp.str, tmp.len);
155 (*valp)[tmp.len] = 0;
157 break;
159 case OPT_ARG_FLOAT:
160 last = parse_float( &str[parse_pos],
161 (float *)opts[idx].valp );
162 break;
163 default:
164 assert( 0 && "Arg type of suboption doesn't exist!" );
165 last = NULL; // break parsing!
168 /* was the conversion succesful? */
169 if ( !last )
171 parse_err = 1; break;
174 /* make test if supplied */
175 if ( opts[idx].test && !opts[idx].test( opts[idx].valp ) )
177 parse_err = 1; break;
180 /* we succeded, set position */
181 parse_pos = last - str;
185 else if ( substr_len == opt_len+2 )
187 if ( opts[idx].type == OPT_ARG_BOOL && // check for no<opt>
188 strncmp( &str[parse_pos], "no", 2 ) == 0 &&
189 strncmp( &str[parse_pos+2], opts[idx].name, opt_len ) == 0 )
191 /* option was found but negated */
192 next = 1;
194 /* set arg to false */
195 *((int *)(opts[idx].valp)) = 0;
197 /* increment position */
198 parse_pos += opt_len+2;
202 ++idx; // test against next option
204 /* break out of the loop, if this subopt is processed */
205 if ( next ) { break; }
208 /* if we had a valid suboption the current pos should *
209 * equal the delimiter char, which should be ':' for *
210 * suboptions. */
211 if ( !parse_err && str[parse_pos] == ':' ) { ++parse_pos; }
212 else if ( str[parse_pos] ) { parse_err = 1; }
216 /* if an error was encountered */
217 if (parse_err)
219 unsigned int i;
220 mp_msg( MSGT_VO, MSGL_FATAL, "Could not parse arguments at the position indicated below:\n%s\n", str );
221 for ( i = 0; i < parse_pos; ++i )
223 mp_msg(MSGT_VO, MSGL_FATAL, " ");
225 mp_msg(MSGT_VO, MSGL_FATAL, "^\n");
227 return -1;
230 /* we could parse everything */
231 return 0;
234 static char const * parse_int( char const * const str, int * const valp )
236 char * endp;
238 assert( str && "parse_int(): str == NULL" );
240 *valp = (int)strtol( str, &endp, 0 );
242 /* nothing was converted */
243 if ( str == endp ) { return NULL; }
245 return endp;
248 static char const * parse_float( char const * const str, float * const valp )
250 char * endp;
252 assert( str && "parse_float(): str == NULL" );
254 *valp = strtod( str, &endp );
256 /* nothing was converted */
257 if ( str == endp ) { return NULL; }
259 return endp;
262 #define QUOTE_CHAR '%'
263 static char const * parse_str( char const * str, strarg_t * const valp )
265 char const * match = strchr( str, ':' );
267 if (str[0] == QUOTE_CHAR) {
268 int len = 0;
269 str = &str[1];
270 len = (int)strtol(str, (char **)&str, 0);
271 if (!str || str[0] != QUOTE_CHAR || (len > strlen(str) - 1))
272 return NULL;
273 str = &str[1];
274 match = &str[len];
276 else
277 if (str[0] == '"') {
278 str = &str[1];
279 match = strchr(str, '"');
280 if (!match)
281 return NULL;
282 valp->len = match - str;
283 valp->str = str;
284 return &match[1];
286 if ( !match )
287 match = &str[strlen(str)];
289 // empty string or too long
290 if ((match == str) || (match - str > INT_MAX))
291 return NULL;
293 valp->len = match - str;
294 valp->str = str;
296 return match;
300 /*** common test functions ***/
302 /** \brief Test if i is not negative */
303 int int_non_neg( int * i )
305 if ( *i < 0 ) { return 0; }
307 return 1;
309 /** \brief Test if i is positive. */
310 int int_pos( int * i )
312 if ( *i > 0 ) { return 1; }
314 return 0;
317 /*** little helpers */
319 /** \brief compare the stings just as strcmp does */
320 int strargcmp(strarg_t *arg, const char *str) {
321 int res = strncmp(arg->str, str, arg->len);
322 if (!res && arg->len != strlen(str))
323 res = arg->len - strlen(str);
324 return res;
327 /** \brief compare the stings just as strcasecmp does */
328 int strargcasecmp(strarg_t *arg, char *str) {
329 int res = strncasecmp(arg->str, str, arg->len);
330 if (!res && arg->len != strlen(str))
331 res = arg->len - strlen(str);
332 return res;