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
20 #include "subopt-helper.h"
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
);
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;
55 while ( str
[parse_pos
] && !parse_err
)
59 idx
= 0; // reset index for the below loop
60 while ( opts
[idx
].name
)
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
) ||
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 */
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
;
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. */
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 */
131 switch ( opts
[idx
].type
)
134 last
= parse_int( &str
[parse_pos
],
135 (int *)opts
[idx
].valp
);
139 last
= parse_str( &str
[parse_pos
],
140 (strarg_t
*)opts
[idx
].valp
);
144 char **valp
= opts
[idx
].valp
;
148 last
= parse_str( &str
[parse_pos
], &tmp
);
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;
160 last
= parse_float( &str
[parse_pos
],
161 (float *)opts
[idx
].valp
);
164 assert( 0 && "Arg type of suboption doesn't exist!" );
165 last
= NULL
; // break parsing!
168 /* was the conversion succesful? */
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 */
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 *
211 if ( !parse_err
&& str
[parse_pos
] == ':' ) { ++parse_pos
; }
212 else if ( str
[parse_pos
] ) { parse_err
= 1; }
216 /* if an error was encountered */
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");
230 /* we could parse everything */
234 static char const * parse_int( char const * const str
, int * const valp
)
238 assert( str
&& "parse_int(): str == NULL" );
240 *valp
= (int)strtol( str
, &endp
, 0 );
242 /* nothing was converted */
243 if ( str
== endp
) { return NULL
; }
248 static char const * parse_float( char const * const str
, float * const valp
)
252 assert( str
&& "parse_float(): str == NULL" );
254 *valp
= strtod( str
, &endp
);
256 /* nothing was converted */
257 if ( str
== endp
) { return NULL
; }
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
) {
270 len
= (int)strtol(str
, (char **)&str
, 0);
271 if (!str
|| str
[0] != QUOTE_CHAR
|| (len
> strlen(str
) - 1))
279 match
= strchr(str
, '"');
282 valp
->len
= match
- str
;
287 match
= &str
[strlen(str
)];
289 // empty string or too long
290 if ((match
== str
) || (match
- str
> INT_MAX
))
293 valp
->len
= match
- str
;
300 /*** common test functions ***/
302 /** \brief Test if i is not negative */
303 int int_non_neg(void *iptr
)
308 /** \brief Test if i is positive. */
309 int int_pos(void *iptr
)
315 /*** little helpers */
317 /** \brief compare the stings just as strcmp does */
318 int strargcmp(strarg_t
*arg
, const char *str
) {
319 int res
= strncmp(arg
->str
, str
, arg
->len
);
320 if (!res
&& arg
->len
!= strlen(str
))
321 res
= arg
->len
- strlen(str
);
325 /** \brief compare the stings just as strcasecmp does */
326 int strargcasecmp(strarg_t
*arg
, char *str
) {
327 int res
= strncasecmp(arg
->str
, str
, arg
->len
);
328 if (!res
&& arg
->len
!= strlen(str
))
329 res
= arg
->len
- strlen(str
);