1 /*****************************************************************************
2 * filters.c: common filter functions
3 *****************************************************************************
4 * Copyright (C) 2010-2017 x264 project
6 * Authors: Diogo Franco <diogomfranco@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
8 * Henrik Gramner <henrik@gramner.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
29 #define RETURN_IF_ERROR( cond, ... ) RETURN_IF_ERR( cond, "options", NULL, __VA_ARGS__ )
31 char **x264_split_options( const char *opt_str
, const char * const *options
)
33 int opt_count
= 0, options_count
= 0, found_named
= 0, size
= 0;
34 const char *opt
= opt_str
;
39 while( options
[options_count
] )
44 int length
= strcspn( opt
, "=," );
45 if( opt
[length
] == '=' )
47 const char * const *option
= options
;
48 while( *option
&& (strlen( *option
) != length
|| strncmp( opt
, *option
, length
)) )
51 RETURN_IF_ERROR( !*option
, "Invalid option '%.*s'\n", length
, opt
);
53 length
+= strcspn( opt
+ length
, "," );
57 RETURN_IF_ERROR( opt_count
>= options_count
, "Too many options given\n" );
58 RETURN_IF_ERROR( found_named
, "Ordered option given after named\n" );
59 size
+= strlen( options
[opt_count
] ) + 1;
65 int offset
= 2 * (opt_count
+1) * sizeof(char*);
66 size
+= offset
+ (opt
- opt_str
);
67 char **opts
= calloc( 1, size
);
68 RETURN_IF_ERROR( !opts
, "malloc failed\n" );
70 #define insert_opt( src, length )\
72 opts[i++] = memcpy( (char*)opts + offset, src, length );\
73 offset += length + 1;\
77 for( int i
= 0; i
< 2*opt_count
; )
79 int length
= strcspn( opt_str
, "=," );
80 if( opt_str
[length
] == '=' )
82 insert_opt( opt_str
, length
);
83 length
= strcspn( opt_str
, "," );
87 const char *option
= options
[i
/2];
88 int option_length
= strlen( option
);
89 insert_opt( option
, option_length
);
91 insert_opt( opt_str
, length
);
94 assert( offset
== size
);
98 char *x264_get_option( const char *name
, char **split_options
)
103 for( int i
= 0; split_options
[i
]; i
+= 2 )
104 if( !strcmp( split_options
[i
], name
) )
106 if( last_i
>= 0 && split_options
[last_i
+1][0] )
107 return split_options
[last_i
+1];
112 int x264_otob( const char *str
, int def
)
115 return !strcasecmp( str
, "true" ) || !strcmp( str
, "1" ) || !strcasecmp( str
, "yes" );
119 double x264_otof( const char *str
, double def
)
125 ret
= strtod( str
, &end
);
126 if( end
== str
|| *end
!= '\0' )
132 int x264_otoi( const char *str
, int def
)
138 ret
= strtol( str
, &end
, 0 );
139 if( end
== str
|| *end
!= '\0' )
145 char *x264_otos( char *str
, char *def
)
147 return str
? str
: def
;