1 /* Copyright  (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (config_file.h).
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 #ifndef __LIBRETRO_SDK_CONFIG_FILE_H
24 #define __LIBRETRO_SDK_CONFIG_FILE_H
25 
26 #include <retro_common_api.h>
27 
28 RETRO_BEGIN_DECLS
29 
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <stddef.h>
33 
34 #include <boolean.h>
35 
36 #define CONFIG_GET_BOOL_BASE(conf, base, var, key) do { \
37    bool tmp = false; \
38    if (config_get_bool(conf, key, &tmp)) \
39       base->var = tmp; \
40 } while(0)
41 
42 #define CONFIG_GET_INT_BASE(conf, base, var, key) do { \
43    int tmp = 0; \
44    if (config_get_int(conf, key, &tmp)) \
45       base->var = tmp; \
46 } while(0)
47 
48 #define CONFIG_GET_FLOAT_BASE(conf, base, var, key) do { \
49    float tmp = 0.0f; \
50    if (config_get_float(conf, key, &tmp)) \
51       base->var = tmp; \
52 } while(0)
53 
54 struct config_file
55 {
56    char *path;
57    struct config_entry_list *entries;
58    struct config_entry_list *tail;
59    struct config_entry_list *last;
60    unsigned include_depth;
61    bool guaranteed_no_duplicates;
62 
63    struct config_include_list *includes;
64 };
65 
66 typedef struct config_file config_file_t;
67 
68 struct config_file_cb
69 {
70    void (*config_file_new_entry_cb)(char*, char*);
71 };
72 
73 typedef struct config_file_cb config_file_cb_t ;
74 
75 /* Config file format
76  * - # are treated as comments. Rest of the line is ignored.
77  * - Format is: key = value. There can be as many spaces as you like in-between.
78  * - Value can be wrapped inside "" for multiword strings. (foo = "hai u")
79  * - #include includes a config file in-place.
80  *
81  * Path is relative to where config file was loaded unless an absolute path is chosen.
82  * Key/value pairs from an #include are read-only, and cannot be modified.
83  */
84 
85 /* Loads a config file. Returns NULL if file doesn't exist.
86  * NULL path will create an empty config file. */
87 config_file_t *config_file_new(const char *path);
88 
89 config_file_t *config_file_new_alloc(void);
90 
91 /* Loads a config file. Returns NULL if file doesn't exist.
92  * NULL path will create an empty config file.
93  * Includes cb callbacks to run custom code during config file processing.*/
94 config_file_t *config_file_new_with_callback(const char *path, config_file_cb_t *cb);
95 
96 /* Load a config file from a string. */
97 config_file_t *config_file_new_from_string(const char *from_string,
98       const char *path);
99 
100 config_file_t *config_file_new_from_path_to_string(const char *path);
101 
102 /* Frees config file. */
103 void config_file_free(config_file_t *conf);
104 
105 /* Loads a new config, and appends its data to conf.
106  * The key-value pairs of the new config file takes priority over the old. */
107 bool config_append_file(config_file_t *conf, const char *path);
108 
109 /* All extract functions return true when value is valid and exists.
110  * Returns false otherwise. */
111 
112 bool config_entry_exists(config_file_t *conf, const char *entry);
113 
114 struct config_entry_list;
115 struct config_file_entry
116 {
117    const char *key;
118    const char *value;
119    /* Used intentionally. Opaque here. */
120    const struct config_entry_list *next;
121 };
122 
123 bool config_get_entry_list_head(config_file_t *conf, struct config_file_entry *entry);
124 bool config_get_entry_list_next(struct config_file_entry *entry);
125 
126 /* Extracts a double from config file. */
127 bool config_get_double(config_file_t *conf, const char *entry, double *in);
128 
129 /* Extracts a float from config file. */
130 bool config_get_float(config_file_t *conf, const char *entry, float *in);
131 
132 /* Extracts an int from config file. */
133 bool config_get_int(config_file_t *conf, const char *entry, int *in);
134 
135 /* Extracts an uint from config file. */
136 bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in);
137 
138 /* Extracts an size_t from config file. */
139 bool config_get_size_t(config_file_t *conf, const char *key, size_t *in);
140 
141 #if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
142 /* Extracts an uint64 from config file. */
143 bool config_get_uint64(config_file_t *conf, const char *entry, uint64_t *in);
144 #endif
145 
146 /* Extracts an unsigned int from config file treating input as hex. */
147 bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
148 
149 /* Extracts a single char. If value consists of several chars,
150  * this is an error. */
151 bool config_get_char(config_file_t *conf, const char *entry, char *in);
152 
153 /* Extracts an allocated string in *in. This must be free()-d if
154  * this function succeeds. */
155 bool config_get_string(config_file_t *conf, const char *entry, char **in);
156 
157 /* Extracts a string to a preallocated buffer. Avoid memory allocation. */
158 bool config_get_array(config_file_t *conf, const char *entry, char *s, size_t len);
159 
160 /* Extracts a string to a preallocated buffer. Avoid memory allocation.
161  * Recognized magic like ~/. Similar to config_get_array() otherwise. */
162 bool config_get_path(config_file_t *conf, const char *entry, char *s, size_t len);
163 
164 /* Extracts a string to a preallocated buffer. Avoid memory allocation. */
165 bool config_get_config_path(config_file_t *conf, char *s, size_t len);
166 
167 /* Extracts a boolean from config.
168  * Valid boolean true are "true" and "1". Valid false are "false" and "0".
169  * Other values will be treated as an error. */
170 bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
171 
172 /* Setters. Similar to the getters.
173  * Will not write to entry if the entry was obtained from an #include. */
174 void config_set_double(config_file_t *conf, const char *entry, double value);
175 void config_set_float(config_file_t *conf, const char *entry, float value);
176 void config_set_int(config_file_t *conf, const char *entry, int val);
177 void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
178 void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
179 void config_set_char(config_file_t *conf, const char *entry, char val);
180 void config_set_string(config_file_t *conf, const char *entry, const char *val);
181 void config_unset(config_file_t *conf, const char *key);
182 void config_set_path(config_file_t *conf, const char *entry, const char *val);
183 void config_set_bool(config_file_t *conf, const char *entry, bool val);
184 void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
185 
186 /* Write the current config to a file. */
187 bool config_file_write(config_file_t *conf, const char *path, bool val);
188 
189 /* Dump the current config to an already opened file.
190  * Does not close the file. */
191 void config_file_dump(config_file_t *conf, FILE *file, bool val);
192 
193 #ifdef ORBIS
194 void config_file_dump_orbis(config_file_t *conf, int fd);
195 #endif
196 
197 bool config_file_exists(const char *path);
198 
199 RETRO_END_DECLS
200 
201 #endif
202