[PATCH 01/15] util: json: add helper to iterate and steal members of json array

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

 



Simplifies cases where JSON array members need to be transferred to a
different structure.
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 36 ++++++++++++++++++++++++++++++++++++
 src/util/virjson.h       |  6 ++++++
 3 files changed, 43 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 55b6a24..010a895 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1762,6 +1762,7 @@ virISCSIScanTargets;

 # util/virjson.h
 virJSONValueArrayAppend;
+virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
 virJSONValueArraySize;
 virJSONValueArraySteal;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 1d8e6d5..989758a 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -949,6 +949,42 @@ virJSONValueArraySteal(virJSONValuePtr array,
 }


+/**
+ * virJSONValueArrayForeachSteal:
+ * @array: array to iterate
+ * @cb: callback called on every member of the array
+ * @opaque: custom data for the callback
+ *
+ * Iterates members of the array and calls the callback on every single member.
+ * By returning success, the array claims ownership of given array element and
+ * is responsible for freeing it.
+ *
+ * Returns 0 if all members were successfully stolen by the callback; -1
+ * otherwise. The rest of the members stays in posession of the array.
+ */
+int
+virJSONValueArrayForeachSteal(virJSONValuePtr array,
+                              virJSONArrayIteratorFunc cb,
+                              void *opaque)
+{
+    ssize_t i;
+
+    if (array->type != VIR_JSON_TYPE_ARRAY)
+        return -1;
+
+    for (i = array->data.array.nvalues - 1; i >= 0; i--) {
+        if (cb(i, array->data.array.values[i], opaque) < 0)
+            break;
+
+        array->data.array.values[i] = NULL;
+    }
+
+    array->data.array.nvalues = i + 1;
+
+    return i < 0 ? 0 : -1;
+}
+
+
 const char *
 virJSONValueGetString(virJSONValuePtr string)
 {
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 8b62d65..5b4f172 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -117,6 +117,12 @@ bool virJSONValueIsArray(virJSONValuePtr array);
 ssize_t virJSONValueArraySize(const virJSONValue *array);
 virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr object, unsigned int element);
 virJSONValuePtr virJSONValueArraySteal(virJSONValuePtr object, unsigned int element);
+typedef int (*virJSONArrayIteratorFunc)(size_t pos,
+                                        virJSONValuePtr item,
+                                        void *opaque);
+int virJSONValueArrayForeachSteal(virJSONValuePtr array,
+                                  virJSONArrayIteratorFunc cb,
+                                  void *opaque);

 int virJSONValueObjectKeysNumber(virJSONValuePtr object);
 const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n);
-- 
2.10.0

--
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]