]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
config: Add function to format values
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Oct 2022 16:00:17 +0000 (16:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 11 Oct 2022 16:00:17 +0000 (16:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/config.h

index 2697b48f38bd2e4463c4ecf353436fe52f39342d..67c50e409a50ca4fde3b60d3b00fb0e0b97dc039 100644 (file)
@@ -178,6 +178,30 @@ int pakfire_config_set(struct pakfire_config* config,
        return pakfire_string_set(entry->value, value);
 }
 
+int pakfire_config_set_format(struct pakfire_config* config,
+               const char* section, const char* key, const char* format, ...) {
+       char* buffer = NULL;
+       va_list args;
+       int r;
+
+       va_start(args, format);
+       r = vasprintf(&buffer, format, args);
+       va_end(args);
+
+       // Break on any errors
+       if (r < 0)
+               return r;
+
+       // Set the value
+       r = pakfire_config_set(config, section, key, buffer);
+
+       // Cleanup
+       if (buffer)
+               free(buffer);
+
+       return r;
+}
+
 static int pakfire_config_append(struct pakfire_config* config,
                const char* section, const char* key, const char* value2) {
        char* buffer = NULL;
index 5688c015b7e9289266dfde162eec85dc91e1c80a..81534377b6a261e0495dc66edce36a773d4e4501 100644 (file)
@@ -33,6 +33,8 @@ struct pakfire_config* pakfire_config_unref(struct pakfire_config* config);
 
 int pakfire_config_set(struct pakfire_config* config,
        const char* section, const char* key, const char* value);
+int pakfire_config_set_format(struct pakfire_config* config,
+       const char* section, const char* key, const char* format, ...);
 
 const char* pakfire_config_get(struct pakfire_config* config,
        const char* section, const char* key, const char* _default);