aboutsummaryrefslogtreecommitdiffstats
path: root/wrap-json.c
diff options
context:
space:
mode:
authorJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-08 15:54:31 +0200
committerJonathan Aillet <jonathan.aillet@iot.bzh>2019-10-09 15:42:15 +0200
commit5b9cce9fd0a1da9aeaaba8465f9a2780282a2247 (patch)
tree1ff4f8f7b9cbfc0883b2b158efc1605db6605070 /wrap-json.c
parent25bb4512a2c2bc66f9eb255a872600f67bd4943f (diff)
Add function to add one json array to another oneicefish_8.99.1icefish/8.99.18.99.1
Add a function to add one json array to another one. The json array to add can be inserted at any place of the json array to complete using 'idx' parameter. Also, correct minor mistake in 'wrap_json_object_add' description. BUG-AGL: SPEC-2867 Change-Id: I8936f298a4499f265646a75584d7fe6b29700513 Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
Diffstat (limited to 'wrap-json.c')
-rw-r--r--wrap-json.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/wrap-json.c b/wrap-json.c
index 27940be..0e0c31c 100644
--- a/wrap-json.c
+++ b/wrap-json.c
@@ -1031,7 +1031,7 @@ struct json_object *wrap_json_clone_deep(struct json_object *object)
* Adds the items of the object 'added' to the object 'dest'.
*
* @param dest the object to complete this object is modified
- * @added the object containing fields to add
+ * @param added the object containing fields to add
*
* @return the destination object 'dest'
*
@@ -1054,6 +1054,46 @@ struct json_object *wrap_json_object_add(struct json_object *dest, struct json_o
}
/**
+ * Insert content of the array 'added' at index 'idx' of 'dest' array.
+ *
+ * @param dest the array to complete, this array is modified
+ * @param added the array containing content to add
+ * @param idx the index where the 'added' array content will be inserted into
+ * 'dest' array. To insert 'added' array at the end of 'dest' array,
+ * you can set 'idx' to:
+ * - a negative value.
+ * - a valued bigger than 'dest' array length.
+ *
+ * @return the destination array 'dest'
+ *
+ * @example wrap_json_array_insert(["a","b",5],["X","Y",0.92], 1) -> ["a","X","Y",0.92,"b",5]
+ */
+struct json_object *wrap_json_array_insert_array(struct json_object *dest, struct json_object *added, int idx)
+{
+ int i, nd, na;
+ if (!json_object_is_type(dest, json_type_array) || !json_object_is_type(added, json_type_array))
+ return dest;
+ nd = (int) json_object_array_length(dest);
+ na = (int) json_object_array_length(added);
+ i = nd + na;
+ if (idx < 0 || idx > nd)
+ idx = nd;
+ while (i > idx + na) {
+ i--;
+ json_object_array_put_idx(dest,
+ i,
+ json_object_get(json_object_array_get_idx(dest, i - na)));
+ }
+ while (i > idx) {
+ i--;
+ json_object_array_put_idx(dest,
+ i,
+ json_object_get(json_object_array_get_idx(added, i - idx)));
+ }
+ return dest;
+}
+
+/**
* Sort the 'array' and returns it. Sorting is done accordingly to the
* order given by the function 'wrap_json_cmp'. If the paramater isn't
* an array, nothing is done and the parameter is returned unchanged.