Fix config handling

This commit is contained in:
Tobias Reisinger 2020-11-12 21:58:01 +01:00
parent fad3d80f39
commit fca35ade9e
18 changed files with 125 additions and 139 deletions

View file

@ -1,10 +1,11 @@
#include <stdlib.h>
#include <string.h>
#include <logger.h>
#include <config.h>
#include <constants.h>
#include <logger.h>
config_t global_config;
config_t *global_config;
#define CONFINI_IS_KEY(SECTION, KEY) \
@ -57,6 +58,7 @@ config_load_log_level(IniDispatch *disp, config_t *config)
return 0;
}
LOGGER_WARNING("invalid log-level '%s'\n", disp->value);
return 0;
}
@ -77,8 +79,49 @@ config_load_log_file(IniDispatch *disp, config_t *config)
return 0;
}
static void
config_load_string(char **holder, char *value)
void
config_init()
{
global_config = calloc(1, sizeof(config_t));
config_load_string(&global_config->file, DEFAULT_CONFIG_PATH);
global_config->discovery_port = 4421;
global_config->mqtt_port = 1885;
global_config->server_port = 5000;
global_config->log_level = LOG_DEBUG;
global_config->log_file = stdout;
global_config->user = NULL;
global_config->group = NULL;
config_load_string(&global_config->content_dir, ".");
config_load_string(&global_config->not_found_file, "404.html");
config_load_string(&global_config->not_found_file_type, "text/html");
config_load_string(&global_config->not_found_content, "404 - NOT FOUND");
config_load_string(&global_config->not_found_content_type, "text/plain");
}
void
config_free()
{
free(global_config->file);
free(global_config->include);
free(global_config->database);
free(global_config->user);
free(global_config->group);
free(global_config->content_dir);
free(global_config->not_found_file);
free(global_config->not_found_file_type);
free(global_config->not_found_content);
free(global_config->not_found_content_type);
free(global_config);
}
void
config_load_string(char **holder, const char *value)
{
if(*holder)
{
@ -93,50 +136,6 @@ config_load_string(char **holder, char *value)
*holder = new_holder;
}
void
config_init()
{
memset(&global_config, 0, sizeof(config_t));
global_config.discovery_port = 4421;
global_config.mqtt_port = 1885;
global_config.server_port = 5000;
global_config.log_level = LOG_INFO;
global_config.log_file = stdout;
global_config.user = NULL;
global_config.group = NULL;
config_load_string(&global_config.content_dir, ".");
config_load_string(&global_config.not_found_file, "404.html");
config_load_string(&global_config.not_found_file_type, "text/html");
config_load_string(&global_config.not_found_content, "404 - NOT FOUND");
config_load_string(&global_config.not_found_content_type, "text/plain");
}
void
config_free()
{
free(global_config.file);
free(global_config.include);
free(global_config.database);
free(global_config.user);
free(global_config.group);
free(global_config.content_dir);
free(global_config.not_found_file);
free(global_config.not_found_file_type);
free(global_config.not_found_content);
free(global_config.not_found_content_type);
}
void
config_try_ini_files(config_t *config)
{
(void)config;
}
int
config_load(IniDispatch *disp, void *config_void)
{