diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-08-27 21:49:47 -0400 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-08-27 21:49:47 -0400 |
commit | e575bd7b35df28c236a6812b430ed4cbd86deb13 (patch) | |
tree | cdc3b084a687e90e5db38463e1345fa033d6a246 | |
parent | 5622b1ab9ed2b2e9dd304b98447217f6f6c0ea26 (diff) |
config: add support for nested config values (no delimiter just yet)
-rw-r--r-- | include/config.h | 1 | ||||
-rw-r--r-- | src/config.c | 26 |
2 files changed, 27 insertions, 0 deletions
diff --git a/include/config.h b/include/config.h index a5edcc9..c2f6413 100644 --- a/include/config.h +++ b/include/config.h @@ -34,6 +34,7 @@ typedef struct { list_t *keys; list_t *values; list_t *array_values; + list_t *nested_config_values; } config_t; config_t *config_parse(char *content); diff --git a/src/config.c b/src/config.c index f4b1a51..e593020 100644 --- a/src/config.c +++ b/src/config.c @@ -27,6 +27,16 @@ #include <util.h> static void +parse_nested_block(char **buffer, list_t *nested_config_values) +{ + (*buffer)++; + char *raw_block = strsep(buffer, "]"); + + config_t *config = config_parse(raw_block); + list_wrap_and_add(nested_config_values, config); +} + +static void parse_array(char **buffer, list_t *array_values) { (*buffer)++; @@ -55,6 +65,7 @@ config_parse(char *content) list_t *keys = list_create(sizeof(ptr_wrapper_t)); list_t *values = list_create(sizeof(ptr_wrapper_t)); list_t *array_values = list_create(sizeof(ptr_wrapper_t)); + list_t *nested_config_values = list_create(sizeof(ptr_wrapper_t)); char *buffer = strdup(content); /* For free() */ @@ -68,10 +79,19 @@ config_parse(char *content) if (*buffer == '{') { parse_array(&buffer, array_values); + list_wrap_and_add(values, NULL); + list_wrap_and_add(nested_config_values, NULL); + } else if (*buffer == '[') { + parse_nested_block(&buffer, nested_config_values); + + list_wrap_and_add(values, NULL); + list_wrap_and_add(array_values, NULL); } else { parse_simple_string(&buffer, values); + list_wrap_and_add(array_values, NULL); + list_wrap_and_add(nested_config_values, NULL); } key = trim(strsep(&buffer, DELIM)); @@ -83,6 +103,7 @@ config_parse(char *content) config->keys = keys; config->values = values; config->array_values = array_values; + config->nested_config_values = nested_config_values; return config; } @@ -100,6 +121,10 @@ config_delete(config_t *config) if (wrapper->ptr != NULL) free(wrapper->ptr); + wrapper = list_get(config->nested_config_values, i); + if (wrapper->ptr != NULL) + config_delete(wrapper->ptr); + list_t *l = unwrap(list_get(config->array_values, i)); if (l != NULL) { for (size_t y = 0; y < l->size; y++) { @@ -113,6 +138,7 @@ config_delete(config_t *config) list_delete(config->keys); list_delete(config->values); list_delete(config->array_values); + list_delete(config->nested_config_values); free(config); } |