Fix config string handling

This commit is contained in:
Tobias Reisinger 2020-11-04 23:03:33 +01:00
parent b7d3f2ad0b
commit ead52a0f47
3 changed files with 95 additions and 33 deletions

View file

@ -6,6 +6,7 @@
config_t global_config;
#define CONFINI_IS_KEY(SECTION, KEY) \
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
ini_string_match_ii(KEY, disp->data, disp->format))
@ -76,6 +77,66 @@ config_load_log_file(IniDispatch *disp, config_t *config)
return 0;
}
static void
config_load_string(char **holder, char *value)
{
if(*holder)
{
free(*holder);
}
size_t value_len = strlen(value);
char *new_holder = malloc(sizeof(char) * (value_len + 1));
strcpy(new_holder, value);
new_holder[value_len] = '\0';
*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)
{
@ -83,44 +144,49 @@ config_load(IniDispatch *disp, void *config_void)
if(disp->type == INI_KEY)
{
if(CONFINI_IS_KEY("core", "include"))
{
config_load_string(&config->include, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "database"))
{
strcpy(config->database, disp->value);
config_load_string(&config->database, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "user"))
{
strcpy(config->user, disp->value);
config_load_string(&config->user, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "group"))
{
strcpy(config->group, disp->value);
config_load_string(&config->group, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "content-dir"))
{
strcpy(config->content_dir, disp->value);
config_load_string(&config->content_dir, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-file"))
{
strcpy(config->not_found_file, disp->value);
config_load_string(&config->not_found_file, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-file-type"))
{
strcpy(config->not_found_file_type, disp->value);
config_load_string(&config->not_found_file_type, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-content"))
{
strcpy(config->not_found_content, disp->value);
config_load_string(&config->not_found_content, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-content-type"))
{
strcpy(config->not_found_content_type, disp->value);
config_load_string(&config->not_found_content_type, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "log-level"))