1 /*****************************************************************************
2 * filters.c: common filter functions
3 *****************************************************************************
4 * Copyright (C) 2010-2015 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 )
36 while( ( tmp = ( tmp = strstr( tmp, sep ) ) ? tmp + strlen( sep ) : 0 ) )
40 if( string[0] == '\0' )
41 return calloc( 1, sizeof( char* ) );
42 char **ret = calloc( 2, sizeof( char* ) );
43 ret[0] = strdup( string );
47 char **split = calloc( ( limit > 0 ? limit : sep_count ) + 2, sizeof(char*) );
49 char *str = strdup( string );
52 char *tok = str, *nexttok = str;
55 nexttok = strstr( nexttok, sep );
58 if( ( limit > 0 && i >= limit ) ||
59 ( i > 0 && ( ( esc = strrchr( split[i-1], '\\' ) ) ? esc[1] == '\0' : 0 ) ) ) // Allow escaping
64 split[j] = realloc( split[j], strlen( split[j] ) + strlen( sep ) + strlen( tok ) + 1 );
66 strcat( split[j], sep );
67 strcat( split[j], tok );
72 split[i++] = strdup( tok );
83 void x264_free_string_array( char **array )
87 for( int i = 0; array[i] != NULL; i++ )
92 char **x264_split_options( const char *opt_str, const char *options[] )
96 char *opt_str_dup = strdup( opt_str );
97 char **split = x264_split_string( opt_str_dup, ",", 0 );
100 while( split[split_count] != NULL )
103 int options_count = 0;
104 while( options[options_count] != NULL )
107 char **opts = calloc( split_count * 2 + 2, sizeof( char * ) );
109 int opt = 0, found_named = 0, invalid = 0;
110 for( int i = 0; split[i] != NULL; i++, invalid = 0 )
112 arg = x264_split_string( split[i], "=", 2 );
117 else RETURN_IF_ERROR( i > options_count || options[i] == NULL, "Too many options given\n" )
120 opts[opt++] = strdup( options[i] );
121 opts[opt++] = strdup( "" );
124 else if( arg[0] == NULL || arg[1] == NULL )
128 else RETURN_IF_ERROR( i > options_count || options[i] == NULL, "Too many options given\n" )
131 opts[opt++] = strdup( options[i] );
133 opts[opt++] = strdup( arg[0] );
135 opts[opt++] = strdup( "" );
142 while( options[j] != NULL && strcmp( arg[0], options[j] ) )
144 RETURN_IF_ERROR( options[j] == NULL, "Invalid option '%s'\n", arg[0] )
147 opts[opt++] = strdup( arg[0] );
148 opts[opt++] = strdup( arg[1] );
151 RETURN_IF_ERROR( invalid, "Ordered option given after named\n" )
152 x264_free_string_array( arg );
154 x264_free_string_array( split );
158 char *x264_get_option( const char *name, char **split_options )
163 for( int i = 0; split_options[i] != NULL; i += 2 )
164 if( !strcmp( split_options[i], name ) )
167 return split_options[last_i+1][0] ? split_options[last_i+1] : NULL;
171 int x264_otob( char *str, int def )
175 ret = !strcasecmp( str, "true" ) ||
176 !strcmp( str, "1" ) ||
177 !strcasecmp( str, "yes" );
181 double x264_otof( char *str, double def )
187 ret = strtod( str, &end );
188 if( end == str || *end != '\0' )
194 int x264_otoi( char *str, int def )
200 ret = strtol( str, &end, 0 );
201 if( end == str || *end != '\0' )
207 char *x264_otos( char *str, char *def )
209 return str ? str : def;