1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (config_file_userdata.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <file/file_path.h>
24 #include <lists/string_list.h>
26 #include <file/config_file_userdata.h>
28 int config_userdata_get_float(void *userdata, const char *key_str,
29 float *value, float default_value)
33 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
35 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
36 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
38 got = config_get_float (usr->conf, key[0], value);
39 got = got || config_get_float(usr->conf, key[1], value);
42 *value = default_value;
46 int config_userdata_get_int(void *userdata, const char *key_str,
47 int *value, int default_value)
51 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
53 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
54 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
56 got = config_get_int (usr->conf, key[0], value);
57 got = got || config_get_int(usr->conf, key[1], value);
60 *value = default_value;
64 int config_userdata_get_hex(void *userdata, const char *key_str,
65 unsigned *value, unsigned default_value)
69 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
71 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
72 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
74 got = config_get_hex(usr->conf, key[0], value);
75 got = got || config_get_hex(usr->conf, key[1], value);
78 *value = default_value;
82 int config_userdata_get_float_array(void *userdata, const char *key_str,
83 float **values, unsigned *out_num_values,
84 const float *default_values, unsigned num_default_values)
87 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
90 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
91 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
93 if ( config_get_string(usr->conf, key[0], &str) ||
94 config_get_string(usr->conf, key[1], &str))
97 struct string_list list = {0};
98 string_list_initialize(&list);
99 string_split_noalloc(&list, str, " ");
100 *values = (float*)calloc(list.size, sizeof(float));
101 for (i = 0; i < list.size; i++)
102 (*values)[i] = (float)strtod(list.elems[i].data, NULL);
103 *out_num_values = (unsigned)list.size;
104 string_list_deinitialize(&list);
109 *values = (float*)calloc(num_default_values, sizeof(float));
110 memcpy(*values, default_values, sizeof(float) * num_default_values);
111 *out_num_values = num_default_values;
115 int config_userdata_get_int_array(void *userdata, const char *key_str,
116 int **values, unsigned *out_num_values,
117 const int *default_values, unsigned num_default_values)
120 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
122 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
123 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
125 if ( config_get_string(usr->conf, key[0], &str) ||
126 config_get_string(usr->conf, key[1], &str))
129 struct string_list list = {0};
130 string_list_initialize(&list);
131 string_split_noalloc(&list, str, " ");
132 *values = (int*)calloc(list.size, sizeof(int));
133 for (i = 0; i < list.size; i++)
134 (*values)[i] = (int)strtod(list.elems[i].data, NULL);
135 *out_num_values = (unsigned)list.size;
136 string_list_deinitialize(&list);
141 *values = (int*)calloc(num_default_values, sizeof(int));
142 memcpy(*values, default_values, sizeof(int) * num_default_values);
143 *out_num_values = num_default_values;
147 int config_userdata_get_string(void *userdata, const char *key_str,
148 char **output, const char *default_output)
151 struct config_file_userdata *usr = (struct config_file_userdata*)userdata;
153 fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0]));
154 fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1]));
156 if ( config_get_string(usr->conf, key[0], &str) ||
157 config_get_string(usr->conf, key[1], &str))
163 *output = strdup(default_output);
167 void config_userdata_free(void *ptr)