1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (config_file.h).
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 #ifndef __LIBRETRO_SDK_CONFIG_FILE_H
24 #define __LIBRETRO_SDK_CONFIG_FILE_H
26 #include <retro_common_api.h>
36 #define CONFIG_GET_BOOL_BASE(conf, base, var, key) do { \
38 if (config_get_bool(conf, key, &tmp)) \
42 #define CONFIG_GET_INT_BASE(conf, base, var, key) do { \
44 if (config_get_int(conf, key, &tmp)) \
48 #define CONFIG_GET_FLOAT_BASE(conf, base, var, key) do { \
50 if (config_get_float(conf, key, &tmp)) \
57 struct config_entry_list **entries_map;
58 struct config_entry_list *entries;
59 struct config_entry_list *tail;
60 struct config_entry_list *last;
61 struct config_include_list *includes;
62 struct path_linked_list *references;
63 unsigned include_depth;
64 bool guaranteed_no_duplicates;
68 typedef struct config_file config_file_t;
72 void (*config_file_new_entry_cb)(char*, char*);
75 typedef struct config_file_cb config_file_cb_t ;
78 * - # are treated as comments. Rest of the line is ignored.
79 * - Format is: key = value. There can be as many spaces as you like in-between.
80 * - Value can be wrapped inside "" for multiword strings. (foo = "hai u")
81 * - #include includes a config file in-place.
83 * Path is relative to where config file was loaded unless an absolute path is chosen.
84 * Key/value pairs from an #include are read-only, and cannot be modified.
90 * Loads a config file.
91 * If @path is NULL, will create an empty config file.
93 * @return Returns NULL if file doesn't exist.
95 config_file_t *config_file_new(const char *path);
97 config_file_t *config_file_new_alloc(void);
100 * config_file_initialize:
104 void config_file_initialize(struct config_file *conf);
107 * config_file_new_with_callback:
109 * Loads a config file.
110 * If @path is NULL, will create an empty config file.
111 * Includes cb callbacks to run custom code during config file processing.
113 * @return Returns NULL if file doesn't exist.
115 config_file_t *config_file_new_with_callback(
116 const char *path, config_file_cb_t *cb);
119 * config_file_new_from_string:
121 * Load a config file from a string.
123 * NOTE: This will modify @from_string.
124 * Pass a copy of source string if original
125 * contents must be preserved
127 config_file_t *config_file_new_from_string(char *from_string,
130 config_file_t *config_file_new_from_path_to_string(const char *path);
137 void config_file_free(config_file_t *conf);
139 void config_file_add_reference(config_file_t *conf, char *path);
141 bool config_file_deinitialize(config_file_t *conf);
144 * config_append_file:
146 * Loads a new config, and appends its data to @conf.
147 * The key-value pairs of the new config file takes priority over the old.
149 bool config_append_file(config_file_t *conf, const char *path);
151 /* All extract functions return true when value is valid and exists.
152 * Returns false otherwise. */
154 struct config_entry_list
158 struct config_entry_list *next;
159 /* If we got this from an #include,
160 * do not allow overwrite. */
164 struct config_file_entry
168 /* Used intentionally. Opaque here. */
169 const struct config_entry_list *next;
172 struct config_entry_list *config_get_entry(
173 const config_file_t *conf, const char *key);
176 * config_get_entry_list_head:
180 bool config_get_entry_list_head(config_file_t *conf,
181 struct config_file_entry *entry);
184 * config_get_entry_list_next:
188 bool config_get_entry_list_next(struct config_file_entry *entry);
193 * Extracts a double from config file.
195 * Hidden non-leaf function cost:
196 * - Calls config_get_entry()
199 * @return True if double found, otherwise false.
201 bool config_get_double(config_file_t *conf, const char *entry, double *in);
206 * Extracts a float from config file.
208 * Hidden non-leaf function cost:
209 * - Calls config_get_entry()
212 * @return true if found, otherwise false.
214 bool config_get_float(config_file_t *conf, const char *entry, float *in);
216 /* Extracts an int from config file. */
217 bool config_get_int(config_file_t *conf, const char *entry, int *in);
219 /* Extracts an uint from config file. */
220 bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in);
222 /* Extracts an size_t from config file. */
223 bool config_get_size_t(config_file_t *conf, const char *key, size_t *in);
225 #if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
226 /* Extracts an uint64 from config file. */
227 bool config_get_uint64(config_file_t *conf, const char *entry, uint64_t *in);
230 /* Extracts an unsigned int from config file treating input as hex. */
231 bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
236 * Extracts a single char from config file.
237 * If value consists of several chars, this is an error.
239 * Hidden non-leaf function cost:
240 * - Calls config_get_entry()
242 * @return true if found, otherwise false.
244 bool config_get_char(config_file_t *conf, const char *entry, char *in);
249 * Extracts an allocated string in *in. This must be free()-d if
250 * this function succeeds.
252 * Hidden non-leaf function cost:
253 * - Calls config_get_entry()
256 * @return true if found, otherwise false.
258 bool config_get_string(config_file_t *conf, const char *entry, char **in);
260 /* Extracts a string to a preallocated buffer. Avoid memory allocation. */
261 bool config_get_array(config_file_t *conf, const char *entry, char *s, size_t len);
264 * config_get_config_path:
266 * Extracts a string to a preallocated buffer.
267 * Avoid memory allocation.
269 * Hidden non-leaf function cost:
272 bool config_get_config_path(config_file_t *conf, char *s, size_t len);
274 /* Extracts a string to a preallocated buffer. Avoid memory allocation.
275 * Recognized magic like ~/. Similar to config_get_array() otherwise. */
276 bool config_get_path(config_file_t *conf, const char *entry, char *s, size_t len);
281 * Extracts a boolean from config.
282 * Valid boolean true are "true" and "1". Valid false are "false" and "0".
283 * Other values will be treated as an error.
285 * Hidden non-leaf function cost:
286 * - Calls string_is_equal() x times
288 * @return true if preconditions are true, otherwise false.
290 bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
292 /* Setters. Similar to the getters.
293 * Will not write to entry if the entry was obtained from an #include. */
294 void config_set_double(config_file_t *conf, const char *entry, double value);
295 void config_set_float(config_file_t *conf, const char *entry, float value);
296 void config_set_int(config_file_t *conf, const char *entry, int val);
297 void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
298 void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
299 void config_set_char(config_file_t *conf, const char *entry, char val);
300 void config_set_string(config_file_t *conf, const char *entry, const char *val);
301 void config_unset(config_file_t *conf, const char *key);
302 void config_set_path(config_file_t *conf, const char *entry, const char *val);
307 * TODO/FIXME - could be turned into a trivial macro or removed
309 void config_set_bool(config_file_t *conf, const char *entry, bool val);
311 void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
316 * Write the current config to a file.
318 bool config_file_write(config_file_t *conf, const char *path, bool val);
323 * Dump the current config to an already opened file.
324 * Does not close the file.
326 void config_file_dump(config_file_t *conf, FILE *file, bool val);