]> git.sesse.net Git - x264/blob - filters/filters.c
checkasm: Disable Windows Error Reporting
[x264] / filters / filters.c
1 /*****************************************************************************
2  * filters.c: common filter functions
3  *****************************************************************************
4  * Copyright (C) 2010-2016 x264 project
5  *
6  * Authors: Diogo Franco <diogomfranco@gmail.com>
7  *          Steven Walters <kemuri9@gmail.com>
8  *          Henrik Gramner <henrik@gramner.com>
9  *
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.
14  *
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.
19  *
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.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "filters.h"
29 #define RETURN_IF_ERROR( cond, ... ) RETURN_IF_ERR( cond, "options", NULL, __VA_ARGS__ )
30
31 char **x264_split_options( const char *opt_str, const char * const *options )
32 {
33     int opt_count = 0, options_count = 0, found_named = 0, size = 0;
34     const char *opt = opt_str;
35
36     if( !opt_str )
37         return NULL;
38
39     while( options[options_count] )
40         options_count++;
41
42     do
43     {
44         int length = strcspn( opt, "=," );
45         if( opt[length] == '=' )
46         {
47             const char * const *option = options;
48             while( *option && (strlen( *option ) != length || strncmp( opt, *option, length )) )
49                 option++;
50
51             RETURN_IF_ERROR( !*option, "Invalid option '%.*s'\n", length, opt )
52             found_named = 1;
53             length += strcspn( opt + length, "," );
54         }
55         else
56         {
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;
60         }
61         opt_count++;
62         opt += length;
63     } while( *opt++ );
64
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" )
69
70 #define insert_opt( src, length )\
71 do {\
72     opts[i++] = memcpy( (char*)opts + offset, src, length );\
73     offset += length + 1;\
74     src    += length + 1;\
75 } while( 0 )
76
77     for( int i = 0; i < 2*opt_count; )
78     {
79         int length = strcspn( opt_str, "=," );
80         if( opt_str[length] == '=' )
81         {
82             insert_opt( opt_str, length );
83             length = strcspn( opt_str, "," );
84         }
85         else
86         {
87             const char *option = options[i/2];
88             int option_length = strlen( option );
89             insert_opt( option, option_length );
90         }
91         insert_opt( opt_str, length );
92     }
93
94     assert( offset == size );
95     return opts;
96 }
97
98 char *x264_get_option( const char *name, char **split_options )
99 {
100     if( split_options )
101     {
102         int last_i = -1;
103         for( int i = 0; split_options[i]; i += 2 )
104             if( !strcmp( split_options[i], name ) )
105                 last_i = i;
106         if( last_i >= 0 && split_options[last_i+1][0] )
107             return split_options[last_i+1];
108     }
109     return NULL;
110 }
111
112 int x264_otob( const char *str, int def )
113 {
114    if( str )
115        return !strcasecmp( str, "true" ) || !strcmp( str, "1" ) || !strcasecmp( str, "yes" );
116    return def;
117 }
118
119 double x264_otof( const char *str, double def )
120 {
121    double ret = def;
122    if( str )
123    {
124        char *end;
125        ret = strtod( str, &end );
126        if( end == str || *end != '\0' )
127            ret = def;
128    }
129    return ret;
130 }
131
132 int x264_otoi( const char *str, int def )
133 {
134     int ret = def;
135     if( str )
136     {
137         char *end;
138         ret = strtol( str, &end, 0 );
139         if( end == str || *end != '\0' )
140             ret = def;
141     }
142     return ret;
143 }
144
145 char *x264_otos( char *str, char *def )
146 {
147     return str ? str : def;
148 }