From 36d364780c129f1dc18e29de1ca3eff1be463642 Mon Sep 17 00:00:00 2001 From: Jakob Olsson <jakob.olsson@iopsys.eu> Date: Fri, 4 Oct 2019 10:04:50 +0200 Subject: [PATCH] json-editor: checkpatch style fixes --- src/json-editor.c | 123 ++++++++++++++++++++++++++++------------------ src/json-editor.h | 18 ++++--- 2 files changed, 88 insertions(+), 53 deletions(-) diff --git a/src/json-editor.c b/src/json-editor.c index d809049..72b7489 100644 --- a/src/json-editor.c +++ b/src/json-editor.c @@ -14,7 +14,7 @@ static char *get_file(const char *path) { FILE *f; size_t nread; - long int len; + long len; char *buffer; f = fopen(path, "r"); @@ -69,7 +69,7 @@ out: int json_object_obj_to_file(struct json_object *obj, const char *path) { FILE *out; - int rv = -1; + int rv = -1; const char *data; if (!obj) { @@ -88,18 +88,18 @@ int json_object_obj_to_file(struct json_object *obj, const char *path) goto out; } - out = fopen (path, "ab+"); - if (out != NULL) { - if (fputs (data, out) != EOF) + out = fopen(path, "ab+"); + if (out != NULL) { + if (fputs(data, out) != EOF) goto out_fclose; rv = 0; - } + } out_fclose: - fclose (out); + fclose(out); out: - return rv; + return rv; } static int get_idx(char *key, int *idx, char *out) @@ -109,14 +109,14 @@ static int get_idx(char *key, int *idx, char *out) char buffer[64] = {0}; char *ptr; - if(!key) + if (!key) return -1; strncpy(buffer, key, sizeof(buffer) - 1); ptr = (char *)buffer; - while((open_brace = strchr(ptr, '['))) { + while ((open_brace = strchr(ptr, '['))) { char *close_brace = strchr(open_brace, ']'); if (close_brace) { @@ -143,12 +143,13 @@ static struct json_object *dummy_val(char *val, enum json_type type) { struct json_object *j_val; - switch(type) { + switch (type) { case json_type_array: j_val = json_tokener_parse(val); break; case json_type_boolean: - ;; + /* TODO: boolean support */ + ; break; case json_type_object: j_val = json_tokener_parse(val); @@ -171,11 +172,13 @@ static struct json_object *dummy_val(char *val, enum json_type type) } /** - * 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. + * 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, char *key) +static int iterate_array(struct json_object *src, int *idx, int len, + struct json_object *j_val, char *key) { struct json_object *val; @@ -200,7 +203,7 @@ static int iterate_array(struct json_object *src, int *idx, int len, struct json j_idx = json_object_array_get_idx(val, idx[i]); /* if we are not at final idx, make sure next entry is an array */ - if (i < len -1) { + if (i < len - 1) { if (!j_idx || !json_object_is_type(j_idx, json_type_array)) { j_idx = json_object_new_array(); if (idx[i] > -1) @@ -223,7 +226,8 @@ static int iterate_array(struct json_object *src, int *idx, int len, struct json return 0; } -static void copy_object_into_object(struct json_object *src, struct json_object *dst) +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)); @@ -235,7 +239,8 @@ static void copy_object_into_object(struct json_object *src, struct json_object * Add a value key pair to src object * */ -static int add_val(struct json_object *src, char *key, char *val, enum json_type type) +static int add_val(struct json_object *src, char *key, char *val, + enum json_type type) { struct json_object *j_val; int idx[32]; @@ -245,15 +250,20 @@ static int add_val(struct json_object *src, char *key, char *val, enum json_type len = get_idx(key, idx, parsed_key); j_val = dummy_val(val, type); if (!j_val || !json_object_is_type(j_val, type)) { - fprintf(stderr, "Invalid input value, parsed value and input type does not match!\n"); + fprintf(stderr, "Invalid input value, parsed value and input type does " + "not match!\n"); json_object_put(j_val); return -1; } - /* if input object is of type object we need to iterate the object and copy {key:value} into specified key */ + /* 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) { - /* if a key is provided, place input under that key, else place in root */ + /* if a key is provided, place input under that key, else place in + * root + */ if (key) { struct json_object *j_new; @@ -275,9 +285,8 @@ static int add_val(struct json_object *src, char *key, char *val, enum json_type /* object type is not an object, attach to provided key */ if (len < 1 || type == json_type_array) json_object_object_add(src, parsed_key, j_val); - else { + else iterate_array(src, idx, len, j_val, parsed_key); - } out: return 0; @@ -309,7 +318,7 @@ static int del_val(struct json_object *src, char *key) j_idx = json_object_array_get_idx(val, idx[i]); /* if we are not at final idx, make sure next entry is an array */ - if (i < len -1) { + if (i < len - 1) { if (!j_idx || !json_object_is_type(j_idx, json_type_array)) { fprintf(stderr, "%s error: type is not array!\n", __func__); goto out; @@ -321,7 +330,8 @@ static int del_val(struct json_object *src, char *key) if (idx[i] > -1) json_object_array_del_idx(val, idx[i], 1); else - json_object_array_del_idx(val, json_object_array_length(val) - 1, 1); + json_object_array_del_idx(val, + json_object_array_length(val) - 1, 1); rv = 0; } @@ -332,7 +342,8 @@ out: return rv; } -int json_object_set_by_string(struct json_object **src, char *fmt, char *val, enum json_type type) +int json_object_set_by_string(struct json_object **src, char *fmt, char *val, + enum json_type type) { return json_object_set_by_string_delimiter(src, fmt, val, type, "."); } @@ -341,7 +352,8 @@ int json_object_set_by_string(struct json_object **src, char *fmt, char *val, en * 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) +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, *j_idx; char fmt_cpy[1024] = {0}, parsed_key[32] = {0}; @@ -357,7 +369,7 @@ int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, cha goto add_key; strcpy(fmt_cpy, fmt); - p = strtok(fmt_cpy,delimiter); + p = strtok(fmt_cpy, delimiter); while (p != NULL) { key = p; @@ -369,9 +381,12 @@ int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, cha len = get_idx(key, idx, parsed_key); - /* as next key exists (p), we muts create an object here if it is currently not an object */ + /* 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)) { + 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); @@ -384,7 +399,9 @@ int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, cha 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(); + struct json_object *obj; + + obj = json_object_new_object(); if (!obj) return -1; @@ -392,25 +409,29 @@ int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, cha j_idx = json_object_array_get_idx(ptr, idx[i]); } ptr = j_idx; - } - else { + } 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_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); + 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); + ptr = json_object_array_get_idx(ptr, + json_object_array_length(ptr) - 1); } } } - /* if this point is reached and ptr is not set, a key is specified which does not exist - create it */ + /* 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) @@ -434,7 +455,8 @@ int json_object_set(struct json_object *src, char *fmt, struct json_object *val) return json_object_set_delimiter(src, fmt, val, "."); } -int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_object *val, const char *delimiter) +int json_object_set_delimiter(struct json_object *src, char *fmt, + struct json_object *val, const char *delimiter) { char fmt_cpy[1024] = {0}; char *p; @@ -448,7 +470,8 @@ int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_ob strcpy(fmt_cpy, fmt); - for (p = strtok(fmt_cpy,delimiter); p != NULL; p = strtok(NULL, delimiter)) { + for (p = strtok(fmt_cpy, delimiter); p != NULL; + p = strtok(NULL, delimiter)) { struct json_object *ptr, *outer_obj = src; json_object_object_get_ex(outer_obj, p, &ptr); @@ -466,12 +489,14 @@ int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_ob return 0; } -struct json_object *json_object_get_by_string(struct json_object *src, char *fmt) +struct json_object *json_object_get_by_string(struct json_object *src, + char *fmt) { return json_object_get_by_string_delimiter(src, fmt, "."); } -struct json_object *json_object_get_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter) +struct json_object *json_object_get_by_string_delimiter(struct json_object *src, + char *fmt, const char *delimiter) { struct json_object *ptr, *outer_obj = src; char fmt_cpy[1024] = {0}; @@ -480,7 +505,8 @@ struct json_object *json_object_get_by_string_delimiter(struct json_object *src, strcpy(fmt_cpy, fmt); - for (char *key = strtok(fmt_cpy,delimiter); key != NULL; key = strtok(NULL, delimiter)) { + for (char *key = strtok(fmt_cpy, delimiter); key != NULL; + key = strtok(NULL, delimiter)) { int len = 0; len = get_idx(key, idx, parsed_key); @@ -490,7 +516,8 @@ struct json_object *json_object_get_by_string_delimiter(struct json_object *src, if (!json_object_is_type(ptr, json_type_array)) return NULL; - int index = (idx[i] == -1 ? json_object_array_length(ptr) -1 : idx[i]); + int index = (idx[i] == -1 ? json_object_array_length(ptr) - 1 : + idx[i]); ptr = json_object_array_get_idx(ptr, index); } @@ -507,7 +534,8 @@ int json_object_del_by_string(struct json_object *src, char *fmt) } -int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter) +int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, + const char *delimiter) { struct json_object *ptr, *outer_obj; char fmt_cpy[1024] = {0}, parsed_key[32] = {0}; @@ -527,7 +555,7 @@ int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, cons ptr = outer_obj = src; strcpy(fmt_cpy, fmt); - p = strtok(fmt_cpy,delimiter); + p = strtok(fmt_cpy, delimiter); while (p != NULL) { key = p; @@ -543,7 +571,8 @@ int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, cons if (!json_object_is_type(ptr, json_type_array)) return -1; - int index = (idx[i] == -1 ? json_object_array_length(ptr) -1 : idx[i]); + int index = (idx[i] == -1 ? json_object_array_length(ptr) - 1 : + idx[i]); ptr = json_object_array_get_idx(ptr, index); } diff --git a/src/json-editor.h b/src/json-editor.h index 35b030e..d70848e 100644 --- a/src/json-editor.h +++ b/src/json-editor.h @@ -10,11 +10,17 @@ struct json_object *json_object_file_to_obj(const char *path); int json_object_obj_to_file(struct json_object *obj, const char *path); -int json_object_set_by_string( struct json_object **src, char *fmt, char *val, enum json_type type); -int json_object_set_by_string_delimiter( struct json_object **src, char *fmt, char *val, enum json_type type, const char *delimiter); +int json_object_set_by_string(struct json_object **src, char *fmt, char *val, + enum json_type type); +int json_object_set_by_string_delimiter(struct json_object **src, char *fmt, + char *val, enum json_type type, const char *delimiter); int json_object_set(struct json_object *src, char *fmt, struct json_object *val); -int json_object_set_delimiter(struct json_object *src, char *fmt, struct json_object *val, const char *delimiter); -struct json_object *json_object_get_by_string(struct json_object *src, char *fmt); -struct json_object *json_object_get_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter); +int json_object_set_delimiter(struct json_object *src, char *fmt, + struct json_object *val, const char *delimiter); +struct json_object *json_object_get_by_string(struct json_object *src, + char *fmt); +struct json_object *json_object_get_by_string_delimiter(struct json_object *src, + char *fmt, const char *delimiter); int json_object_del_by_string(struct json_object *src, char *fmt); -int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, const char *delimiter); \ No newline at end of file +int json_object_del_by_string_delimiter(struct json_object *src, char *fmt, + const char *delimiter); -- GitLab