]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add a convenience function to format strings
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 17:46:38 +0000 (17:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Feb 2025 17:46:38 +0000 (17:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index ee3177eb806af34bdc44086d48b432d7055e9ce9..b9ad764db835f95b28ee61fe65a3a525bbfaeab2 100644 (file)
@@ -119,6 +119,28 @@ int pakfire_json_add_stringn(struct json_object* json,
        return json_object_object_add(json, name, object);
 }
 
+int pakfire_json_add_stringf(struct json_object* json,
+               const char* name, 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
+       r = pakfire_json_add_stringn(json, name, buffer, r);
+
+       // Cleanup
+       free(buffer);
+
+       return r;
+}
+
 int pakfire_json_add_string_array(struct json_object* json, const char* name, char** array) {
        int r = 1;
 
index 7ea0886e2a0fbdefaa933cfea9df56fa157a7c95..6dfcb682876351d4cea6e4fc01f6722b51f85a42 100644 (file)
@@ -38,6 +38,8 @@ struct json_object* pakfire_json_new_object(void);
 
 int pakfire_json_add_string(struct json_object* json, const char* name, const char* value);
 int pakfire_json_add_stringn(struct json_object* json, const char* name, const char* value, size_t length);
+int pakfire_json_add_stringf(struct json_object* json, const char* name, const char* format, ...)
+       __attribute__((format(printf, 3, 4)));
 int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value);
 int pakfire_json_add_uint64(struct json_object* json, const char* name, uint64_t value);
 int pakfire_json_add_double(struct json_object* json, const char* name, double value);