]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add function to add formatted strings to an array
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 14:59:13 +0000 (14:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 14:59:13 +0000 (14:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index ded6663c85a4db28f0f7a248bb0119dea5431538..bdceb3baf5f4888aa901272c8def1ea3a47d20b3 100644 (file)
@@ -244,6 +244,27 @@ int pakfire_json_array_add_string(struct json_object* array, const char* s) {
        return json_object_array_add(array, object);
 }
 
+int pakfire_json_array_add_stringf(struct json_object* array, const char* format, ...) {
+       char* buffer = NULL;
+       va_list args;
+       int r;
+
+       // Format the input
+       va_start(args, format);
+       r = vasprintf(&buffer, format, args);
+       va_end(args);
+       if (r < 0)
+               return r;
+
+       // Add the string to the array
+       r = pakfire_json_array_add_string(array, buffer);
+
+       // Cleanup
+       free(buffer);
+
+       return r;
+}
+
 static int __pakfire_json_get_object(struct json_object* json,
                const char* key, const json_type type, struct json_object** o) {
        struct json_object* object = NULL;
index 438398c0c49cc238892bbdb24529ac7d7a9b8bb1..764bf2a7cb1a2cec6662ff9a9abde37caeca0b00 100644 (file)
@@ -48,6 +48,7 @@ int pakfire_json_add_object(struct json_object* json, const char* name, struct j
 int pakfire_json_add_array(struct json_object* json, const char* name, struct json_object** array);
 
 int pakfire_json_array_add_string(struct json_object* array, const char* s);
+int pakfire_json_array_add_stringf(struct json_object* array, const char* format, ...);
 
 int pakfire_json_get_string(struct json_object* json, const char* key, const char** value);
 int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* value);