]> git.ipfire.org Git - pakfire.git/commitdiff
config: Add function to check whether a section exists
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 14:39:41 +0000 (14:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Apr 2021 14:39:41 +0000 (14:39 +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 65aa5fb8401dd161923e5c0a97697ae4d32a8936..b64947ef52f401aa3ee088b1fa0a6040b233dea2 100644 (file)
@@ -244,6 +244,17 @@ char** pakfire_config_sections(struct pakfire_config* config) {
        return sections;
 }
 
+int pakfire_config_has_section(struct pakfire_config* config, const char* section) {
+       struct pakfire_config_entry* entry;
+
+       STAILQ_FOREACH(entry, &config->entries, nodes) {
+               if (strcmp(entry->section, section) == 0)
+                       return 1;
+       }
+
+       return 0;
+}
+
 static ssize_t strip(char* s) {
        if (!s)
                return 0;
index 0dca2b6e265905d6a356fe474dfede41c44db1fc..f87fff122f24cb39b7ddfda9c69503d5d117aacf 100644 (file)
@@ -41,6 +41,7 @@ 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_has_section(struct pakfire_config* config, const char* section);
 
 int pakfire_config_read(struct pakfire_config* config, FILE* f);
 
index 30757b4c701f25ef8ed035b44d840159cf0d8059..903a090ba579f6a941ec78ae876027a9c50322d1 100644 (file)
@@ -59,6 +59,10 @@ static int test_get_and_set(const struct test* t) {
        // Return default instead of NULL
        ASSERT_STRING_EQUALS(pakfire_config_get(config, "section1", "D", "XXX"), "XXX");
 
+       // Check if sections exist
+       ASSERT(pakfire_config_has_section(config, "section1"));
+       ASSERT(!pakfire_config_has_section(config, "section3"));
+
        pakfire_config_free(config);
 
        return EXIT_SUCCESS;