[PATCH v2 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.
---

Notes:
    v2:
    - iterate from beginning
    - add option to keep array member in the array without breaking iteration
    - documented callback return codes

 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virjson.h       |  6 ++++++
 3 files changed, 61 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 162fda5..4c08508 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1764,6 +1764,7 @@ virISCSIScanTargets;

 # util/virjson.h
 virJSONValueArrayAppend;
+virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
 virJSONValueArraySize;
 virJSONValueArraySteal;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 1d8e6d5..d55600a 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -949,6 +949,60 @@ 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.
+ * The return codes of the callback are interpreted as follows:
+ * 0: callback claims ownership of the array element and is responsible for
+ *    freeing it
+ * 1: callback does not claim the ownership, but the iteration continues
+ * -1: callback doesn't claim ownership and iteration does not continue
+ *
+ * Returns 0 if all members were iterated and/or stolen by the callback; -1
+ * on callback failure or if the JSON value object is not an array.
+ * The rest of the members stay in possession of the array and it's condensed.
+ */
+int
+virJSONValueArrayForeachSteal(virJSONValuePtr array,
+                              virJSONArrayIteratorFunc cb,
+                              void *opaque)
+{
+    size_t i;
+    size_t j = 0;
+    int ret = 0;
+    int rc;
+
+    if (array->type != VIR_JSON_TYPE_ARRAY)
+        return -1;
+
+    for (i = 0; i < array->data.array.nvalues; i++) {
+        if ((rc = cb(i, array->data.array.values[i], opaque)) < 0) {
+            ret = -1;
+            break;
+        }
+
+        if (rc == 0)
+            array->data.array.values[i] = NULL;
+    }
+
+    /* condense the remaining entries at the beginning */
+    for (i = 0; i < array->data.array.nvalues; i++) {
+        if (!array->data.array.values[i])
+            continue;
+
+        array->data.array.values[j++] = array->data.array.values[i];
+    }
+
+    array->data.array.nvalues = j;
+
+    return ret;
+}
+
+
 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.2

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