|
25 | 25 |
|
26 | 26 | #include <file/config_file_userdata.h> |
27 | 27 |
|
| 28 | +#define CFG_KEY_SIZE 256 |
| 29 | + |
28 | 30 | int config_userdata_get_float(void *userdata, const char *key_str, |
29 | 31 | float *value, float default_value) |
30 | 32 | { |
31 | | - char key[256]; |
32 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
| 33 | + char key[CFG_KEY_SIZE]; |
| 34 | + const struct config_file_userdata *usr = |
| 35 | + (const struct config_file_userdata *)userdata; |
| 36 | + |
33 | 37 | fill_pathname_join_delim(key, usr->prefix[0], key_str, '_', sizeof(key)); |
34 | 38 | if (config_get_float(usr->conf, key, value)) |
35 | | - return true; |
36 | | - *value = default_value; |
| 39 | + return 1; |
| 40 | + |
37 | 41 | fill_pathname_join_delim(key, usr->prefix[1], key_str, '_', sizeof(key)); |
38 | 42 | if (config_get_float(usr->conf, key, value)) |
39 | | - return true; |
40 | | - return false; |
| 43 | + return 1; |
| 44 | + |
| 45 | + *value = default_value; |
| 46 | + return 0; |
41 | 47 | } |
42 | 48 |
|
43 | 49 | int config_userdata_get_int(void *userdata, const char *key_str, |
44 | 50 | int *value, int default_value) |
45 | 51 | { |
46 | | - char key[256]; |
47 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
| 52 | + char key[CFG_KEY_SIZE]; |
| 53 | + const struct config_file_userdata *usr = |
| 54 | + (const struct config_file_userdata *)userdata; |
| 55 | + |
48 | 56 | fill_pathname_join_delim(key, usr->prefix[0], key_str, '_', sizeof(key)); |
49 | 57 | if (config_get_int(usr->conf, key, value)) |
50 | | - return true; |
51 | | - *value = default_value; |
| 58 | + return 1; |
| 59 | + |
52 | 60 | fill_pathname_join_delim(key, usr->prefix[1], key_str, '_', sizeof(key)); |
53 | 61 | if (config_get_int(usr->conf, key, value)) |
54 | | - return true; |
55 | | - return false; |
| 62 | + return 1; |
| 63 | + |
| 64 | + *value = default_value; |
| 65 | + return 0; |
56 | 66 | } |
57 | 67 |
|
58 | 68 | int config_userdata_get_hex(void *userdata, const char *key_str, |
59 | 69 | unsigned *value, unsigned default_value) |
60 | 70 | { |
61 | | - char key[256]; |
62 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
| 71 | + char key[CFG_KEY_SIZE]; |
| 72 | + const struct config_file_userdata *usr = |
| 73 | + (const struct config_file_userdata *)userdata; |
| 74 | + |
63 | 75 | fill_pathname_join_delim(key, usr->prefix[0], key_str, '_', sizeof(key)); |
64 | 76 | if (config_get_hex(usr->conf, key, value)) |
65 | | - return true; |
66 | | - *value = default_value; |
| 77 | + return 1; |
| 78 | + |
67 | 79 | fill_pathname_join_delim(key, usr->prefix[1], key_str, '_', sizeof(key)); |
68 | 80 | if (config_get_hex(usr->conf, key, value)) |
69 | | - return true; |
70 | | - return false; |
| 81 | + return 1; |
| 82 | + |
| 83 | + *value = default_value; |
| 84 | + return 0; |
71 | 85 | } |
72 | 86 |
|
73 | 87 | int config_userdata_get_float_array(void *userdata, const char *key_str, |
74 | 88 | float **values, unsigned *out_num_values, |
75 | 89 | const float *default_values, unsigned num_default_values) |
76 | 90 | { |
77 | | - char key[2][256]; |
| 91 | + char key0[CFG_KEY_SIZE]; |
| 92 | + char key1[CFG_KEY_SIZE]; |
78 | 93 | char *str = NULL; |
79 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
| 94 | + const struct config_file_userdata *usr = |
| 95 | + (const struct config_file_userdata *)userdata; |
80 | 96 |
|
81 | | - fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0])); |
82 | | - fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1])); |
| 97 | + fill_pathname_join_delim(key0, usr->prefix[0], key_str, '_', sizeof(key0)); |
| 98 | + fill_pathname_join_delim(key1, usr->prefix[1], key_str, '_', sizeof(key1)); |
83 | 99 |
|
84 | | - if ( config_get_string(usr->conf, key[0], &str) |
85 | | - || config_get_string(usr->conf, key[1], &str)) |
| 100 | + if ( config_get_string(usr->conf, key0, &str) |
| 101 | + || config_get_string(usr->conf, key1, &str)) |
86 | 102 | { |
87 | 103 | unsigned i; |
88 | | - struct string_list list = {0}; |
| 104 | + struct string_list list; |
89 | 105 | string_list_initialize(&list); |
90 | 106 | string_split_noalloc(&list, str, " "); |
91 | | - *values = (float*)calloc(list.size, sizeof(float)); |
92 | | - for (i = 0; i < list.size; i++) |
93 | | - (*values)[i] = (float)strtod(list.elems[i].data, NULL); |
| 107 | + |
| 108 | + *values = (float *)calloc(list.size, sizeof(float)); |
94 | 109 | *out_num_values = (unsigned)list.size; |
| 110 | + |
| 111 | + for (i = 0; i < list.size; i++) |
| 112 | + (*values)[i] = (float)strtof(list.elems[i].data, NULL); |
| 113 | + |
95 | 114 | string_list_deinitialize(&list); |
96 | 115 | free(str); |
97 | | - return true; |
| 116 | + return 1; |
98 | 117 | } |
99 | 118 |
|
100 | | - *values = (float*)calloc(num_default_values, sizeof(float)); |
| 119 | + *values = (float *)calloc(num_default_values, sizeof(float)); |
101 | 120 | memcpy(*values, default_values, sizeof(float) * num_default_values); |
102 | 121 | *out_num_values = num_default_values; |
103 | | - return false; |
| 122 | + return 0; |
104 | 123 | } |
105 | 124 |
|
106 | 125 | int config_userdata_get_int_array(void *userdata, const char *key_str, |
107 | 126 | int **values, unsigned *out_num_values, |
108 | 127 | const int *default_values, unsigned num_default_values) |
109 | 128 | { |
110 | | - char key[2][256]; |
| 129 | + char key0[CFG_KEY_SIZE]; |
| 130 | + char key1[CFG_KEY_SIZE]; |
111 | 131 | char *str = NULL; |
112 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
113 | | - fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0])); |
114 | | - fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1])); |
| 132 | + const struct config_file_userdata *usr = |
| 133 | + (const struct config_file_userdata *)userdata; |
115 | 134 |
|
116 | | - if ( config_get_string(usr->conf, key[0], &str) |
117 | | - || config_get_string(usr->conf, key[1], &str)) |
| 135 | + fill_pathname_join_delim(key0, usr->prefix[0], key_str, '_', sizeof(key0)); |
| 136 | + fill_pathname_join_delim(key1, usr->prefix[1], key_str, '_', sizeof(key1)); |
| 137 | + |
| 138 | + if ( config_get_string(usr->conf, key0, &str) |
| 139 | + || config_get_string(usr->conf, key1, &str)) |
118 | 140 | { |
119 | 141 | unsigned i; |
120 | | - struct string_list list = {0}; |
| 142 | + struct string_list list; |
121 | 143 | string_list_initialize(&list); |
122 | 144 | string_split_noalloc(&list, str, " "); |
123 | | - *values = (int*)calloc(list.size, sizeof(int)); |
124 | | - for (i = 0; i < list.size; i++) |
125 | | - (*values)[i] = (int)strtod(list.elems[i].data, NULL); |
| 145 | + |
| 146 | + *values = (int *)calloc(list.size, sizeof(int)); |
126 | 147 | *out_num_values = (unsigned)list.size; |
| 148 | + |
| 149 | + for (i = 0; i < list.size; i++) |
| 150 | + (*values)[i] = (int)strtol(list.elems[i].data, NULL, 10); |
| 151 | + |
127 | 152 | string_list_deinitialize(&list); |
128 | 153 | free(str); |
129 | | - return true; |
| 154 | + return 1; |
130 | 155 | } |
131 | 156 |
|
132 | | - *values = (int*)calloc(num_default_values, sizeof(int)); |
| 157 | + *values = (int *)calloc(num_default_values, sizeof(int)); |
133 | 158 | memcpy(*values, default_values, sizeof(int) * num_default_values); |
134 | 159 | *out_num_values = num_default_values; |
135 | | - return false; |
| 160 | + return 0; |
136 | 161 | } |
137 | 162 |
|
138 | 163 | int config_userdata_get_string(void *userdata, const char *key_str, |
139 | 164 | char **output, const char *default_output) |
140 | 165 | { |
141 | | - char key[2][256]; |
| 166 | + char key0[CFG_KEY_SIZE]; |
| 167 | + char key1[CFG_KEY_SIZE]; |
142 | 168 | char *str = NULL; |
143 | | - struct config_file_userdata *usr = (struct config_file_userdata*)userdata; |
144 | | - fill_pathname_join_delim(key[0], usr->prefix[0], key_str, '_', sizeof(key[0])); |
145 | | - fill_pathname_join_delim(key[1], usr->prefix[1], key_str, '_', sizeof(key[1])); |
| 169 | + const struct config_file_userdata *usr = |
| 170 | + (const struct config_file_userdata *)userdata; |
| 171 | + |
| 172 | + fill_pathname_join_delim(key0, usr->prefix[0], key_str, '_', sizeof(key0)); |
| 173 | + fill_pathname_join_delim(key1, usr->prefix[1], key_str, '_', sizeof(key1)); |
146 | 174 |
|
147 | | - if ( config_get_string(usr->conf, key[0], &str) |
148 | | - || config_get_string(usr->conf, key[1], &str)) |
| 175 | + if ( config_get_string(usr->conf, key0, &str) |
| 176 | + || config_get_string(usr->conf, key1, &str)) |
149 | 177 | { |
150 | 178 | *output = str; |
151 | | - return true; |
| 179 | + return 1; |
152 | 180 | } |
153 | 181 |
|
154 | 182 | *output = strdup(default_output); |
155 | | - return false; |
| 183 | + return 0; |
156 | 184 | } |
157 | 185 |
|
158 | 186 | void config_userdata_free(void *ptr) |
|
0 commit comments