aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-12 01:44:14 +0100
committerFederico Angelilli <code@fedang.net>2024-11-12 01:44:14 +0100
commite85ad7414c063e42296f7ef4e202829567484ac0 (patch)
tree25454de6b83a2dd91142c97b4a28add01331b3ea /src/config.c
parent686802c2a472746a1b40fab30cbb0351e536c9ba (diff)
Fix config and blocks
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c42
1 files changed, 21 insertions, 21 deletions
diff --git a/src/config.c b/src/config.c
index 0d48ef6..d89bdde 100644
--- a/src/config.c
+++ b/src/config.c
@@ -350,6 +350,21 @@ static config_status_t config_read_block(const block_scheme_t *scheme, block_t *
: config_read_entry(scheme->entries, block, type, section, key, value);
}
+static block_t *config_alloc_block(config_t *config, const block_scheme_t *scheme, char *label)
+{
+ config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t *));
+ assert(config->blocks != NULL);
+
+ config->blocks[config->n_blocks - 1] = calloc(1, scheme->size);
+ block_t *block = config->blocks[config->n_blocks - 1];
+ assert(block != NULL);
+
+ memcpy(block, &scheme->block, sizeof(block_t));
+ block->label = label;
+ return block;
+}
+
+// TODO: More robust way to define defaults
void config_init(config_t *config)
{
const config_t config_default = {
@@ -363,11 +378,9 @@ void config_init(config_t *config)
memcpy(config, &config_default, sizeof(config_t));
config->font = strcopy(config_default.font);
- config->n_blocks = 1;
- config->blocks = calloc(1, sizeof(block_t *));
- config->blocks[0]->label = strcopy("bar");
- config->blocks[0]->type = BLOCK_GROUP;
- //config->blocks->group.spacing = 10;
+ extern const block_scheme_t block_group_scheme;
+ block_group_t *bar = (block_group_t *)config_alloc_block(config, &block_group_scheme, strcopy("bar"));
+ bar->spacing = 10;
}
void config_read(config_t *config, FILE *file)
@@ -388,10 +401,6 @@ void config_read(config_t *config, FILE *file)
if (section != NULL) {
if (!strncmp(section, "block.", 6)) {
- config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t *));
- assert(config->blocks != NULL);
- block = config->blocks[config->n_blocks - 1];
-
char *label = strcopy(section + 6);
if (label == NULL || *label == '\0' || !strcmp(label, "bar")) {
++errors;
@@ -399,7 +408,7 @@ void config_read(config_t *config, FILE *file)
"s:section", section,
"i:line", ini.line);
} else {
- for (size_t i = 0; i < config->n_blocks - 1; i++) {
+ for (size_t i = 0; i < config->n_blocks; i++) {
if (!strcmp(label, config->blocks[i]->label)) {
++errors;
log_value_error("Block section must have a unique label",
@@ -432,15 +441,7 @@ void config_read(config_t *config, FILE *file)
continue;
scheme = block_schemes[i];
- block_copy(block, &scheme->block);
- free(block->label);
- block->label = label;
-
- //if (scheme->size != 0) {
- // block->state = malloc(scheme->size);
- // memset(block->state, 0, scheme->size);
- //}
-
+ block = config_alloc_block(config, scheme, label);
goto skip_pair;
}
@@ -481,8 +482,7 @@ void config_read(config_t *config, FILE *file)
}
if (block != NULL) {
- status = config_read_block(scheme, config->blocks[config->n_blocks - 1],
- &type, section, key, value);
+ status = config_read_block(scheme, block, &type, section, key, value);
}
switch (status) {