From 00a29f004f18edc960a078a6887dc5c8af55598c Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Thu, 3 Oct 2019 13:33:01 +0200
Subject: [PATCH] cleanup and add some comments

---
 api.c | 168 +++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 101 insertions(+), 67 deletions(-)

diff --git a/api.c b/api.c
index 27717e6..803b45d 100644
--- a/api.c
+++ b/api.c
@@ -70,7 +70,7 @@ int json_object_obj_to_file(struct json_object *obj, const char *path)
 {
 	FILE *out;
     int rv = -1;
-	char *data;
+	const char *data;
 
 	if (!obj) {
 		fprintf(stderr, "%s error: No object provided!\n", __func__);
@@ -102,7 +102,7 @@ out:
     return rv;
 }
 
-int get_idx(char *key, int *idx, char *out)
+static int get_idx(char *key, int *idx, char *out)
 {
 	char *open_brace;
 	int len = 0;
@@ -139,7 +139,7 @@ int get_idx(char *key, int *idx, char *out)
 	return len;
 }
 
-struct json_object *dummy_val(char *val, enum json_type type)
+static struct json_object *dummy_val(char *val, enum json_type type)
 {
 	struct json_object *j_val;
 
@@ -170,39 +170,52 @@ struct json_object *dummy_val(char *val, enum json_type type)
 	return j_val;
 }
 
-int add_array(struct json_object *ptr, int *idx, int len, struct json_object *j_val, enum json_type type, char *key)
+/**
+ * If key to be added is indexed, makes sure specified key is an array and selects appropriate index.
+ * Should the key not be an array, it will be converted to an array and appropriate indexes will be prepared.
+ *
+ */
+static int iterate_array(struct json_object *src, int *idx, int len, struct json_object *j_val, enum json_type type, char *key)
 {
-	struct json_object *tmp, *tmp1;
+	struct json_object *val;
 
 	if (len < 1)
 		return -1;
 
 	if (!j_val)
 		return -1;
-	json_object_object_get_ex(ptr, key, &tmp);
-	if (!tmp || !json_object_is_type(tmp, json_type_array)) {
-		tmp = json_object_new_array();
-		json_object_object_add(ptr, key, tmp);
+
+	/* select key, if it does not exist or is not an array, create one */
+	json_object_object_get_ex(src, key, &val);
+	if (!val || !json_object_is_type(val, json_type_array)) {
+		val = json_object_new_array();
+		json_object_object_add(src, key, val);
 	}
 
+	/* iterate every index that was appended to the key */
 	for (int i = 0; i < len; i++) {
-		struct json_object *tmp1;
+		struct json_object *j_idx;
+
+		/* select specified idx from currently selected array */
+		j_idx = json_object_array_get_idx(val, idx[i]);
 
-		tmp1 = json_object_array_get_idx(tmp, idx[i]);
+		/* if we are not at final idx, make sure next entry is an array */
 		if (i < len -1) {
-			if (!tmp1 || !json_object_is_type(tmp1, json_type_array)) {
-				tmp1 = json_object_new_array();
+			if (!j_idx || !json_object_is_type(j_idx, json_type_array)) {
+				j_idx = json_object_new_array();
 				if (idx[i] > -1)
-					json_object_array_put_idx(tmp, idx[i], tmp1);
+					json_object_array_put_idx(val, idx[i], j_idx);
 				else
-					json_object_array_add(tmp, tmp1);
+					json_object_array_add(val, j_idx);
 			}
-			tmp = tmp1;
-		} else {
+			val = j_idx;
+		}
+		/* at final index, place passed object at specified idx */
+		else {
 			if (idx[i] > -1)
-				json_object_array_put_idx(tmp, idx[i], j_val);
+				json_object_array_put_idx(val, idx[i], j_val);
 			else
-				json_object_array_add(tmp, j_val);
+				json_object_array_add(val, j_val);
 		}
 
 	}
@@ -210,7 +223,19 @@ int add_array(struct json_object *ptr, int *idx, int len, struct json_object *j_
 	return 0;
 }
 
-int add_val(struct json_object *ptr, char *key, char *val, enum json_type type)
+static void copy_object_into_object(struct json_object *src, struct json_object *dst)
+{
+	json_object_object_foreach(src, key, val)
+		json_object_object_add(dst, key, json_object_get(val));
+
+	json_object_put(src);
+}
+
+/**
+ * Add a value key pair to src object
+ *
+ */
+static int add_val(struct json_object *src, char *key, char *val, enum json_type type)
 {
 	struct json_object *j_val;
 	int idx[32];
@@ -225,32 +250,36 @@ int add_val(struct json_object *ptr, char *key, char *val, enum json_type type)
 		return -1;
 	}
 
+	/* if input object is of type object we need to iterate the object and copy {key:value} into specified key */
 	if (json_type_object == type) {
 		if (len < 1) {
-			struct json_object *tmp = ptr;
+			/* if a key is provided, place input under that key, else place in root */
+			if (key) {
+				struct json_object *tmp;
 
-			if (key)
-				ptr = json_object_new_object();
+				tmp = json_object_new_object();
+				if (!tmp)
+					return -1;
 
-			json_object_object_foreach(j_val, key1, val1)
-				json_object_object_add(ptr, key1, json_object_get(val1));
-			if (key) {
-				json_object_object_add(tmp, parsed_key, ptr);
-			}
-			json_object_put(j_val);
-		} else {
-			add_array(ptr, idx, len, j_val, type, parsed_key);
-		}
+				copy_object_into_object(j_val, tmp);
+				json_object_object_add(src, parsed_key, tmp);
+			} else
+				copy_object_into_object(j_val, src);
+
+		} else
+			iterate_array(src, idx, len, j_val, type, parsed_key);
 
-		return 0;
+		goto out;
 	}
 
+	/* object type is not an object, attach to provided key */
 	if (len < 1 || type == json_type_array)
-		json_object_object_add(ptr, parsed_key, j_val);
+		json_object_object_add(src, parsed_key, j_val);
 	else {
-		add_array(ptr, idx, len, j_val, type, parsed_key);
+		iterate_array(src, idx, len, j_val, type, parsed_key);
 	}
 
+out:
 	return 0;
 }
 
@@ -259,9 +288,13 @@ int json_object_set_by_string(struct json_object **src, char *fmt, char *val, en
 	return json_object_set_by_string_delimiter(src, fmt, val, type, ".");
 }
 
+/**
+ * Generate object according to javascript style format
+ *
+ */
 int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, char *val, enum json_type type, const char *delimiter)
 {
-	struct json_object *ptr, *outer_obj, *tmp;
+	struct json_object *ptr, *outer_obj, *j_idx;
 	char fmt_cpy[1024] = {0}, parsed_key[32] = {0};
 	char *p, *key = fmt;
 	int idx[32];
@@ -289,47 +322,48 @@ int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, cha
 
 			len = get_idx(key, idx, parsed_key);
 
-			/* if we need to step further and currently marked value isnt an object, we need to overwrite it */
+			/* as next key exists (p), we muts create an object here if it is currently not an object */
 			json_object_object_get_ex(outer_obj, parsed_key, &ptr);
 			if (!json_object_is_type(ptr, json_type_object) && !json_object_is_type(ptr, json_type_array)) {
+				/* we know its an object because it is not the last key */
 				ptr = json_object_new_object();
 				json_object_object_add(outer_obj, parsed_key, ptr);
 			}
 
-			if (len > 0) {
-				for (int i = 0; i < len; i++) {
-					if (ptr && json_object_get_type(ptr) == json_type_array) {
-						if (idx[i] > -1 && idx[i] < json_object_array_length(ptr)) {
-							tmp = json_object_array_get_idx(ptr, idx[i]);
-							if (!json_object_is_type(tmp, json_type_object)) {
-								struct json_object *obj = json_object_new_object();
-								if (!obj)
-									return -1;
-
-								json_object_array_put_idx(ptr, idx[i], obj);
-								tmp = json_object_array_get_idx(ptr, idx[i]);
-							}
-							ptr = tmp;
+			/* if the key is indexed, we need step into the array */
+			for (int i = 0; i < len; i++) {
+				/* make sure ptr is an array, step into specified index */
+				if (ptr && json_object_get_type(ptr) == json_type_array) {
+					if (idx[i] > -1 && idx[i] < json_object_array_length(ptr)) {
+						j_idx = json_object_array_get_idx(ptr, idx[i]);
+						if (!json_object_is_type(j_idx, json_type_object)) {
+							struct json_object *obj = json_object_new_object();
+							if (!obj)
+								return -1;
+
+							json_object_array_put_idx(ptr, idx[i], obj);
+							j_idx = json_object_array_get_idx(ptr, idx[i]);
 						}
-						else {
-							if (i == len - 1)
-								json_object_array_add(ptr, json_object_new_object());
-							else if (i < len -1)
-								json_object_array_add(ptr, json_object_new_array());
-							ptr = json_object_array_get_idx(ptr, json_object_array_length(ptr) - 1);
-						}
-					} else {
-						ptr = json_object_new_array();
-						json_object_object_add(outer_obj, parsed_key, ptr);
-						if (i == len - 1) {
+						ptr = j_idx;
+					}
+					else {
+						if (i == len - 1)
 							json_object_array_add(ptr, json_object_new_object());
-							ptr = json_object_array_get_idx(ptr, json_object_array_length(ptr) - 1);
-						}
+						else if (i < len -1)
+							json_object_array_add(ptr, json_object_new_array());
+						ptr = json_object_array_get_idx(ptr, json_object_array_length(ptr) - 1);
+					}
+				} else {
+					ptr = json_object_new_array();
+					json_object_object_add(outer_obj, parsed_key, ptr);
+					if (i == len - 1) {
+						json_object_array_add(ptr, json_object_new_object());
+						ptr = json_object_array_get_idx(ptr, json_object_array_length(ptr) - 1);
 					}
 				}
 			}
 
-			/* create key object if it does not exist */
+			/* if this point is reached and ptr is not set, a key is specified which does not exist - create it */
 			if (!ptr) {
 				ptr = json_object_new_object();
 				if (json_object_get_type(outer_obj) == json_type_array)
@@ -379,8 +413,8 @@ int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_ob
 		tmp = ptr;
 	}
 
-	json_object_object_foreach(val, key, val1)
-		json_object_object_add(src, key, val1);
+	json_object_object_foreach(val, key, j_val)
+		json_object_object_add(src, key, j_val);
 
 	return 0;
 }
-- 
GitLab