1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (config_file.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __LIBRETRO_SDK_CONFIG_FILE_H
#define __LIBRETRO_SDK_CONFIG_FILE_H
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <boolean.h>
#define CONFIG_GET_BOOL_BASE(conf, base, var, key) do { \
bool tmp = false; \
if (config_get_bool(conf, key, &tmp)) \
base->var = tmp; \
} while(0)
#define CONFIG_GET_INT_BASE(conf, base, var, key) do { \
int tmp = 0; \
if (config_get_int(conf, key, &tmp)) \
base->var = tmp; \
} while(0)
#define CONFIG_GET_FLOAT_BASE(conf, base, var, key) do { \
float tmp = 0.0f; \
if (config_get_float(conf, key, &tmp)) \
base->var = tmp; \
} while(0)
struct config_file
{
char *path;
struct config_entry_list **entries_map;
struct config_entry_list *entries;
struct config_entry_list *tail;
struct config_entry_list *last;
struct config_include_list *includes;
struct path_linked_list *references;
unsigned include_depth;
bool guaranteed_no_duplicates;
bool modified;
};
typedef struct config_file config_file_t;
struct config_file_cb
{
void (*config_file_new_entry_cb)(char*, char*);
};
typedef struct config_file_cb config_file_cb_t ;
/* Config file format
* - # are treated as comments. Rest of the line is ignored.
* - Format is: key = value. There can be as many spaces as you like in-between.
* - Value can be wrapped inside "" for multiword strings. (foo = "hai u")
* - #include includes a config file in-place.
*
* Path is relative to where config file was loaded unless an absolute path is chosen.
* Key/value pairs from an #include are read-only, and cannot be modified.
*/
/**
* config_file_new:
*
* Loads a config file.
* If @path is NULL, will create an empty config file.
*
* @return Returns NULL if file doesn't exist.
**/
config_file_t *config_file_new(const char *path);
config_file_t *config_file_new_alloc(void);
/**
* config_file_initialize:
*
* Leaf function.
**/
void config_file_initialize(struct config_file *conf);
/**
* config_file_new_with_callback:
*
* Loads a config file.
* If @path is NULL, will create an empty config file.
* Includes cb callbacks to run custom code during config file processing.
*
* @return Returns NULL if file doesn't exist.
**/
config_file_t *config_file_new_with_callback(
const char *path, config_file_cb_t *cb);
/**
* config_file_new_from_string:
*
* Load a config file from a string.
*
* NOTE: This will modify @from_string.
* Pass a copy of source string if original
* contents must be preserved
**/
config_file_t *config_file_new_from_string(char *from_string,
const char *path);
config_file_t *config_file_new_from_path_to_string(const char *path);
/**
* config_file_free:
*
* Frees config file.
**/
void config_file_free(config_file_t *conf);
void config_file_add_reference(config_file_t *conf, char *path);
bool config_file_deinitialize(config_file_t *conf);
/**
* config_append_file:
*
* Loads a new config, and appends its data to @conf.
* The key-value pairs of the new config file takes priority over the old.
**/
bool config_append_file(config_file_t *conf, const char *path);
/* All extract functions return true when value is valid and exists.
* Returns false otherwise. */
struct config_entry_list
{
char *key;
char *value;
struct config_entry_list *next;
/* If we got this from an #include,
* do not allow overwrite. */
bool readonly;
};
struct config_file_entry
{
const char *key;
const char *value;
/* Used intentionally. Opaque here. */
const struct config_entry_list *next;
};
struct config_entry_list *config_get_entry(
const config_file_t *conf, const char *key);
/**
* config_get_entry_list_head:
*
* Leaf function.
**/
bool config_get_entry_list_head(config_file_t *conf,
struct config_file_entry *entry);
/**
* config_get_entry_list_next:
*
* Leaf function.
**/
bool config_get_entry_list_next(struct config_file_entry *entry);
/**
* config_get_double:
*
* Extracts a double from config file.
*
* Hidden non-leaf function cost:
* - Calls config_get_entry()
* - Calls strtod
*
* @return True if double found, otherwise false.
**/
bool config_get_double(config_file_t *conf, const char *entry, double *in);
/**
* config_get_float:
*
* Extracts a float from config file.
*
* Hidden non-leaf function cost:
* - Calls config_get_entry()
* - Calls strtod
*
* @return true if found, otherwise false.
**/
bool config_get_float(config_file_t *conf, const char *entry, float *in);
/* Extracts an int from config file. */
bool config_get_int(config_file_t *conf, const char *entry, int *in);
/* Extracts an uint from config file. */
bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in);
/* Extracts an size_t from config file. */
bool config_get_size_t(config_file_t *conf, const char *key, size_t *in);
#if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
/* Extracts an uint64 from config file. */
bool config_get_uint64(config_file_t *conf, const char *entry, uint64_t *in);
#endif
/* Extracts an unsigned int from config file treating input as hex. */
bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
/**
* config_get_char:
*
* Extracts a single char from config file.
* If value consists of several chars, this is an error.
*
* Hidden non-leaf function cost:
* - Calls config_get_entry()
*
* @return true if found, otherwise false.
**/
bool config_get_char(config_file_t *conf, const char *entry, char *in);
/**
* config_get_string:
*
* Extracts an allocated string in *in. This must be free()-d if
* this function succeeds.
*
* Hidden non-leaf function cost:
* - Calls config_get_entry()
* - Calls strdup
*
* @return true if found, otherwise false.
**/
bool config_get_string(config_file_t *conf, const char *entry, char **in);
/* Extracts a string to a preallocated buffer. Avoid memory allocation. */
bool config_get_array(config_file_t *conf, const char *entry, char *s, size_t len);
/**
* config_get_config_path:
*
* Extracts a string to a preallocated buffer.
* Avoid memory allocation.
*
* Hidden non-leaf function cost:
* - Calls strlcpy
**/
bool config_get_config_path(config_file_t *conf, char *s, size_t len);
/* Extracts a string to a preallocated buffer. Avoid memory allocation.
* Recognized magic like ~/. Similar to config_get_array() otherwise. */
bool config_get_path(config_file_t *conf, const char *entry, char *s, size_t len);
/**
* config_get_bool:
*
* Extracts a boolean from config.
* Valid boolean true are "true" and "1". Valid false are "false" and "0".
* Other values will be treated as an error.
*
* Hidden non-leaf function cost:
* - Calls string_is_equal() x times
*
* @return true if preconditions are true, otherwise false.
**/
bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
/* Setters. Similar to the getters.
* Will not write to entry if the entry was obtained from an #include. */
void config_set_double(config_file_t *conf, const char *entry, double value);
void config_set_float(config_file_t *conf, const char *entry, float value);
void config_set_int(config_file_t *conf, const char *entry, int val);
void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
void config_set_char(config_file_t *conf, const char *entry, char val);
void config_set_string(config_file_t *conf, const char *entry, const char *val);
void config_unset(config_file_t *conf, const char *key);
void config_set_path(config_file_t *conf, const char *entry, const char *val);
/**
* config_set_bool:
* TODO/FIXME - could be turned into a trivial macro or removed
**/
void config_set_bool(config_file_t *conf, const char *entry, bool val);
void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
/**
* config_file_write:
*
* Write the current config to a file.
**/
bool config_file_write(config_file_t *conf, const char *path, bool val);
/**
* config_file_dump:
*
* Dump the current config to an already opened file.
* Does not close the file.
**/
void config_file_dump(config_file_t *conf, FILE *file, bool val);
RETRO_END_DECLS
#endif
|