diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-26 15:51:02 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-26 15:51:02 +0530 |
commit | 2d6c8b7a6ab63442f03699c4401bd9742855722f (patch) | |
tree | 98a7be2f3ce6be3b68578eff56b59bdb56189d1e | |
parent | 2c994981958f7562051c4c4660dbfd6cce1bb6f2 (diff) |
config: support arrays
-rw-r--r-- | include/config.h | 1 | ||||
-rw-r--r-- | src/config.c | 38 |
2 files changed, 34 insertions, 5 deletions
diff --git a/include/config.h b/include/config.h index 414e6d6..11bb18e 100644 --- a/include/config.h +++ b/include/config.h @@ -2,6 +2,7 @@ #define __CONFIG_H #define DELIM "=" +#define DELIM_ARRAY "," void config_parse(char *content); diff --git a/src/config.c b/src/config.c index 6c7123c..73f88c5 100644 --- a/src/config.c +++ b/src/config.c @@ -12,6 +12,7 @@ config_parse(char *content) { list_t *keys = list_create(sizeof(char *)); list_t *values = list_create(sizeof(char *)); + list_t *array_values = list_create(sizeof(list_t)); char *buffer = strdup(content); /* For free() */ @@ -26,18 +27,45 @@ config_parse(char *content) if (*buffer == '{') { buffer++; - list_add(values, "\0"); - + list_t *l = list_create(sizeof(char *)); char *raw_array = remove_spaces(strsep(&buffer, "}")); + + char *value = strsep(&raw_array, DELIM_ARRAY); + while (value != NULL) { + list_add(l, strdup(value)); + value = strsep(&raw_array, DELIM_ARRAY); + } + + list_add(array_values, l); + list_add(values, "\0"); } else { char *value = trim(strsep(&buffer, "\n")); + + list_add(array_values, &((list_t) { 0 })); list_add(values, value); } - printf("KEY %s\n", key); - key = trim(strsep(&buffer, DELIM)); } - /* free(x); */ + for (size_t i = 0; i < keys->size; i++) { + char *key = list_get(keys, i); + char *value = list_get(values, i); + + if (strcmp(value, "\0") == 0) { + printf("%s ARRAY with VALUES ", key); + list_t *xvalues = list_get(array_values, i); + + for (size_t y = 0; y < xvalues->size; y++) { + char *value = list_get(xvalues, y); + printf("%s ", value); + } + + printf("\n"); + } else { + printf("%s: %s\n", key, value); + } + } + + free(x); } |