liba52 asm optimizations ported to amd64
[mplayer.git] / subopt-helper.c
blob9ae6669a0565f6362ab412a8aa2799c4a7bda7c8
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 );
36 /**
37 * \brief Try to parse all options in str and fail if it was not possible.
39 * \param str Pointer to the zero terminated string to be parsed.
40 * \param opts Pointer to a options array. The array must be terminated
41 * with an element having set name to NULL in its opt_t structure.
43 * \return The return value is zero if the string could be parsed
44 * else a non-zero value is returned.
47 int subopt_parse( char const * const str, opt_t * opts )
49 int parse_err = 0, idx;
50 unsigned int parse_pos = 0;
52 /* Initialize set member to false. *
53 * It is set to true if it was found in str */
54 for ( idx=0; opts[idx].name; ++idx )
56 opts[idx].set = 0;
59 if ( str )
61 while ( str[parse_pos] && !parse_err )
63 int next = 0;
65 idx = 0; // reset index for the below loop
66 while ( opts[idx].name )
68 int opt_len;
69 int substr_len;
71 // get length of the option we test against */
72 opt_len = strlen( opts[idx].name );
74 // get length of the current substring of str */
76 char * delim, * arg_delim;
78 /* search nearest delimiter ( option or argument delimiter ) */
79 delim = strchr( &str[parse_pos], ':' );
80 arg_delim = strchr( &str[parse_pos], '=' );
82 if ( ( delim && arg_delim && delim > arg_delim ) ||
83 delim == NULL )
85 delim = strchr( &str[parse_pos], '=' );
88 substr_len = delim ? // is a delim present
89 delim - &str[parse_pos] : // yes
90 strlen( &str[parse_pos] ); // no, end of string
93 //printf( "substr_len=%d, opt_len=%d\n", substr_len, opt_len );
95 /* Check if the length of the current option matches the *
96 * length of the option we want to test again. */
97 if ( substr_len == opt_len )
99 /* check if option was activated/deactivated */
100 if( strncmp( &str[parse_pos], opts[idx].name, opt_len ) == 0 )
102 /* option was found */
103 opts[idx].set = 1; next = 1;
105 assert( opts[idx].valp && "Need a pointer to store the arg!" );
107 /* type specific code */
108 if ( opts[idx].type == OPT_ARG_BOOL )
110 /* Handle OPT_ARG_BOOL seperately so *
111 * the others can share code. */
113 /* set option to true */
114 *((int *)(opts[idx].valp)) = 1;
116 /* increment position */
117 parse_pos += opt_len;
119 else
121 /* Type is not OPT_ARG_BOOL, means we have to parse *
122 * for the arg delimiter character and eventually *
123 * call a test function. */
124 char const * last;
126 /* increment position to check for arg */
127 parse_pos += opt_len;
129 if ( str[parse_pos] != '=' )
131 parse_err = 1; break;
134 /* '=' char was there, so let's move after it */
135 ++parse_pos;
137 switch ( opts[idx].type )
139 case OPT_ARG_INT:
140 last = parse_int( &str[parse_pos],
141 (int *)opts[idx].valp );
143 break;
144 case OPT_ARG_STR:
145 last = parse_str( &str[parse_pos],
146 (strarg_t *)opts[idx].valp );
147 break;
148 case OPT_ARG_MSTRZ:
150 char **valp = opts[idx].valp;
151 strarg_t tmp;
152 tmp.str = NULL;
153 tmp.len = 0;
154 last = parse_str( &str[parse_pos], &tmp );
155 if (*valp)
156 free(*valp);
157 *valp = NULL;
158 if (tmp.str && tmp.len > 0) {
159 *valp = malloc(tmp.len + 1);
160 memcpy(*valp, tmp.str, tmp.len);
161 (*valp)[tmp.len] = 0;
163 break;
165 default:
166 assert( 0 && "Arg type of suboption doesn't exist!" );
167 last = NULL; // break parsing!
170 /* was the conversion succesful? */
171 if ( !last )
173 parse_err = 1; break;
176 /* make test if supplied */
177 if ( opts[idx].test && !opts[idx].test( opts[idx].valp ) )
179 parse_err = 1; break;
182 /* we succeded, set position */
183 parse_pos = last - str;
187 else if ( substr_len == opt_len+2 )
189 if ( opts[idx].type == OPT_ARG_BOOL && // check for no<opt>
190 strncmp( &str[parse_pos], "no", 2 ) == 0 &&
191 strncmp( &str[parse_pos+2], opts[idx].name, opt_len ) == 0 )
193 /* option was found but negated */
194 opts[idx].set = 1; next = 1;
196 /* set arg to false */
197 *((int *)(opts[idx].valp)) = 0;
199 /* increment position */
200 parse_pos += opt_len+2;
204 ++idx; // test against next option
206 /* break out of the loop, if this subopt is processed */
207 if ( next ) { break; }
210 /* if we had a valid suboption the current pos should *
211 * equal the delimiter char, which should be ':' for *
212 * suboptions. */
213 if ( !parse_err && str[parse_pos] == ':' ) { ++parse_pos; }
214 else if ( str[parse_pos] ) { parse_err = 1; }
218 /* if an error was encountered */
219 if (parse_err)
221 unsigned int i;
222 mp_msg( MSGT_VO, MSGL_FATAL, "Could not parse arguments at the position indicated below:\n%s\n", str );
223 for ( i = 0; i < parse_pos; ++i )
225 mp_msg(MSGT_VO, MSGL_FATAL, " ");
227 mp_msg(MSGT_VO, MSGL_FATAL, "^\n");
229 return -1;
232 /* we could parse everything */
233 return 0;
236 static char const * parse_int( char const * const str, int * const valp )
238 char * endp;
240 assert( str && "parse_int(): str == NULL" );
242 *valp = (int)strtol( str, &endp, 0 );
244 /* nothing was converted */
245 if ( str == endp ) { return NULL; }
247 return endp;
250 #define QUOTE_CHAR '%'
251 static char const * parse_str( char const * str, strarg_t * const valp )
253 char const * match = strchr( str, ':' );
255 if (str[0] == QUOTE_CHAR) {
256 int len = 0;
257 str = &str[1];
258 len = (int)strtol(str, (char **)&str, 0);
259 if (!str || str[0] != QUOTE_CHAR || (len > strlen(str) - 1))
260 return NULL;
261 str = &str[1];
262 match = &str[len];
264 else
265 if ( !match )
266 match = &str[strlen(str)];
268 // empty string or too long
269 if ((match == str) || (match - str > INT_MAX))
270 return NULL;
272 valp->len = match - str;
273 valp->str = str;
275 return match;
279 /*** common test functions ***/
281 /** \brief Test if i is not negative */
282 int int_non_neg( int * i )
284 if ( *i < 0 ) { return 0; }
286 return 1;
288 /** \brief Test if i is positive. */
289 int int_pos( int * i )
291 if ( *i > 0 ) { return 1; }
293 return 0;
296 /*** little helpers */
298 /** \brief compare the stings just as strcmp does */
299 int strargcmp(strarg_t *arg, char *str) {
300 int res = strncmp(arg->str, str, arg->len);
301 if (!res && arg->len != strlen(str))
302 res = arg->len - strlen(str);
303 return res;
306 /** \brief compare the stings just as strcasecmp does */
307 int strargcasecmp(strarg_t *arg, char *str) {
308 int res = strncasecmp(arg->str, str, arg->len);
309 if (!res && arg->len != strlen(str))
310 res = arg->len - strlen(str);
311 return res;