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
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include "config-file.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "base.h"
|
|
|
|
#include "bmem.h"
|
|
|
|
#include "darray.h"
|
|
|
|
#include "lexer.h"
|
|
|
|
#include "dstr.h"
|
|
|
|
|
|
|
|
struct config_item {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void config_item_free(struct config_item *item)
|
|
|
|
{
|
|
|
|
bfree(item->name);
|
|
|
|
bfree(item->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct config_section {
|
|
|
|
char *name;
|
|
|
|
struct darray items; /* struct config_item */
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void config_section_free(struct config_section *section)
|
|
|
|
{
|
|
|
|
struct config_item *items = section->items.array;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < section->items.num; i++)
|
|
|
|
config_item_free(items+i);
|
|
|
|
|
|
|
|
darray_free(§ion->items);
|
|
|
|
bfree(section->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct config_data {
|
|
|
|
char *file;
|
|
|
|
struct darray sections; /* struct config_section */
|
|
|
|
struct darray defaults; /* struct config_section */
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
config_t *config_create(const char *file)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
struct config_data *config;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = os_fopen(file, "wb");
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
fclose(f);
|
|
|
|
|
2014-02-09 11:34:07 -08:00
|
|
|
config = bzalloc(sizeof(struct config_data));
|
2013-09-30 19:37:13 -07:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void remove_ref_whitespace(struct strref *ref)
|
|
|
|
{
|
|
|
|
if (ref->array) {
|
|
|
|
while (is_whitespace(*ref->array)) {
|
|
|
|
ref->array++;
|
|
|
|
ref->len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ref->len && is_whitespace(ref->array[ref->len-1]))
|
|
|
|
ref->len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool config_parse_string(struct lexer *lex, struct strref *ref,
|
|
|
|
char end)
|
|
|
|
{
|
|
|
|
bool success = end != 0;
|
|
|
|
struct base_token token;
|
|
|
|
base_token_clear(&token);
|
|
|
|
|
2013-12-15 23:05:27 -08:00
|
|
|
while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
if (end) {
|
|
|
|
if (*token.text.array == end) {
|
|
|
|
success = true;
|
|
|
|
break;
|
|
|
|
} else if (is_newline(*token.text.array)) {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_newline(*token.text.array)) {
|
|
|
|
success = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strref_add(ref, &token.text);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_ref_whitespace(ref);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_add_item(struct darray *items, struct strref *name,
|
|
|
|
struct strref *value)
|
|
|
|
{
|
|
|
|
struct config_item item;
|
|
|
|
item.name = bstrdup_n(name->array, name->len);
|
|
|
|
item.value = bstrdup_n(value->array, value->len);
|
|
|
|
darray_push_back(sizeof(struct config_item), items, &item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_parse_section(struct config_section *section,
|
|
|
|
struct lexer *lex)
|
|
|
|
{
|
|
|
|
struct base_token token;
|
|
|
|
|
2013-12-15 23:05:27 -08:00
|
|
|
while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
struct strref name, value;
|
|
|
|
|
|
|
|
while (token.type == BASETOKEN_WHITESPACE) {
|
2013-12-15 23:05:27 -08:00
|
|
|
if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
|
2013-09-30 19:37:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.type == BASETOKEN_OTHER) {
|
|
|
|
if (*token.text.array == '#') {
|
|
|
|
do {
|
|
|
|
if (!lexer_getbasetoken(lex, &token,
|
2013-12-15 23:05:27 -08:00
|
|
|
PARSE_WHITESPACE))
|
2013-09-30 19:37:13 -07:00
|
|
|
return;
|
|
|
|
} while (!is_newline(*token.text.array));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (*token.text.array == '[') {
|
|
|
|
lex->offset--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strref_copy(&name, &token.text);
|
|
|
|
if (!config_parse_string(lex, &name, '='))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strref_clear(&value);
|
|
|
|
config_parse_string(lex, &value, 0);
|
|
|
|
|
|
|
|
config_add_item(§ion->items, &name, &value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:06:25 -08:00
|
|
|
static void parse_config_data(struct darray *sections, struct lexer *lex)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
struct strref section_name;
|
2014-11-10 01:06:25 -08:00
|
|
|
struct base_token token;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
base_token_clear(&token);
|
|
|
|
|
2014-11-10 01:06:25 -08:00
|
|
|
while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
|
2013-09-30 19:37:13 -07:00
|
|
|
struct config_section *section;
|
|
|
|
|
|
|
|
while (token.type == BASETOKEN_WHITESPACE) {
|
2014-11-10 01:06:25 -08:00
|
|
|
if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
|
|
|
|
return;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*token.text.array != '[') {
|
|
|
|
while (!is_newline(*token.text.array)) {
|
2014-11-10 01:06:25 -08:00
|
|
|
if (!lexer_getbasetoken(lex, &token,
|
2013-12-15 23:05:27 -08:00
|
|
|
PARSE_WHITESPACE))
|
2014-11-10 01:06:25 -08:00
|
|
|
return;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
strref_clear(§ion_name);
|
2014-11-10 01:06:25 -08:00
|
|
|
config_parse_string(lex, §ion_name, ']');
|
2013-09-30 19:37:13 -07:00
|
|
|
if (!section_name.len)
|
2014-11-10 01:06:25 -08:00
|
|
|
return;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
section = darray_push_back_new(sizeof(struct config_section),
|
|
|
|
sections);
|
|
|
|
section->name = bstrdup_n(section_name.array,
|
|
|
|
section_name.len);
|
2014-11-10 01:06:25 -08:00
|
|
|
config_parse_section(section, lex);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
2014-11-10 01:06:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_parse_file(struct darray *sections, const char *file,
|
|
|
|
bool always_open)
|
|
|
|
{
|
|
|
|
char *file_data;
|
|
|
|
struct lexer lex;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = os_fopen(file, "rb");
|
|
|
|
if (always_open && !f)
|
|
|
|
f = os_fopen(file, "w+");
|
|
|
|
if (!f)
|
|
|
|
return CONFIG_FILENOTFOUND;
|
|
|
|
|
|
|
|
os_fread_utf8(f, &file_data);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (!file_data)
|
|
|
|
return CONFIG_SUCCESS;
|
|
|
|
|
|
|
|
lexer_init(&lex);
|
|
|
|
lexer_start_move(&lex, file_data);
|
|
|
|
|
|
|
|
parse_config_data(sections, &lex);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
lexer_free(&lex);
|
|
|
|
return CONFIG_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int config_open(config_t **config, const char *file,
|
2013-11-23 22:34:38 -08:00
|
|
|
enum config_open_type open_type)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
int errorcode;
|
2013-11-23 22:34:38 -08:00
|
|
|
bool always_open = open_type == CONFIG_OPEN_ALWAYS;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (!config)
|
|
|
|
return CONFIG_ERROR;
|
|
|
|
|
2014-02-09 11:34:07 -08:00
|
|
|
*config = bzalloc(sizeof(struct config_data));
|
2014-04-14 13:55:14 -07:00
|
|
|
if (!*config)
|
|
|
|
return CONFIG_ERROR;
|
|
|
|
|
2013-12-17 17:16:36 -08:00
|
|
|
(*config)->file = bstrdup(file);
|
|
|
|
|
2014-11-10 01:06:25 -08:00
|
|
|
errorcode = config_parse_file(&(*config)->sections, file, always_open);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (errorcode != CONFIG_SUCCESS) {
|
|
|
|
config_close(*config);
|
|
|
|
*config = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorcode;
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:06:25 -08:00
|
|
|
int config_open_string(config_t **config, const char *str)
|
|
|
|
{
|
|
|
|
struct lexer lex;
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
return CONFIG_ERROR;
|
|
|
|
|
|
|
|
*config = bzalloc(sizeof(struct config_data));
|
|
|
|
if (!*config)
|
|
|
|
return CONFIG_ERROR;
|
|
|
|
|
|
|
|
(*config)->file = NULL;
|
|
|
|
|
|
|
|
lexer_init(&lex);
|
|
|
|
lexer_start(&lex, str);
|
|
|
|
parse_config_data(&(*config)->sections, &lex);
|
|
|
|
lexer_free(&lex);
|
|
|
|
|
|
|
|
return CONFIG_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int config_open_defaults(config_t *config, const char *file)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
if (!config)
|
|
|
|
return CONFIG_ERROR;
|
|
|
|
|
2014-11-10 01:06:25 -08:00
|
|
|
return config_parse_file(&config->defaults, file, false);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int config_save(config_t *config)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
struct dstr str;
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
return CONFIG_ERROR;
|
2014-11-10 01:06:25 -08:00
|
|
|
if (!config->file)
|
|
|
|
return CONFIG_ERROR;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
dstr_init(&str);
|
|
|
|
|
|
|
|
f = os_fopen(config->file, "wb");
|
|
|
|
if (!f)
|
|
|
|
return CONFIG_FILENOTFOUND;
|
|
|
|
|
|
|
|
for (i = 0; i < config->sections.num; i++) {
|
|
|
|
struct config_section *section = darray_item(
|
|
|
|
sizeof(struct config_section),
|
|
|
|
&config->sections, i);
|
|
|
|
|
|
|
|
if (i) dstr_cat(&str, "\n");
|
|
|
|
|
2013-12-17 17:16:36 -08:00
|
|
|
dstr_cat(&str, "[");
|
2013-09-30 19:37:13 -07:00
|
|
|
dstr_cat(&str, section->name);
|
|
|
|
dstr_cat(&str, "]\n");
|
|
|
|
|
|
|
|
for (j = 0; j < section->items.num; j++) {
|
|
|
|
struct config_item *item = darray_item(
|
|
|
|
sizeof(struct config_item),
|
2013-12-18 11:08:38 -08:00
|
|
|
§ion->items, j);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
dstr_cat(&str, item->name);
|
|
|
|
dstr_cat(&str, "=");
|
|
|
|
dstr_cat(&str, item->value);
|
|
|
|
dstr_cat(&str, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
fwrite("\xEF\xBB\xBF", 1, 3, f);
|
|
|
|
#endif
|
|
|
|
fwrite(str.array, 1, str.len, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
dstr_free(&str);
|
|
|
|
|
|
|
|
return CONFIG_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_close(config_t *config)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
struct config_section *defaults, *sections;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!config) return;
|
|
|
|
|
|
|
|
defaults = config->defaults.array;
|
|
|
|
sections = config->sections.array;
|
|
|
|
|
|
|
|
for (i = 0; i < config->defaults.num; i++)
|
|
|
|
config_section_free(defaults+i);
|
|
|
|
for (i = 0; i < config->sections.num; i++)
|
|
|
|
config_section_free(sections+i);
|
|
|
|
|
|
|
|
darray_free(&config->defaults);
|
|
|
|
darray_free(&config->sections);
|
|
|
|
bfree(config->file);
|
|
|
|
bfree(config);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
size_t config_num_sections(config_t *config)
|
2013-12-12 20:42:44 -08:00
|
|
|
{
|
|
|
|
return config->sections.num;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
const char *config_get_section(config_t *config, size_t idx)
|
2013-12-12 20:42:44 -08:00
|
|
|
{
|
|
|
|
struct config_section *section;
|
|
|
|
|
|
|
|
if (idx >= config->sections.num)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
section = darray_item(sizeof(struct config_section), &config->sections,
|
|
|
|
idx);
|
|
|
|
|
|
|
|
return section->name;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static const struct config_item *config_find_item(const struct darray *sections,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *section, const char *name)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < sections->num; i++) {
|
2014-09-26 15:25:59 -07:00
|
|
|
const struct config_section *sec = darray_item(
|
2013-09-30 19:37:13 -07:00
|
|
|
sizeof(struct config_section), sections, i);
|
|
|
|
|
2013-12-07 09:22:56 -08:00
|
|
|
if (astrcmpi(sec->name, section) == 0) {
|
2013-09-30 19:37:13 -07:00
|
|
|
for (j = 0; j < sec->items.num; j++) {
|
|
|
|
struct config_item *item = darray_item(
|
|
|
|
sizeof(struct config_item),
|
2013-12-07 09:22:56 -08:00
|
|
|
&sec->items, j);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (astrcmpi(item->name, name) == 0)
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_set_item(struct darray *sections, const char *section,
|
|
|
|
const char *name, char *value)
|
|
|
|
{
|
|
|
|
struct config_section *sec = NULL;
|
|
|
|
struct config_section *array = sections->array;
|
|
|
|
struct config_item *item;
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < sections->num; i++) {
|
|
|
|
struct config_section *cur_sec = array+i;
|
|
|
|
struct config_item *items = cur_sec->items.array;
|
|
|
|
|
|
|
|
if (astrcmpi(cur_sec->name, section) == 0) {
|
|
|
|
for (j = 0; j < cur_sec->items.num; j++) {
|
2013-12-07 09:22:56 -08:00
|
|
|
item = items+j;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
if (astrcmpi(item->name, name) == 0) {
|
|
|
|
bfree(item->value);
|
|
|
|
item->value = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sec = cur_sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sec) {
|
|
|
|
sec = darray_push_back_new(sizeof(struct config_section),
|
|
|
|
sections);
|
|
|
|
sec->name = bstrdup(section);
|
|
|
|
}
|
|
|
|
|
|
|
|
item = darray_push_back_new(sizeof(struct config_item), &sec->items);
|
|
|
|
item->name = bstrdup(name);
|
|
|
|
item->value = value;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_string(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
value = "";
|
|
|
|
config_set_item(&config->sections, section, name, bstrdup(value));
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_int(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, int64_t value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
2013-11-23 22:34:38 -08:00
|
|
|
dstr_printf(&str, "%lld", value);
|
2013-09-30 19:37:13 -07:00
|
|
|
config_set_item(&config->sections, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_uint(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, uint64_t value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
2013-11-23 22:34:38 -08:00
|
|
|
dstr_printf(&str, "%llu", value);
|
2013-09-30 19:37:13 -07:00
|
|
|
config_set_item(&config->sections, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_bool(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, bool value)
|
|
|
|
{
|
|
|
|
char *str = bstrdup(value ? "true" : "false");
|
|
|
|
config_set_item(&config->sections, section, name, str);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_double(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, double value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
|
|
|
dstr_printf(&str, "%g", value);
|
|
|
|
config_set_item(&config->sections, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_default_string(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, const char *value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
value = "";
|
|
|
|
config_set_item(&config->defaults, section, name, bstrdup(value));
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_default_int(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, int64_t value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
2013-11-23 22:34:38 -08:00
|
|
|
dstr_printf(&str, "%lld", value);
|
2013-09-30 19:37:13 -07:00
|
|
|
config_set_item(&config->defaults, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_default_uint(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, uint64_t value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
2013-11-23 22:34:38 -08:00
|
|
|
dstr_printf(&str, "%llu", value);
|
2013-09-30 19:37:13 -07:00
|
|
|
config_set_item(&config->defaults, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_default_bool(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, bool value)
|
|
|
|
{
|
|
|
|
char *str = bstrdup(value ? "true" : "false");
|
|
|
|
config_set_item(&config->defaults, section, name, str);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void config_set_default_double(config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name, double value)
|
|
|
|
{
|
|
|
|
struct dstr str;
|
|
|
|
dstr_init(&str);
|
|
|
|
dstr_printf(&str, "%g", value);
|
|
|
|
config_set_item(&config->defaults, section, name, str.array);
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
const char *config_get_string(const config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
2014-09-26 15:25:59 -07:00
|
|
|
const struct config_item *item = config_find_item(&config->sections,
|
2013-09-30 19:37:13 -07:00
|
|
|
section, name);
|
|
|
|
if (!item)
|
|
|
|
item = config_find_item(&config->defaults, section, name);
|
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return item->value;
|
|
|
|
}
|
|
|
|
|
2014-11-10 01:08:23 -08:00
|
|
|
static inline int64_t str_to_int64(const char *str)
|
|
|
|
{
|
|
|
|
if (!str || !*str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (str[0] == '0' && str[1] == 'x')
|
|
|
|
return strtoll(str + 2, NULL, 16);
|
|
|
|
else
|
|
|
|
return strtoll(str, NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t str_to_uint64(const char *str)
|
|
|
|
{
|
|
|
|
if (!str || !*str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (str[0] == '0' && str[1] == 'x')
|
|
|
|
return strtoull(str + 2, NULL, 16);
|
|
|
|
else
|
|
|
|
return strtoull(str, NULL, 10);
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
int64_t config_get_int(const config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_string(config, section, name);
|
|
|
|
if (value)
|
2014-11-10 01:08:23 -08:00
|
|
|
return str_to_int64(value);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
uint64_t config_get_uint(const config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_string(config, section, name);
|
|
|
|
if (value)
|
2014-11-10 01:08:23 -08:00
|
|
|
return str_to_uint64(value);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool config_get_bool(const config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_string(config, section, name);
|
|
|
|
if (value)
|
|
|
|
return astrcmpi(value, "true") == 0 ||
|
2014-11-10 01:08:23 -08:00
|
|
|
!!str_to_uint64(value);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
double config_get_double(const config_t *config, const char *section,
|
2013-09-30 19:37:13 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_string(config, section, name);
|
|
|
|
if (value)
|
|
|
|
return strtod(value, NULL);
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
2014-03-07 11:56:31 -08:00
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
const char *config_get_default_string(const config_t *config,
|
|
|
|
const char *section, const char *name)
|
2014-03-07 11:56:31 -08:00
|
|
|
{
|
2014-09-26 15:25:59 -07:00
|
|
|
const struct config_item *item;
|
2014-03-07 11:56:31 -08:00
|
|
|
|
|
|
|
item = config_find_item(&config->defaults, section, name);
|
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return item->value;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
int64_t config_get_default_int(const config_t *config, const char *section,
|
2014-03-07 11:56:31 -08:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_default_string(config, section, name);
|
|
|
|
if (value)
|
2014-11-10 01:08:23 -08:00
|
|
|
return str_to_int64(value);
|
2014-03-07 11:56:31 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
uint64_t config_get_default_uint(const config_t *config, const char *section,
|
2014-03-07 11:56:31 -08:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_default_string(config, section, name);
|
|
|
|
if (value)
|
2014-11-10 01:08:23 -08:00
|
|
|
return str_to_uint64(value);
|
2014-03-07 11:56:31 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool config_get_default_bool(const config_t *config, const char *section,
|
2014-03-07 11:56:31 -08:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_default_string(config, section, name);
|
|
|
|
if (value)
|
|
|
|
return astrcmpi(value, "true") == 0 ||
|
2014-11-10 01:08:23 -08:00
|
|
|
!!str_to_uint64(value);
|
2014-03-07 11:56:31 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
double config_get_default_double(const config_t *config, const char *section,
|
2014-03-07 11:56:31 -08:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const char *value = config_get_default_string(config, section, name);
|
|
|
|
if (value)
|
|
|
|
return strtod(value, NULL);
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
2014-07-11 10:58:29 -07:00
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool config_has_user_value(const config_t *config, const char *section,
|
2014-07-11 10:58:29 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return config_find_item(&config->sections, section, name) != NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool config_has_default_value(const config_t *config, const char *section,
|
2014-07-11 10:58:29 -07:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return config_find_item(&config->defaults, section, name) != NULL;
|
|
|
|
}
|
|
|
|
|