RetroArch
config_file.h
Go to the documentation of this file.
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 
24 #ifndef __LIBRETRO_SDK_CONFIG_FILE_H
25 #define __LIBRETRO_SDK_CONFIG_FILE_H
26 
27 #include <retro_common_api.h>
28 
30 
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stddef.h>
34 
35 #include <boolean.h>
36 
37 #define CONFIG_GET_BOOL_BASE(conf, base, var, key) do { \
38  bool tmp = false; \
39  if (config_get_bool(conf, key, &tmp)) \
40  base->var = tmp; \
41 } while(0)
42 
43 #define CONFIG_GET_INT_BASE(conf, base, var, key) do { \
44  int tmp = 0; \
45  if (config_get_int(conf, key, &tmp)) \
46  base->var = tmp; \
47 } while(0)
48 
49 #define CONFIG_GET_FLOAT_BASE(conf, base, var, key) do { \
50  float tmp = 0.0f; \
51  if (config_get_float(conf, key, &tmp)) \
52  base->var = tmp; \
53 } while(0)
54 
56 {
57  char *path;
61  unsigned include_depth;
63 
65 };
66 
67 
68 typedef struct config_file config_file_t;
69 
71 {
72  void (*config_file_new_entry_cb)(char*, char*);
73 };
74 
76 
77 /* Config file format
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.
82  *
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.
85  */
86 
87 /* Loads a config file. Returns NULL if file doesn't exist.
88  * NULL path will create an empty config file. */
89 config_file_t *config_file_new(const char *path);
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.*/
95 
96 /* Load a config file from a string. */
97 config_file_t *config_file_new_from_string(const char *from_string);
98 
99 /* Frees config file. */
100 void config_file_free(config_file_t *conf);
101 
102 /* Loads a new config, and appends its data to conf.
103  * The key-value pairs of the new config file takes priority over the old. */
104 bool config_append_file(config_file_t *conf, const char *path);
105 
106 /* All extract functions return true when value is valid and exists.
107  * Returns false otherwise. */
108 
109 bool config_entry_exists(config_file_t *conf, const char *entry);
110 
111 struct config_entry_list;
113 {
114  const char *key;
115  const char *value;
116  /* Used intentionally. Opaque here. */
117  const struct config_entry_list *next;
118 };
119 
122 
123 /* Extracts a double from config file. */
124 bool config_get_double(config_file_t *conf, const char *entry, double *in);
125 
126 /* Extracts a float from config file. */
127 bool config_get_float(config_file_t *conf, const char *entry, float *in);
128 
129 /* Extracts an int from config file. */
130 bool config_get_int(config_file_t *conf, const char *entry, int *in);
131 
132 /* Extracts an uint from config file. */
133 bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in);
134 
135 /* Extracts an size_t from config file. */
136 bool config_get_size_t(config_file_t *conf, const char *key, size_t *in);
137 
138 #if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
139 /* Extracts an uint64 from config file. */
140 bool config_get_uint64(config_file_t *conf, const char *entry, uint64_t *in);
141 #endif
142 
143 /* Extracts an unsigned int from config file treating input as hex. */
144 bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
145 
146 /* Extracts a single char. If value consists of several chars,
147  * this is an error. */
148 bool config_get_char(config_file_t *conf, const char *entry, char *in);
149 
150 /* Extracts an allocated string in *in. This must be free()-d if
151  * this function succeeds. */
152 bool config_get_string(config_file_t *conf, const char *entry, char **in);
153 
154 /* Extracts a string to a preallocated buffer. Avoid memory allocation. */
155 bool config_get_array(config_file_t *conf, const char *entry, char *s, size_t len);
156 
157 /* Extracts a string to a preallocated buffer. Avoid memory allocation.
158  * Recognized magic like ~/. Similar to config_get_array() otherwise. */
159 bool config_get_path(config_file_t *conf, const char *entry, char *s, size_t len);
160 
161 /* Extracts a string to a preallocated buffer. Avoid memory allocation. */
162 bool config_get_config_path(config_file_t *conf, char *s, size_t len);
163 
164 /* Extracts a boolean from config.
165  * Valid boolean true are "true" and "1". Valid false are "false" and "0".
166  * Other values will be treated as an error. */
167 bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
168 
169 /* Setters. Similar to the getters.
170  * Will not write to entry if the entry was obtained from an #include. */
171 void config_set_double(config_file_t *conf, const char *entry, double value);
172 void config_set_float(config_file_t *conf, const char *entry, float value);
173 void config_set_int(config_file_t *conf, const char *entry, int val);
174 void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
175 void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
176 void config_set_char(config_file_t *conf, const char *entry, char val);
177 void config_set_string(config_file_t *conf, const char *entry, const char *val);
178 void config_unset(config_file_t *conf, const char *key);
179 void config_set_path(config_file_t *conf, const char *entry, const char *val);
180 void config_set_bool(config_file_t *conf, const char *entry, bool val);
181 void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
182 
183 /* Write the current config to a file. */
184 bool config_file_write(config_file_t *conf, const char *path);
185 
186 /* Dump the current config to an already opened file.
187  * Does not close the file. */
189 
190 bool config_file_exists(const char *path);
191 
193 
194 #endif
195 
bool guaranteed_no_duplicates
Definition: config_file.h:62
bool config_get_double(config_file_t *conf, const char *entry, double *in)
Definition: config_file.c:639
GLuint GLfloat * val
Definition: glext.h:7847
bool config_get_bool(config_file_t *conf, const char *entry, bool *in)
Definition: config_file.c:824
void config_file_dump(config_file_t *conf, FILE *file)
Definition: config_file.c:1006
#define RETRO_BEGIN_DECLS
Definition: retro_common_api.h:41
bool config_get_float(config_file_t *conf, const char *entry, float *in)
Definition: config_file.c:652
bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in)
Definition: config_file.c:741
GLsizei const GLchar ** path
Definition: glext.h:7901
GLenum GLsizei len
Definition: glext.h:7389
bool config_get_char(config_file_t *conf, const char *entry, char *in)
Definition: config_file.c:760
bool config_get_array(config_file_t *conf, const char *entry, char *s, size_t len)
Definition: config_file.c:796
void config_set_char(config_file_t *conf, const char *entry, char val)
Definition: config_file.c:965
char * key
Definition: config_file.c:57
const char * key
Definition: config_file.h:114
bool config_entry_exists(config_file_t *conf, const char *entry)
Definition: config_file.c:1028
bool config_get_string(config_file_t *conf, const char *entry, char **in)
Definition: config_file.c:776
bool config_append_file(config_file_t *conf, const char *path)
Definition: config_file.c:530
GLdouble s
Definition: glext.h:6390
const char * value
Definition: config_file.h:115
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
bool config_get_entry_list_head(config_file_t *conf, struct config_file_entry *entry)
Definition: config_file.c:1042
void(* config_file_new_entry_cb)(char *, char *)
Definition: config_file.h:72
struct config_entry_list * entries
Definition: config_file.h:58
void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val)
Definition: config_file.c:956
bool config_get_entry_list_next(struct config_file_entry *entry)
Definition: config_file.c:1056
config_file_t * config_file_new_from_string(const char *from_string)
Definition: config_file.c:548
void config_set_path(config_file_t *conf, const char *entry, const char *val)
Definition: config_file.c:892
Definition: config_file.c:51
Definition: config_file.h:70
bool config_get_size_t(config_file_t *conf, const char *key, size_t *in)
Definition: config_file.c:684
GLuint in
Definition: glext.h:10523
void config_set_hex(config_file_t *conf, const char *entry, unsigned val)
Definition: config_file.c:947
bool config_get_path(config_file_t *conf, const char *entry, char *s, size_t len)
Definition: config_file.c:806
#define RETRO_END_DECLS
Definition: retro_common_api.h:42
config_file_t * config_file_new(const char *path)
Definition: config_file.c:614
struct config_entry_list * tail
Definition: config_file.h:59
#define FILE
Definition: file_stream_transforms.h:35
Definition: config_file.h:55
struct config_include_list * includes
Definition: config_file.h:64
void config_unset(config_file_t *conf, const char *key)
Definition: config_file.c:878
struct config_entry_list * last
Definition: config_file.h:60
const struct config_entry_list * next
Definition: config_file.h:117
void config_set_double(config_file_t *conf, const char *entry, double value)
Definition: config_file.c:905
bool config_file_exists(const char *path)
Definition: config_file.c:1069
bool config_file_write(config_file_t *conf, const char *path)
Definition: config_file.c:979
Definition: config_file.c:62
Definition: config_file.h:112
void config_set_bool(config_file_t *conf, const char *entry, bool val)
Definition: config_file.c:974
unsigned include_depth
Definition: config_file.h:61
void config_set_string(config_file_t *conf, const char *entry, const char *val)
Definition: config_file.c:845
bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in)
Definition: config_file.c:722
GLsizei const GLfloat * value
Definition: glext.h:6709
void config_set_uint(config_file_t *conf, const char *key, unsigned int val)
Definition: config_file.c:938
config_file_t * config_file_new_with_callback(const char *path, config_file_cb_t *cb)
Definition: config_file.c:610
void config_file_free(config_file_t *conf)
Definition: config_file.c:489
Definition: civetweb.c:1024
void config_set_int(config_file_t *conf, const char *entry, int val)
Definition: config_file.c:929
void config_set_float(config_file_t *conf, const char *entry, float value)
Definition: config_file.c:920
unsigned __int64 uint64_t
Definition: stdint.h:136
bool config_get_int(config_file_t *conf, const char *entry, int *in)
Definition: config_file.c:665
char * path
Definition: config_file.h:57
bool config_get_config_path(config_file_t *conf, char *s, size_t len)
Definition: config_file.c:788