util: json: Introduce virJSONValueObjectAppendStringPrintf

Add a variant similar to virJSONValueObjectAppendString which also
formats more complex value strings with printf syntax.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Peter Krempa 2020-05-14 08:09:56 +02:00
parent 003f063dd6
commit 9c9732d03a
3 changed files with 20 additions and 0 deletions

View File

@ -2361,6 +2361,7 @@ virJSONValueObjectAppendNumberLong;
virJSONValueObjectAppendNumberUint;
virJSONValueObjectAppendNumberUlong;
virJSONValueObjectAppendString;
virJSONValueObjectAppendStringPrintf;
virJSONValueObjectCreate;
virJSONValueObjectCreateVArgs;
virJSONValueObjectDeflatten;

View File

@ -649,6 +649,23 @@ virJSONValueObjectAppendString(virJSONValuePtr object,
}
int
virJSONValueObjectAppendStringPrintf(virJSONValuePtr object,
const char *key,
const char *fmt,
...)
{
va_list ap;
g_autofree char *str = NULL;
va_start(ap, fmt);
str = g_strdup_vprintf(fmt, ap);
va_end(ap);
return virJSONValueObjectInsertString(object, key, str, false);
}
int
virJSONValueObjectPrependString(virJSONValuePtr object,
const char *key,

View File

@ -127,6 +127,8 @@ int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *
int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key);
int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value);
int virJSONValueObjectAppendStringPrintf(virJSONValuePtr object, const char *key, const char *fmt, ...)
G_GNUC_PRINTF(3, 4);
int virJSONValueObjectPrependString(virJSONValuePtr object, const char *key, const char *value);
int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number);
int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number);