diff options
| author | Federico Angelilli <code@fedang.net> | 2024-07-12 13:33:28 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-07-12 13:33:28 +0200 |
| commit | e98943f9eb5cea2921969501e52e8c319323ed4c (patch) | |
| tree | f7879c77e85d4cdea19c53bef2d1ae3dc192c20c /src/config.c | |
| parent | e45c7bc6b41fbcea84ca51d01b8661f93145ce7a (diff) | |
Start working on special sections
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 76 |
1 files changed, 57 insertions, 19 deletions
diff --git a/src/config.c b/src/config.c index d636c31..4451341 100644 --- a/src/config.c +++ b/src/config.c @@ -37,11 +37,15 @@ static const config_entry_t bar_entries[] = { { 0 }, }; -static const config_entry_t block_entries[] = { - { "type", CONFIG_INT, offsetof(block_t, type) }, - { "text", CONFIG_STRING, offsetof(block_t, text.text) }, - { 0 }, -}; +//static const config_entry_t block_entries[] = { +// { "type", CONFIG_INT, offsetof(config_block_t, type) }, +// { "x-padding", CONFIG_UINT, offsetof(config_block_t, x_padding) }, +// { "y-padding", CONFIG_UINT, offsetof(config_block_t, y_padding) }, +// { "text-color", CONFIG_STRING, offsetof(config_block_t, text_color) }, +// { "text", CONFIG_STRING, offsetof(config_block_t, text) }, +// { "color", CONFIG_STRING, offsetof(config_block_t, color) }, +// { 0 }, +//}; static bool config_read_string(const char *value, char **result) { @@ -250,11 +254,6 @@ static bool config_read_entry(const config_entry_t *entries, void *result, int l void config_init(config_t *config) { const config_t config_default = { - .main_block = { - .label = "main_block", - .color = color_rgb(100, 100, 100), - .type = BLOCK_GROUP, - }, .font = "monospace 10", .monitor = NULL, .height = 50, @@ -263,8 +262,6 @@ void config_init(config_t *config) }; memcpy(config, &config_default, sizeof(config_t)); - block_copy(&config->main_block, &config_default.main_block); - config->font = strcopy(config_default.font); config->monitor = strcopy(config_default.monitor); } @@ -281,12 +278,19 @@ void config_read(config_t *config, FILE *file) const config_entry_t *entries = NULL; void *result = NULL; + config_section_t **section_ptr = NULL; + size_t *section_size = NULL; + char *section_label = NULL; + if (section != NULL) { if (!strncmp(section, "block.", 6)) { - entries = block_entries; - config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t)); - assert(config->blocks != NULL); - result = &config->blocks[config->n_blocks - 1]; + section_ptr = &config->blocks; + section_size = &config->n_blocks; + section_label = strcopy(section + 6); + } else if (!strncmp(section, "action.", 7)) { + section_ptr = &config->actions; + section_size = &config->n_actions; + section_label = strcopy(section + 7); } else if (!strcmp(section, "bar")) { entries = bar_entries; result = config; @@ -294,6 +298,19 @@ void config_read(config_t *config, FILE *file) log_warn("Unknown section '%s'", section); } + if (section_ptr != NULL) { + if (*section_label == '\0') { + ++errors; + log_value_error("Sections must have a non-empty label", + "s:section", section, + "i:line", ini.line); + } + + *section_ptr = realloc(*section_ptr, ++(*section_size) * sizeof(config_section_t)); + assert(*section_ptr != NULL); + (*section_ptr)[*section_size - 1].label = section_label; + } + char *key; while ((key = any_ini_stream_next_key(&ini)) != NULL) { char *value = any_ini_stream_next_value(&ini); @@ -318,13 +335,34 @@ void config_read(config_t *config, FILE *file) log_panic("Config file contained %d errors", errors); } +void config_resolve(config_t *config, block_t *block) +{ + int errors = 1; + + if (errors > 0) + log_panic("Failed to resolve config"); +} + void config_free(config_t *config) { - block_free(&config->main_block); + for (int i = 0; i < config->n_blocks; i++) { + for (int j = 0; j < config->blocks[i].n_pairs; j++) + pair_free(&config->blocks[i].pairs[j]); - for (int i = 0; i < config->n_blocks; i++) - block_free(&config->blocks[i]); + free(config->blocks[i].pairs); + free(config->blocks[i].label); + } + + for (int i = 0; i < config->n_actions; i++) { + for (int j = 0; j < config->actions[i].n_pairs; j++) + pair_free(&config->actions[i].pairs[j]); + + free(config->actions[i].pairs); + free(config->actions[i].label); + } + free(config->blocks); + free(config->actions); free(config->font); free(config->monitor); } |
