2015-01-07 18:31:19 +01:00
|
|
|
/* Copyright (C) 2010-2015 The RetroArch team
|
2010-12-30 01:50:37 +01:00
|
|
|
*
|
2014-10-22 03:35:04 +02:00
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
* The following license statement only applies to this file (config_file.h).
|
|
|
|
* ---------------------------------------------------------------------------------------
|
2010-12-30 01:50:37 +01:00
|
|
|
*
|
2014-10-22 03:35:04 +02:00
|
|
|
* 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.
|
2010-12-30 01:50:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-10-22 03:26:31 +02:00
|
|
|
#ifndef __LIBRETRO_SDK_CONFIG_FILE_H
|
|
|
|
#define __LIBRETRO_SDK_CONFIG_FILE_H
|
2010-12-29 14:27:44 +01:00
|
|
|
|
2012-11-04 22:55:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2010-12-29 14:27:44 +01:00
|
|
|
#include <stdint.h>
|
2014-10-21 05:05:52 +02:00
|
|
|
#include <boolean.h>
|
2011-01-09 15:50:30 +01:00
|
|
|
#include <stdio.h>
|
2011-08-25 16:15:34 +02:00
|
|
|
#include <stddef.h>
|
2010-12-29 14:27:44 +01:00
|
|
|
|
2014-10-22 03:22:08 +02:00
|
|
|
struct config_entry_list
|
|
|
|
{
|
|
|
|
/* If we got this from an #include,
|
|
|
|
* do not allow overwrite. */
|
|
|
|
bool readonly;
|
|
|
|
char *key;
|
|
|
|
char *value;
|
2015-06-14 09:25:00 -03:00
|
|
|
uint32_t key_hash;
|
|
|
|
|
2014-10-22 03:22:08 +02:00
|
|
|
struct config_entry_list *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct config_include_list
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct config_include_list *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct config_file
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct config_entry_list *entries;
|
|
|
|
struct config_entry_list *tail;
|
|
|
|
unsigned include_depth;
|
|
|
|
|
|
|
|
struct config_include_list *includes;
|
|
|
|
};
|
|
|
|
|
2010-12-29 14:27:44 +01:00
|
|
|
typedef struct config_file config_file_t;
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Loads a config file. Returns NULL if file doesn't exist.
|
|
|
|
* NULL path will create an empty config file. */
|
2010-12-29 14:27:44 +01:00
|
|
|
config_file_t *config_file_new(const char *path);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Load a config file from a string. */
|
2013-09-04 21:57:13 +01:00
|
|
|
config_file_t *config_file_new_from_string(const char *from_string);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Frees config file. */
|
2010-12-29 14:27:44 +01:00
|
|
|
void config_file_free(config_file_t *conf);
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Loads a new config, and appends its data to conf.
|
|
|
|
* The key-value pairs of the new config file takes priority over the old. */
|
2012-09-11 00:10:44 +02:00
|
|
|
bool config_append_file(config_file_t *conf, const char *path);
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* All extract functions return true when value is valid and exists.
|
|
|
|
* Returns false otherwise. */
|
2011-10-17 19:11:31 +02:00
|
|
|
|
|
|
|
bool config_entry_exists(config_file_t *conf, const char *entry);
|
2010-12-29 17:52:53 +01:00
|
|
|
|
2012-11-23 22:46:21 +01:00
|
|
|
struct config_entry_list;
|
|
|
|
struct config_file_entry
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
const char *value;
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Used intentionally. Opaque here. */
|
|
|
|
const struct config_entry_list *next;
|
2012-11-23 22:46:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool config_get_entry_list_head(config_file_t *conf, struct config_file_entry *entry);
|
|
|
|
bool config_get_entry_list_next(struct config_file_entry *entry);
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Extracts a double from config file. */
|
2010-12-29 14:27:44 +01:00
|
|
|
bool config_get_double(config_file_t *conf, const char *entry, double *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts a float from config file. */
|
2012-01-13 00:08:55 +01:00
|
|
|
bool config_get_float(config_file_t *conf, const char *entry, float *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts an int from config file. */
|
2010-12-29 14:27:44 +01:00
|
|
|
bool config_get_int(config_file_t *conf, const char *entry, int *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts an uint from config file. */
|
2012-01-11 01:04:17 +01:00
|
|
|
bool config_get_uint(config_file_t *conf, const char *entry, unsigned *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts an uint64 from config file. */
|
2012-03-04 13:55:35 +01:00
|
|
|
bool config_get_uint64(config_file_t *conf, const char *entry, uint64_t *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts an unsigned int from config file treating input as hex. */
|
2011-05-27 02:25:26 +02:00
|
|
|
bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts a single char. If value consists of several chars,
|
|
|
|
* this is an error. */
|
2010-12-29 14:27:44 +01:00
|
|
|
bool config_get_char(config_file_t *conf, const char *entry, char *in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts an allocated string in *in. This must be free()-d if
|
|
|
|
* this function succeeds. */
|
2010-12-29 14:27:44 +01:00
|
|
|
bool config_get_string(config_file_t *conf, const char *entry, char **in);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts a string to a preallocated buffer. Avoid memory allocation. */
|
2011-08-25 16:15:34 +02:00
|
|
|
bool config_get_array(config_file_t *conf, const char *entry, char *in, size_t size);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* Extracts a string to a preallocated buffer. Avoid memory allocation.
|
|
|
|
* Recognized magic like ~/. Similar to config_get_array() otherwise. */
|
2012-09-08 00:31:30 +02:00
|
|
|
bool config_get_path(config_file_t *conf, const char *entry, char *in, size_t size);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
|
|
|
/* 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. */
|
2010-12-29 14:27:44 +01:00
|
|
|
bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Setters. Similar to the getters.
|
|
|
|
* Will not write to entry if the entry was obtained from an #include. */
|
2011-01-09 02:58:26 +01:00
|
|
|
void config_set_double(config_file_t *conf, const char *entry, double value);
|
2012-01-30 15:14:30 +01:00
|
|
|
void config_set_float(config_file_t *conf, const char *entry, float value);
|
2011-01-09 02:58:26 +01:00
|
|
|
void config_set_int(config_file_t *conf, const char *entry, int val);
|
2013-04-06 11:30:56 +02:00
|
|
|
void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
|
2012-03-04 13:55:35 +01:00
|
|
|
void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
|
2011-01-09 02:58:26 +01:00
|
|
|
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);
|
2013-12-24 12:23:21 -05:00
|
|
|
void config_set_path(config_file_t *conf, const char *entry, const char *val);
|
2011-01-09 02:58:26 +01:00
|
|
|
void config_set_bool(config_file_t *conf, const char *entry, bool val);
|
2011-01-09 15:50:30 +01:00
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Write the current config to a file. */
|
2011-01-09 02:58:26 +01:00
|
|
|
bool config_file_write(config_file_t *conf, const char *path);
|
|
|
|
|
2014-09-02 19:20:03 +02:00
|
|
|
/* Dump the current config to an already opened file.
|
|
|
|
* Does not close the file. */
|
2011-01-09 15:50:30 +01:00
|
|
|
void config_file_dump(config_file_t *conf, FILE *file);
|
2014-09-02 19:20:03 +02:00
|
|
|
|
2012-11-04 22:55:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-12-29 14:27:44 +01:00
|
|
|
#endif
|
2011-10-17 19:11:31 +02:00
|
|
|
|