aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-12 00:36:47 +0100
committerFederico Angelilli <code@fedang.net>2024-11-12 00:36:47 +0100
commit686802c2a472746a1b40fab30cbb0351e536c9ba (patch)
tree9a4fe3fa654fbe7b9bb35893a2da71c9754745b4 /src/config.c
parent3f2af32442b9384d316651d42ad35945533dc39e (diff)
Modify block struct
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c94
1 files changed, 52 insertions, 42 deletions
diff --git a/src/config.c b/src/config.c
index 84804bc..0d48ef6 100644
--- a/src/config.c
+++ b/src/config.c
@@ -44,9 +44,9 @@ static const config_entry_t block_entries[] = {
};
static const config_entry_t block_group_entries[] = {
- { "spacing", CONFIG_UINT, NULL, offsetof(block_t, group.spacing) },
- { "blocks", CONFIG_LIST, NULL, offsetof(block_t, group.children) },
- { "collapse", CONFIG_BOOL, NULL, offsetof(block_t, group.collapse) },
+ { "spacing", CONFIG_UINT, NULL, offsetof(block_group_t, spacing) },
+ { "blocks", CONFIG_LIST, NULL, offsetof(block_group_t, children) },
+ { "collapse", CONFIG_BOOL, NULL, offsetof(block_group_t, collapse) },
{ 0 },
};
@@ -58,10 +58,10 @@ static config_enum_t text_align_enum[] = {
};
static const config_entry_t block_text_entries[] = {
- { "text", CONFIG_STRING, NULL, offsetof(block_t, text.text) },
- { "text-color", CONFIG_COLOR, NULL, offsetof(block_t, text.text_color) },
- { "text-size", CONFIG_UINT, NULL, offsetof(block_t, text.text_size) },
- { "text-align", CONFIG_ENUM, text_align_enum, offsetof(block_t, text.text_size) },
+ { "text", CONFIG_STRING, NULL, offsetof(block_text_t, text) },
+ { "text-color", CONFIG_COLOR, NULL, offsetof(block_text_t, text_color) },
+ { "text-size", CONFIG_UINT, NULL, offsetof(block_text_t, text_size) },
+ { "text-align", CONFIG_ENUM, text_align_enum, offsetof(block_text_t, text_size) },
{ 0 },
};
@@ -347,7 +347,7 @@ static config_status_t config_read_block(const block_scheme_t *scheme, block_t *
return status != CONFIG_UNKNOWN || scheme == NULL || scheme->entries == NULL
? status
- : config_read_entry(scheme->entries, block->state, type, section, key, value);
+ : config_read_entry(scheme->entries, block, type, section, key, value);
}
void config_init(config_t *config)
@@ -364,10 +364,10 @@ void config_init(config_t *config)
config->font = strcopy(config_default.font);
config->n_blocks = 1;
- config->blocks = calloc(1, sizeof(block_t));
- config->blocks->label = strcopy("bar");
- config->blocks->type = BLOCK_GROUP;
- config->blocks->group.spacing = 10;
+ config->blocks = calloc(1, sizeof(block_t *));
+ config->blocks[0]->label = strcopy("bar");
+ config->blocks[0]->type = BLOCK_GROUP;
+ //config->blocks->group.spacing = 10;
}
void config_read(config_t *config, FILE *file)
@@ -388,9 +388,9 @@ 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));
+ config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t *));
assert(config->blocks != NULL);
- block = &config->blocks[config->n_blocks - 1];
+ block = config->blocks[config->n_blocks - 1];
char *label = strcopy(section + 6);
if (label == NULL || *label == '\0' || !strcmp(label, "bar")) {
@@ -400,7 +400,7 @@ void config_read(config_t *config, FILE *file)
"i:line", ini.line);
} else {
for (size_t i = 0; i < config->n_blocks - 1; i++) {
- if (!strcmp(label, config->blocks[i].label)) {
+ if (!strcmp(label, config->blocks[i]->label)) {
++errors;
log_value_error("Block section must have a unique label",
"s:section", section,
@@ -436,10 +436,10 @@ void config_read(config_t *config, FILE *file)
free(block->label);
block->label = label;
- if (scheme->size != 0) {
- block->state = malloc(scheme->size);
- memset(block->state, 0, scheme->size);
- }
+ //if (scheme->size != 0) {
+ // block->state = malloc(scheme->size);
+ // memset(block->state, 0, scheme->size);
+ //}
goto skip_pair;
}
@@ -477,11 +477,11 @@ void config_read(config_t *config, FILE *file)
// Try the block entries
if (status == CONFIG_UNKNOWN)
- block = config->blocks;
+ block = config->blocks[0];
}
if (block != NULL) {
- status = config_read_block(scheme, &config->blocks[config->n_blocks - 1],
+ status = config_read_block(scheme, config->blocks[config->n_blocks - 1],
&type, section, key, value);
}
@@ -531,40 +531,48 @@ skip_pair:
log_panic("Config file contained %d errors", n_errors);
}
-static bool config_resolve_children(config_t *config, block_t *block)
+bool config_resolve_children(config_t *config, block_t *block)
{
block->resolved = true;
+
if (block->type == BLOCK_TEXT)
return true;
- char **children = (char **)block->group.children;
- block->group.children = NULL;
- block->group.n_children = 0;
+ if (block->type == BLOCK_SPEC) {
+ block_spec_t *spec = (block_spec_t *)block;
+ return spec->resolve_fn == NULL || spec->resolve_fn(block, config);
+ }
+
+ assert(block->type == BLOCK_GROUP);
+ block_group_t *group = (block_group_t *)block;
+
+ char **children = (char **)group->children;
+ group->children = NULL;
+ group->n_children = 0;
if (children == NULL)
return true;
- size_t n = 0;
- for ( ; children[n] != NULL; n++);
+ while (children[group->n_children] != NULL)
+ group->n_children++;
- block->group.n_children = n;
- block->group.children = malloc(n * sizeof(block_t *));
+ group->children = malloc(group->n_children * sizeof(block_t *));
- for (size_t i = 0; i < n; i++) {
+ for (size_t i = 0; i < group->n_children; i++) {
for (size_t j = 0; j < config->n_blocks; j++) {
- if (&config->blocks[j] == block)
+ if (config->blocks[j] == block)
continue;
- if (!strcmp(children[i], config->blocks[j].label)) {
- if (config->blocks[j].resolved) {
- log_error("Block '%s' can only be referenced by one block", config->blocks[j].label);
+ if (!strcmp(children[i], config->blocks[j]->label)) {
+ if (config->blocks[j]->resolved) {
+ log_error("Block '%s' can only be referenced by one block", config->blocks[j]->label);
return false;
}
- log_debug("Block '%s' is parent of '%s'", block->label, config->blocks[j].label);
- block->group.children[i] = &config->blocks[j];
+ log_debug("Block '%s' is parent of '%s'", block->label, config->blocks[j]->label);
+ group->children[i] = config->blocks[j];
- if (!config_resolve_children(config, &config->blocks[j]))
+ if (!config_resolve_children(config, config->blocks[j]))
return false;
goto next;
}
@@ -581,25 +589,27 @@ next:
block_t *config_resolve(config_t *config)
{
int errors = 0;
- if (config->blocks->group.children == NULL) {
+ block_group_t *bar = (block_group_t *)config->blocks[0];
+
+ if (bar->children == NULL) {
errors++;
log_error("Section '%s' requires at least one child", "bar");
} else {
// NOTE: The first item is the main block
- config->blocks->min_width = config->blocks->max_width = config->width;
- errors += !config_resolve_children(config, config->blocks);
+ bar->block.min_width = bar->block.max_width = config->width;
+ errors += !config_resolve_children(config, config->blocks[0]);
}
if (errors > 0)
log_panic("Config could not be resolved");
- return config->blocks;
+ return config->blocks[0];
}
void config_free(config_t *config)
{
for (int i = 0; i < config->n_blocks; i++)
- block_free(&config->blocks[i]);
+ block_free(config->blocks[i]);
free(config->blocks);
free(config->font);