From: John Ferlan Date: Mon, 3 Oct 2016 18:45:13 +0000 (-0400) Subject: util: Introduce virJSONValueObjectStealArray X-Git-Tag: v2.4.0-rc1~210 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ebf8b783bfeeaeaa0fd99ab5ec864fd8d98f3a6d;p=thirdparty%2Flibvirt.git util: Introduce virJSONValueObjectStealArray Provide the Steal API for any code paths that will desire to grab the object array and then free it afterwards rather than relying to freeing the whole chain from the reply. --- diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index eae817d14d..c4af57d02b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1821,6 +1821,7 @@ virJSONValueObjectHasKey; virJSONValueObjectIsNull; virJSONValueObjectKeysNumber; virJSONValueObjectRemoveKey; +virJSONValueObjectStealArray; virJSONValueToString; diff --git a/src/util/virjson.c b/src/util/virjson.c index b6d9a34201..1d8e6d5774 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -770,6 +770,30 @@ virJSONValueObjectGet(virJSONValuePtr object, } +static virJSONValuePtr +virJSONValueObjectSteal(virJSONValuePtr object, + const char *key) +{ + size_t i; + virJSONValuePtr obj = NULL; + + if (object->type != VIR_JSON_TYPE_OBJECT) + return NULL; + + for (i = 0; i < object->data.object.npairs; i++) { + if (STREQ(object->data.object.pairs[i].key, key)) { + VIR_STEAL_PTR(obj, object->data.object.pairs[i].value); + VIR_FREE(object->data.object.pairs[i].key); + VIR_DELETE_ELEMENT(object->data.object.pairs, i, + object->data.object.npairs); + break; + } + } + + return obj; +} + + /* Return the value associated with KEY within OBJECT, but return NULL * if the key is missing or if value is not the correct TYPE. */ virJSONValuePtr @@ -785,6 +809,21 @@ virJSONValueObjectGetByType(virJSONValuePtr object, } +/* Steal the value associated with KEY within OBJECT, but return NULL + * if the key is missing or if value is not the correct TYPE. */ +static virJSONValuePtr +virJSONValueObjectStealByType(virJSONValuePtr object, + const char *key, + virJSONType type) +{ + virJSONValuePtr value = virJSONValueObjectSteal(object, key); + + if (value && value->type == type) + return value; + return NULL; +} + + int virJSONValueObjectKeysNumber(virJSONValuePtr object) { @@ -1194,6 +1233,13 @@ virJSONValueObjectGetArray(virJSONValuePtr object, const char *key) } +virJSONValuePtr +virJSONValueObjectStealArray(virJSONValuePtr object, const char *key) +{ + return virJSONValueObjectStealByType(object, key, VIR_JSON_TYPE_ARRAY); +} + + int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key) diff --git a/src/util/virjson.h b/src/util/virjson.h index 64cae88dff..8b62d651d6 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -136,6 +136,8 @@ virJSONValuePtr virJSONValueObjectGetObject(virJSONValuePtr object, const char *key); virJSONValuePtr virJSONValueObjectGetArray(virJSONValuePtr object, const char *key); +virJSONValuePtr virJSONValueObjectStealArray(virJSONValuePtr object, + const char *key); const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key); int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value);