diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-08-27 11:34:22 -0400 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-08-27 11:34:22 -0400 |
commit | 6c4b9cb920d8dd1ceca6723f3df5257c5925f727 (patch) | |
tree | 5b9c71036066c676593a1c7c385589ac382def51 | |
parent | 1760d67f85c47b74a4b7b5d2d73c30f589dd704a (diff) |
config: add separate functions for parsing arrays and simple strings
-rw-r--r-- | src/config.c | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/src/config.c b/src/config.c index 1ca48a4..52448d4 100644 --- a/src/config.c +++ b/src/config.c @@ -26,6 +26,29 @@ #include <string.h> #include <util.h> +static void +parse_array(char **buffer, list_t *array_values) +{ + (*buffer)++; + list_t *l = list_create(sizeof(ptr_wrapper_t)); + char *raw_array = strsep(buffer, "}"); + + char *value = strsep(&raw_array, DELIM_ARRAY); + while (value != NULL) { + list_wrap_and_add(l, strdup(trim(value))); + value = strsep(&raw_array, DELIM_ARRAY); + } + + list_wrap_and_add(array_values, l); +} + +static void +parse_simple_string(char **buffer, list_t *values) +{ + char *value = trim(strsep(buffer, "\n")); + list_wrap_and_add(values, strdup(value)); +} + config_t * config_parse(char *content) { @@ -44,23 +67,11 @@ config_parse(char *content) list_wrap_and_add(keys, strdup(key)); if (*buffer == '{') { - buffer++; - list_t *l = list_create(sizeof(ptr_wrapper_t)); - char *raw_array = strsep(&buffer, "}"); - - char *value = strsep(&raw_array, DELIM_ARRAY); - while (value != NULL) { - list_wrap_and_add(l, strdup(trim(value))); - value = strsep(&raw_array, DELIM_ARRAY); - } - - list_wrap_and_add(array_values, l); + parse_array(&buffer, array_values); list_wrap_and_add(values, NULL); } else { - char *value = trim(strsep(&buffer, "\n")); - + parse_simple_string(&buffer, values); list_wrap_and_add(array_values, NULL); - list_wrap_and_add(values, strdup(value)); } key = trim(strsep(&buffer, DELIM)); |