[PATCH 09/19] util: json: Introduce virJSONValueArrayConcat

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

 



Add a helper that concatenates the second array into the first.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 31 +++++++++++++++++++++++++++++++
 src/util/virjson.h       |  2 ++
 3 files changed, 34 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ebf830791e..b2d2420415 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2284,6 +2284,7 @@ virISCSIScanTargets;
 virJSONStringReformat;
 virJSONValueArrayAppend;
 virJSONValueArrayAppendString;
+virJSONValueArrayConcat;
 virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
 virJSONValueArraySize;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 988a09e956..50993648eb 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -811,6 +811,37 @@ virJSONValueArrayAppendString(virJSONValuePtr object,
 }


+/**
+ * virJSONValueArrayConcat:
+ * @a: JSON value array (destination)
+ * @c: JSON value array (source)
+ *
+ * Merges the members of @c array into @a. The values are stolen from @c.
+ */
+int
+virJSONValueArrayConcat(virJSONValuePtr a,
+                        virJSONValuePtr c)
+{
+    size_t i;
+
+    if (a->type != VIR_JSON_TYPE_ARRAY ||
+        c->type != VIR_JSON_TYPE_ARRAY) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array"));
+        return -1;
+    }
+
+    a->data.array.values = g_renew(virJSONValuePtr, a->data.array.values,
+                                   a->data.array.nvalues + c->data.array.nvalues);
+
+    for (i = 0; i < c->data.array.nvalues; i++)
+        a->data.array.values[a->data.array.nvalues++] = g_steal_pointer(&c->data.array.values[i]);
+
+    c->data.array.nvalues = 0;
+
+    return 0;
+}
+
+
 int
 virJSONValueObjectHasKey(virJSONValuePtr object,
                          const char *key)
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 7a6b063b17..0894e91b59 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -71,6 +71,8 @@ virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);

 int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr value);
 int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
+int virJSONValueArrayConcat(virJSONValuePtr a,
+                            virJSONValuePtr c);

 int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key);
 virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
-- 
2.24.1





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

  Powered by Linux