From bcef77a7fb3b73d2a2fbcea51012014b62755bb5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 31 May 2019 18:42:01 +0200 Subject: Switch to a JSON-based config format We can get rid of a lot of code by ditching out INI format parser. We also remove the support for "subtype" and "generator" templates for now; rather than implementing this in NeCo itself, templates could be implemented as a Lua DSL later. --- src/config-ini.c | 140 ------------------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100644 src/config-ini.c (limited to 'src/config-ini.c') diff --git a/src/config-ini.c b/src/config-ini.c deleted file mode 100644 index 0461e13..0000000 --- a/src/config-ini.c +++ /dev/null @@ -1,140 +0,0 @@ -#include "config-ini.h" - -#include -#include -#include -#include - -static void free_field(ini_field_t *field) { - free(field->key); - free(field->value); - free(field); -} - -static void free_section(ini_section_t *section) { - ini_field_t *field, *tmp; - - list_for_each_entry_safe(field, tmp, §ion->fields, node) - free_field(field); - - free(section->name); - free(section); -} - -void free_ini_file(ini_file_t *file) { - ini_section_t *section, *tmp; - - list_for_each_entry_safe(section, tmp, &file->sections, node) - free_section(section); - - free(file); -} - -static bool add_field(ini_section_t *section, const char *key, const char *value) { - ini_field_t *field = calloc(1, sizeof(*field)); - if (!field) - return false; - - field->key = strdup(key); - field->value = strdup(value); - - if (!field->key || !field->value) { - free_field(field); - return false; - } - - list_add_tail(&field->node, §ion->fields); - - return true; -} - -static ini_section_t * add_section(ini_file_t *file, const char *name) { - ini_section_t *section = calloc(1, sizeof(*section)); - if (!section) - return NULL; - - section->name = strdup(name); - if (!section->name) { - free(section); - return false; - } - - INIT_LIST_HEAD(§ion->fields); - list_add_tail(§ion->node, &file->sections); - - return section; -} - -ini_file_t * read_ini_file(FILE *f) { - ini_file_t *file = calloc(1, sizeof(*file)); - if (!file) - goto error; - - INIT_LIST_HEAD(&file->sections); - - ini_section_t *section = NULL; - int err = 0; - - char *line = NULL; - size_t n = 0; - - while (getline(&line, &n, f) >= 0) { - char *input = line; - - while (isspace(input[0])) - input++; - - if (input[0] == '#' || input[0] == '\0') - continue; - - size_t len = strlen(input); - - while (isspace(input[len-1])) - len--; - - if (input[0] == '[') { - if (input[len-1] != ']') { - err = EINVAL; - goto error; - } - - input[len-1] = '\0'; - - section = add_section(file, input+1); - if (!section) - goto error; - } else { - if (!section) { - err = EINVAL; - goto error; - } - - input[len] = '\0'; - - char *delim = strchr(input, '='); - if (!delim) { - err = EINVAL; - goto error; - } - - *delim = '\0'; - - if (!add_field(section, input, delim+1)) - goto error; - } - } - - if (ferror(f)) - err = EIO; - -error: - free(line); - - if (err) { - free_ini_file(file); - errno = err; - return NULL; - } - - return file; -} -- cgit v1.2.3