]> git.ipfire.org Git - pakfire.git/commitdiff
config: Add function that lists all sections
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 13:55:32 +0000 (13:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 13:55:32 +0000 (13:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/config.h
tests/libpakfire/config.c

index 13dc13fc1753459a7534e6651b12fe2e0b3b423c..65aa5fb8401dd161923e5c0a97697ae4d32a8936 100644 (file)
@@ -206,6 +206,44 @@ int pakfire_config_get_bool(struct pakfire_config* config,
        return _default;
 }
 
+static int pakfire_section_in_sections(char** sections, const char* section) {
+       if (!sections)
+               return 0;
+
+       for (char** s = sections; *s; s++) {
+               if (strcmp(section, *s) == 0)
+                       return 1;
+       }
+
+       return 0;
+}
+
+char** pakfire_config_sections(struct pakfire_config* config) {
+       struct pakfire_config_entry* entry;
+
+       char** sections = NULL;
+       size_t num_sections = 0;
+
+       STAILQ_FOREACH(entry, &config->entries, nodes) {
+               // Skip empty sections
+               if (!*entry->section)
+                       continue;
+
+               // Skip if this section is already on the list
+               if (pakfire_section_in_sections(sections, entry->section))
+                       continue;
+
+               // Make space in the array
+               sections = reallocarray(sections, sizeof(*sections), num_sections++ + 1);
+               if (!sections)
+                       return NULL;
+
+               sections[num_sections - 1] = strdup(entry->section);
+       }
+
+       return sections;
+}
+
 static ssize_t strip(char* s) {
        if (!s)
                return 0;
index 31fc5666bf0b4b2ff44765d8670a74ea6317c2ac..0dca2b6e265905d6a356fe474dfede41c44db1fc 100644 (file)
@@ -40,6 +40,8 @@ long int pakfire_config_get_int(struct pakfire_config* config,
 int pakfire_config_get_bool(struct pakfire_config* config,
        const char* section, const char* key, int _default);
 
+char** pakfire_config_sections(struct pakfire_config* config);
+
 int pakfire_config_read(struct pakfire_config* config, FILE* f);
 
 #endif
index 235297549cd357e3ebb415fa55f0e5fd3711aee9..30757b4c701f25ef8ed035b44d840159cf0d8059 100644 (file)
@@ -91,6 +91,22 @@ static int test_parse(const struct test* t) {
        ASSERT_STRING_EQUALS(pakfire_config_get(config, "section2", "key1", NULL), "value1");
        ASSERT_STRING_EQUALS(pakfire_config_get(config, "section2", "key2", NULL), "value2");
 
+       // Get sections
+       char** sections = pakfire_config_sections(config);
+       ASSERT(sections);
+
+       // Count sections
+       size_t counter = 0;
+       for (char** section = sections; *section; section++)
+               counter++;
+
+       ASSERT(counter == 2);
+
+       // Check for section names
+       ASSERT_STRING_EQUALS(sections[0], "section1");
+       ASSERT_STRING_EQUALS(sections[1], "section2");
+       ASSERT_NULL(sections[2]);
+
        pakfire_config_free(config);
 
        fclose(f);