Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (config_file_userdata.c). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
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. | |
21 | */ | |
22 | ||
23 | #include <file/file_path.h> | |
24 | #include <lists/string_list.h> | |
25 | ||
26 | #include <file/config_file_userdata.h> | |
27 | ||
28 | int config_userdata_get_float(void *userdata, const char *key_str, | |
29 | float *value, float default_value) | |
30 | { | |
31 | bool got; | |
32 | char key[2][256]; | |
33 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
34 | ||
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])); | |
37 | ||
38 | got = config_get_float (usr->conf, key[0], value); | |
39 | got = got || config_get_float(usr->conf, key[1], value); | |
40 | ||
41 | if (!got) | |
42 | *value = default_value; | |
43 | return got; | |
44 | } | |
45 | ||
46 | int config_userdata_get_int(void *userdata, const char *key_str, | |
47 | int *value, int default_value) | |
48 | { | |
49 | bool got; | |
50 | char key[2][256]; | |
51 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
52 | ||
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])); | |
55 | ||
56 | got = config_get_int (usr->conf, key[0], value); | |
57 | got = got || config_get_int(usr->conf, key[1], value); | |
58 | ||
59 | if (!got) | |
60 | *value = default_value; | |
61 | return got; | |
62 | } | |
63 | ||
64 | int config_userdata_get_hex(void *userdata, const char *key_str, | |
65 | unsigned *value, unsigned default_value) | |
66 | { | |
67 | bool got; | |
68 | char key[2][256]; | |
69 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
70 | ||
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])); | |
73 | ||
74 | got = config_get_hex(usr->conf, key[0], value); | |
75 | got = got || config_get_hex(usr->conf, key[1], value); | |
76 | ||
77 | if (!got) | |
78 | *value = default_value; | |
79 | return got; | |
80 | } | |
81 | ||
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) | |
85 | { | |
86 | char key[2][256]; | |
87 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
88 | char *str = NULL; | |
89 | ||
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])); | |
92 | ||
93 | if ( config_get_string(usr->conf, key[0], &str) || | |
94 | config_get_string(usr->conf, key[1], &str)) | |
95 | { | |
96 | unsigned i; | |
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); | |
105 | free(str); | |
106 | return true; | |
107 | } | |
108 | ||
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; | |
112 | return false; | |
113 | } | |
114 | ||
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) | |
118 | { | |
119 | char key[2][256]; | |
120 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
121 | char *str = NULL; | |
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])); | |
124 | ||
125 | if ( config_get_string(usr->conf, key[0], &str) || | |
126 | config_get_string(usr->conf, key[1], &str)) | |
127 | { | |
128 | unsigned i; | |
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); | |
137 | free(str); | |
138 | return true; | |
139 | } | |
140 | ||
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; | |
144 | return false; | |
145 | } | |
146 | ||
147 | int config_userdata_get_string(void *userdata, const char *key_str, | |
148 | char **output, const char *default_output) | |
149 | { | |
150 | char key[2][256]; | |
151 | struct config_file_userdata *usr = (struct config_file_userdata*)userdata; | |
152 | char *str = NULL; | |
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])); | |
155 | ||
156 | if ( config_get_string(usr->conf, key[0], &str) || | |
157 | config_get_string(usr->conf, key[1], &str)) | |
158 | { | |
159 | *output = str; | |
160 | return true; | |
161 | } | |
162 | ||
163 | *output = strdup(default_output); | |
164 | return false; | |
165 | } | |
166 | ||
167 | void config_userdata_free(void *ptr) | |
168 | { | |
169 | if (ptr) | |
170 | free(ptr); | |
171 | } |