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;
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
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);