forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_file_userdata.c
149 lines (128 loc) · 5.42 KB
/
config_file_userdata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (C) 2010-2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (config_file_userdata.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <file/file_path.h>
#include <lists/string_list.h>
#include "config_file_userdata.h"
int config_userdata_get_float(void *userdata, const char *key_str,
float *value, float default_value)
{
bool got;
char key[2][256];
struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
got = config_get_float (usr->conf, key[0], value);
got = got || config_get_float(usr->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
int config_userdata_get_int(void *userdata, const char *key_str,
int *value, int default_value)
{
bool got;
char key[2][256];
struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
got = config_get_int (usr->conf, key[0], value);
got = got || config_get_int(usr->conf, key[1], value);
if (!got)
*value = default_value;
return got;
}
int config_userdata_get_float_array(void *userdata, const char *key_str,
float **values, unsigned *out_num_values,
const float *default_values, unsigned num_default_values)
{
char key[2][256];
struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
char *str = NULL;
fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
if ( config_get_string(usr->conf, key[0], &str) ||
config_get_string(usr->conf, key[1], &str))
{
unsigned i;
struct string_list *list = string_split(str, " ");
*values = (float*)calloc(list->size, sizeof(float));
for (i = 0; i < list->size; i++)
(*values)[i] = (float)strtod(list->elems[i].data, NULL);
*out_num_values = list->size;
string_list_free(list);
free(str);
return true;
}
*values = (float*)calloc(num_default_values, sizeof(float));
memcpy(*values, default_values, sizeof(float) * num_default_values);
*out_num_values = num_default_values;
return false;
}
int config_userdata_get_int_array(void *userdata, const char *key_str,
int **values, unsigned *out_num_values,
const int *default_values, unsigned num_default_values)
{
char key[2][256];
struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
char *str = NULL;
fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
if ( config_get_string(usr->conf, key[0], &str) ||
config_get_string(usr->conf, key[1], &str))
{
unsigned i;
struct string_list *list = string_split(str, " ");
*values = (int*)calloc(list->size, sizeof(int));
for (i = 0; i < list->size; i++)
(*values)[i] = (int)strtod(list->elems[i].data, NULL);
*out_num_values = list->size;
string_list_free(list);
free(str);
return true;
}
*values = (int*)calloc(num_default_values, sizeof(int));
memcpy(*values, default_values, sizeof(int) * num_default_values);
*out_num_values = num_default_values;
return false;
}
int config_userdata_get_string(void *userdata, const char *key_str,
char **output, const char *default_output)
{
char key[2][256];
struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
char *str = NULL;
fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
if ( config_get_string(usr->conf, key[0], &str) ||
config_get_string(usr->conf, key[1], &str))
{
*output = str;
return true;
}
*output = strdup(default_output);
return false;
}
void config_userdata_free(void *ptr)
{
if (ptr)
free(ptr);
}