diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-29 13:09:56 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-29 13:09:56 +0100 |
| commit | 728e1e204d39bf11013ee999bcbff779aebc3399 (patch) | |
| tree | a70b7cdf8576142022fc20b6ef219ecb84fcd028 /src/config.c | |
| parent | 8ad2993b9e6679a669b80154639b4941ed2fafb4 (diff) | |
Simplify parsing
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 146 |
1 files changed, 53 insertions, 93 deletions
diff --git a/src/config.c b/src/config.c index 439a869..71b64de 100644 --- a/src/config.c +++ b/src/config.c @@ -14,13 +14,6 @@ #define ANY_INI_IMPLEMENT #include "any_ini.h" -config_enum_t text_align_enum[] = { - { "left", ALIGN_LEFT }, - { "center", ALIGN_CENTER }, - { "right", ALIGN_RIGHT }, - { 0 }, -}; - const config_entry_t bar_entries[] = { { "override-redirect", CONFIG_BOOL, NULL, offsetof(config_t, override_redirect) }, { "action-strict-run", CONFIG_BOOL, NULL, offsetof(config_t, action_strict_run) }, @@ -39,6 +32,13 @@ const config_entry_t bar_entries[] = { { 0 }, }; +const config_enum_t text_align_enum[] = { + { "left", ALIGN_LEFT }, + { "center", ALIGN_CENTER }, + { "right", ALIGN_RIGHT }, + { 0 }, +}; + const config_entry_t block_entries[] = { { "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) }, { "color", CONFIG_GRADIENT, NULL, offsetof(block_t, bg_color) }, @@ -325,30 +325,6 @@ static bool config_read_list(const char *value, char ***result) return true; } -static config_status_t config_read_block(block_t *block, const char **type, const char *key, const char *value) -{ - const char *section = "block"; - size_t index = 0; - - config_status_t status = config_read_entry(block_entries, block, &index, section, block->label, key, value); - *type = config_type_to_string(block_entries[index].type); - - if (status != CONFIG_UNKNOWN) return status; - - if (block->type == BLOCK_GROUP) - status = config_read_entry(block_group_entries, block, &index, section, block->label, key, value); - else if (block->type == BLOCK_TEXT) - status = config_read_entry(block_text_entries, block, &index, section, block->label, key, value); - - *type = config_type_to_string(block_entries[index].type); - if (status != CONFIG_UNKNOWN || block->scheme->entries == NULL) - return status; - - status = config_read_entry(block->scheme->entries, block, &index, section, block->label, key, value); - *type = config_type_to_string(block_entries[index].type); - return status; -} - 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 *)); @@ -364,6 +340,31 @@ static block_t *config_alloc_block(config_t *config, const block_scheme_t *schem return block; } +static config_status_t config_read_block(block_t *block, config_type_t *type, const char *key, const char *value) +{ + size_t index = 0; + config_status_t status = config_read_entry(block_entries, block, &index, key, value); + *type = block_entries[index].type; + + if (status != CONFIG_UNKNOWN) + return status; + + if (block->type == BLOCK_GROUP) { + status = config_read_entry(block_group_entries, block, &index, key, value); + *type = block_group_entries[index].type; + } else if (block->type == BLOCK_TEXT) { + status = config_read_entry(block_text_entries, block, &index, key, value); + *type = block_text_entries[index].type; + } + + if (status != CONFIG_UNKNOWN || block->scheme->entries == NULL) + return status; + + status = config_read_entry(block->scheme->entries, block, &index, key, value); + *type = block->scheme->entries[index].type; + return status; +} + static config_status_t config_read_action(action_t *action, const char *key, const char *value) { action_type_t type; @@ -408,8 +409,7 @@ void config_init(config_t *config) bar->spacing = 10; } -config_status_t config_read_entry(const config_entry_t *entries, void *result, size_t *index, - const char *section, const char *label, const char *key, const char *value) +config_status_t config_read_entry(const config_entry_t *entries, void *result, size_t *index, const char *key, const char *value) { for (size_t i = 0; entries[i].key != NULL; i++) { if (!strcmp(key, entries[i].key)) { @@ -423,99 +423,53 @@ config_status_t config_read_entry(const config_entry_t *entries, void *result, s switch (entries[i].type) { case CONFIG_STRING: - // NOTE: The previous string is freed by config_read_string - if (config_read_string(value, (char **)result)) { - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(char **)result); + if (config_read_string(value, (char **)result)) return CONFIG_SUCCESS; - } break; case CONFIG_INT: - if (config_read_int(value, (int *)result)) { - log_debug("Set '%s%s%s.%s' to '%d'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(int *)result); + if (config_read_int(value, (int *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_UINT: - if (config_read_uint(value, (unsigned int *)result)) { - log_debug("Set '%s%s%s.%s' to '%u'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(unsigned int *)result); + if (config_read_uint(value, (unsigned int *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_DOUBLE: - if (config_read_double(value, (double *)result)) { - log_debug("Set '%s%s%s.%s' to '%g'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(double *)result); + if (config_read_double(value, (double *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_BOOL: - if (config_read_bool(value, (bool *)result)) { - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(bool *)result ? "true" : "false"); + if (config_read_bool(value, (bool *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_COLOR: - if (config_read_color(value, (color_t *)result)) { - char *color = color_to_string((color_t *)result); - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, color); - free(color); + if (config_read_color(value, (color_t *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_GRADIENT: - if (config_read_gradient(value, (gradient_t *)result)) { - char *gradient = gradient_to_string((gradient_t *)result); - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, gradient); - free(gradient); + if (config_read_gradient(value, (gradient_t *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_ENUM: - if (config_read_enum(value, (config_enum_t *)entries[i].data, (int *)result)) { - log_debug("Set '%s%s%s.%s' to '%d'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, *(int *)result); + if (config_read_enum(value, (config_enum_t *)entries[i].data, (int *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_TIME: - if (config_read_time(value, (struct timespec *)result)) { - // TODO: Print - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, value); + if (config_read_time(value, (struct timespec *)result)) return CONFIG_SUCCESS; - } break; case CONFIG_LIST: - if (config_read_list(value, (char ***)result)) { - log_debug("Set '%s%s%s.%s' to '%s'", - section != NULL ? section : "", section != NULL ? "." : "", - label, key, value); + if (config_read_list(value, (char ***)result)) return CONFIG_SUCCESS; - } break; default: @@ -649,18 +603,18 @@ int config_read(config_t *config, FILE *file) if (!bar_section && block == NULL && action == NULL) goto skip_pair; - const char *type = "none"; + config_type_t type; config_status_t status; if (bar_section) { size_t index = 0; - status = config_read_entry(bar_entries, config, &index, NULL, "bar", key, value); + status = config_read_entry(bar_entries, config, &index, key, value); // Try the block entries if (status == CONFIG_UNKNOWN) block = config->blocks[0]; else - type = config_type_to_string(bar_entries[index].type); + type = bar_entries[index].type; } if (block != NULL) { @@ -673,6 +627,12 @@ int config_read(config_t *config, FILE *file) switch (status) { case CONFIG_SUCCESS: + log_value_debug("Parsed entry", + "s:section", section, + "s:key", key, + "s:value", value, + "s:type", config_type_to_string(type), + "i:line", ini.line); break; case CONFIG_INVALID: @@ -681,7 +641,7 @@ int config_read(config_t *config, FILE *file) "s:section", section, "s:key", key, "s:value", value, - "s:type", type, + "s:type", config_type_to_string(type), "i:line", ini.line); break; |
