1 /*****************************************************************************
2 * filters.c: common filter functions
3 *****************************************************************************
4 * Copyright (C) 2010-2016 x264 project
6 * Authors: Diogo Franco <diogomfranco@gmail.com>
7 * Steven Walters <kemuri9@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
28 #define RETURN_IF_ERROR( cond, ... ) RETURN_IF_ERR( cond, "options", NULL, __VA_ARGS__ )
30 char **x264_split_string( char *string, char *sep, int limit )
35 int sep_len = strlen( sep );
37 while( ( tmp = ( tmp = strstr( tmp, sep ) ) ? tmp + sep_len : 0 ) )
41 if( string[0] == '\0' )
42 return calloc( 1, sizeof( char* ) );
43 char **ret = calloc( 2, sizeof( char* ) );
44 ret[0] = strdup( string );
48 char **split = calloc( ( limit > 0 ? limit : sep_count ) + 2, sizeof(char*) );
50 char *str = strdup( string );
53 char *tok = str, *nexttok = str;
56 nexttok = strstr( nexttok, sep );
59 if( ( limit > 0 && i >= limit ) ||
60 ( i > 0 && ( ( esc = strrchr( split[i-1], '\\' ) ) ? esc[1] == '\0' : 0 ) ) ) // Allow escaping
65 split[j] = realloc( split[j], strlen( split[j] ) + sep_len + strlen( tok ) + 1 );
67 strcat( split[j], sep );
68 strcat( split[j], tok );
73 split[i++] = strdup( tok );
84 void x264_free_string_array( char **array )
88 for( int i = 0; array[i] != NULL; i++ )
93 char **x264_split_options( const char *opt_str, const char *options[] )
97 char *opt_str_dup = strdup( opt_str );
98 char **split = x264_split_string( opt_str_dup, ",", 0 );
101 while( split[split_count] != NULL )
104 int options_count = 0;
105 while( options[options_count] != NULL )
108 char **opts = calloc( split_count * 2 + 2, sizeof( char * ) );
110 int opt = 0, found_named = 0, invalid = 0;
111 for( int i = 0; split[i] != NULL; i++, invalid = 0 )
113 arg = x264_split_string( split[i], "=", 2 );
118 else RETURN_IF_ERROR( i > options_count || options[i] == NULL, "Too many options given\n" )
121 opts[opt++] = strdup( options[i] );
122 opts[opt++] = strdup( "" );
125 else if( arg[0] == NULL || arg[1] == NULL )
129 else RETURN_IF_ERROR( i > options_count || options[i] == NULL, "Too many options given\n" )
132 opts[opt++] = strdup( options[i] );
134 opts[opt++] = strdup( arg[0] );
136 opts[opt++] = strdup( "" );
143 while( options[j] != NULL && strcmp( arg[0], options[j] ) )
145 RETURN_IF_ERROR( options[j] == NULL, "Invalid option '%s'\n", arg[0] )
148 opts[opt++] = strdup( arg[0] );
149 opts[opt++] = strdup( arg[1] );
152 RETURN_IF_ERROR( invalid, "Ordered option given after named\n" )
153 x264_free_string_array( arg );
155 x264_free_string_array( split );
159 char *x264_get_option( const char *name, char **split_options )
164 for( int i = 0; split_options[i] != NULL; i += 2 )
165 if( !strcmp( split_options[i], name ) )
168 return split_options[last_i+1][0] ? split_options[last_i+1] : NULL;
172 int x264_otob( char *str, int def )
176 ret = !strcasecmp( str, "true" ) ||
177 !strcmp( str, "1" ) ||
178 !strcasecmp( str, "yes" );
182 double x264_otof( char *str, double def )
188 ret = strtod( str, &end );
189 if( end == str || *end != '\0' )
195 int x264_otoi( char *str, int def )
201 ret = strtol( str, &end, 0 );
202 if( end == str || *end != '\0' )
208 char *x264_otos( char *str, char *def )
210 return str ? str : def;