]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virJSONValueObjectStealArray
authorJohn Ferlan <jferlan@redhat.com>
Mon, 3 Oct 2016 18:45:13 +0000 (14:45 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Wed, 5 Oct 2016 15:12:02 +0000 (11:12 -0400)
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.

src/libvirt_private.syms
src/util/virjson.c
src/util/virjson.h

index eae817d14dc1f439682e78ac52435029ca70bcf6..c4af57d02bc482e85767ac0acf5900d516898af8 100644 (file)
@@ -1821,6 +1821,7 @@ virJSONValueObjectHasKey;
 virJSONValueObjectIsNull;
 virJSONValueObjectKeysNumber;
 virJSONValueObjectRemoveKey;
+virJSONValueObjectStealArray;
 virJSONValueToString;
 
 
index b6d9a3420194f31455f644a169ffc19a3fe14632..1d8e6d577403c5b2d21c1c614d8f8bc4e58647cf 100644 (file)
@@ -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)
index 64cae88dffb5def546226423a678c44ff9a3ac35..8b62d651d60e727e271e90a546fb7db96d83f901 100644 (file)
@@ -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);