1 #include <coreopt.h>
2 #include <string.h>
3
coreopt(retro_environment_t env_cb,const struct retro_variable * vars,const char * opt_name,const char ** opt_value)4 int coreopt( retro_environment_t env_cb, const struct retro_variable* vars, const char* opt_name, const char** opt_value )
5 {
6 const struct retro_variable* var = vars;
7
8 while ( var->key )
9 {
10 if ( !strcmp( var->key, opt_name ) )
11 {
12 break;
13 }
14
15 ++var;
16 }
17
18 if ( var->key )
19 {
20 const char* first_opt = strchr( var->value, ';' );
21
22 if ( first_opt )
23 {
24 while ( *++first_opt == ' ' ) /* do nothing */;
25
26 struct retro_variable user_opt;
27
28 user_opt.key = var->key;
29 user_opt.value = NULL;
30
31 if ( env_cb( RETRO_ENVIRONMENT_GET_VARIABLE, &user_opt ) && user_opt.value )
32 {
33 const char* option = first_opt;
34 size_t opt_len = strlen( user_opt.value );
35
36 for ( ;; )
37 {
38 option = strstr( option, user_opt.value );
39
40 if ( !option )
41 {
42 break;
43 }
44
45 if ( ( option == first_opt || option[ -1 ] == '|' ) || ( option[ opt_len ] == 0 || option[ opt_len ] == '|' ) )
46 {
47 int pipes = 0;
48
49 while ( option > first_opt )
50 {
51 pipes += *--option == '|';
52 }
53
54 if ( opt_value )
55 {
56 *opt_value = user_opt.value;
57 }
58
59 return pipes;
60 }
61
62 option += opt_len;
63 }
64 }
65 }
66 }
67
68 return -1;
69 }
70