2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-14 04:21:15 -07:00
|
|
|
#pragma once
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
#include "c99defs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic ini-style config file functions
|
2014-04-07 01:29:56 -07:00
|
|
|
*
|
|
|
|
* NOTE: It is highly recommended to use the default value functions (bottom of
|
|
|
|
* the file) before reading any variables from config files.
|
2013-09-30 19:37:13 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct config_data;
|
2014-09-25 17:44:05 -07:00
|
|
|
typedef struct config_data config_t;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
#define CONFIG_SUCCESS 0
|
2013-09-30 19:37:13 -07:00
|
|
|
#define CONFIG_FILENOTFOUND -1
|
2019-06-22 22:13:45 -07:00
|
|
|
#define CONFIG_ERROR -2
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-11-23 22:34:38 -08:00
|
|
|
enum config_open_type {
|
|
|
|
CONFIG_OPEN_EXISTING,
|
|
|
|
CONFIG_OPEN_ALWAYS,
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT config_t *config_create(const char *file);
|
|
|
|
EXPORT int config_open(config_t **config, const char *file,
|
2019-06-22 22:13:45 -07:00
|
|
|
enum config_open_type open_type);
|
2014-11-10 01:06:25 -08:00
|
|
|
EXPORT int config_open_string(config_t **config, const char *str);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT int config_save(config_t *config);
|
2015-08-21 17:42:24 -07:00
|
|
|
EXPORT int config_save_safe(config_t *config, const char *temp_ext,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *backup_ext);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_close(config_t *config);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT size_t config_num_sections(config_t *config);
|
|
|
|
EXPORT const char *config_get_section(config_t *config, size_t idx);
|
2013-12-12 20:42:44 -08:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_string(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, const char *value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_int(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, int64_t value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_uint(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, uint64_t value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_bool(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, bool value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_double(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, double value);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
EXPORT const char *config_get_string(config_t *config, const char *section,
|
|
|
|
const char *name);
|
2017-02-21 23:02:17 -08:00
|
|
|
EXPORT int64_t config_get_int(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name);
|
2017-02-21 23:02:17 -08:00
|
|
|
EXPORT uint64_t config_get_uint(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name);
|
2017-02-21 23:02:17 -08:00
|
|
|
EXPORT bool config_get_bool(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name);
|
2017-02-21 23:02:17 -08:00
|
|
|
EXPORT double config_get_double(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2015-07-03 09:48:49 -07:00
|
|
|
EXPORT bool config_remove_value(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name);
|
2015-07-03 09:48:49 -07:00
|
|
|
|
2014-04-07 01:29:56 -07:00
|
|
|
/*
|
|
|
|
* DEFAULT VALUES
|
|
|
|
*
|
|
|
|
* The following functions are used to set what values will return if they do
|
|
|
|
* not exist. Call these functions *once* for each known value before reading
|
|
|
|
* any of them anywhere else.
|
|
|
|
*
|
|
|
|
* These do *not* actually set any values, they only set what values will be
|
|
|
|
* returned for config_get_* if the specified variable does not exist.
|
|
|
|
*
|
2017-03-19 04:35:51 -07:00
|
|
|
* You can initialize the defaults programmatically using config_set_default_*
|
2014-04-07 01:29:56 -07:00
|
|
|
* functions (recommended for most cases), or you can initialize it via a file
|
|
|
|
* with config_open_defaults.
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT int config_open_defaults(config_t *config, const char *file);
|
2014-04-07 01:29:56 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_default_string(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, const char *value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_default_int(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, int64_t value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_default_uint(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, uint64_t value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_default_bool(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, bool value);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void config_set_default_double(config_t *config, const char *section,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *name, double value);
|
2014-04-07 01:29:56 -07:00
|
|
|
|
|
|
|
/* These functions allow you to get the current default values rather than get
|
|
|
|
* the actual values. Probably almost never really needed */
|
2017-02-21 23:02:17 -08:00
|
|
|
EXPORT const char *config_get_default_string(config_t *config,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *section,
|
|
|
|
const char *name);
|
|
|
|
EXPORT int64_t config_get_default_int(config_t *config, const char *section,
|
|
|
|
const char *name);
|
|
|
|
EXPORT uint64_t config_get_default_uint(config_t *config, const char *section,
|
|
|
|
const char *name);
|
|
|
|
EXPORT bool config_get_default_bool(config_t *config, const char *section,
|
|
|
|
const char *name);
|
|
|
|
EXPORT double config_get_default_double(config_t *config, const char *section,
|
|
|
|
const char *name);
|
|
|
|
|
|
|
|
EXPORT bool config_has_user_value(config_t *config, const char *section,
|
|
|
|
const char *name);
|
|
|
|
EXPORT bool config_has_default_value(config_t *config, const char *section,
|
|
|
|
const char *name);
|
2014-07-11 10:58:29 -07:00
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|