[PATCH v3 1/4] util: Introduce virJSONValueObjectStealArray

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Provide the Steal API for any code paths that will desire to grab the
entire array and then free it afterwards rather than relying to freeing
the whole chain from the reply.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/util/virjson.h       |  2 ++
 3 files changed, 46 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index eae817d..c4af57d 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 b6d9a34..e705f53 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -770,6 +770,27 @@ 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);
+            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 +806,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 +1230,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 64cae88..8b62d65 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);
-- 
2.7.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]