summaryrefslogtreecommitdiff
path: root/json_pointer.c
diff options
context:
space:
mode:
authordota17 <chenguopingdota@163.com>2020-03-28 10:25:00 +0800
committerdota17 <chenguopingdota@163.com>2020-04-03 11:39:30 +0800
commit8b162c4b896e5b40feb587aedb7c4f687d14dc67 (patch)
tree1404f3ba6994caf33f5147c793307c2569ca3226 /json_pointer.c
parentc117d8a8a83d5bcdd433b05e1846ccb73eb3413d (diff)
downloadjson-c-8b162c4b896e5b40feb587aedb7c4f687d14dc67.tar.gz
clang-format the files
Diffstat (limited to 'json_pointer.c')
-rw-r--r--json_pointer.c105
1 files changed, 65 insertions, 40 deletions
diff --git a/json_pointer.c b/json_pointer.c
index b2c8526..99cc542 100644
--- a/json_pointer.c
+++ b/json_pointer.c
@@ -10,11 +10,11 @@
#include "strerror_override.h"
+#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include "json_pointer.h"
#include "strdup_compat.h"
@@ -30,7 +30,8 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur,
int slen = strlen(s);
int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
char *p = s;
- while ((p = strstr(p, occur))) {
+ while ((p = strstr(p, occur)))
+ {
*p = repl_char;
p++;
slen -= skip;
@@ -44,8 +45,10 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx
/* this code-path optimizes a bit, for when we reference the 0-9 index range
* in a JSON array and because leading zeros not allowed
*/
- if (len == 1) {
- if (isdigit((unsigned char)path[0])) {
+ if (len == 1)
+ {
+ if (isdigit((unsigned char)path[0]))
+ {
*idx = (path[0] - '0');
goto check_oob;
}
@@ -53,26 +56,31 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx
return 0;
}
/* leading zeros not allowed per RFC */
- if (path[0] == '0') {
+ if (path[0] == '0')
+ {
errno = EINVAL;
return 0;
}
/* RFC states base-10 decimals */
- for (i = 0; i < len; i++) {
- if (!isdigit((unsigned char)path[i])) {
+ for (i = 0; i < len; i++)
+ {
+ if (!isdigit((unsigned char)path[i]))
+ {
errno = EINVAL;
return 0;
}
}
*idx = strtol(path, NULL, 10);
- if (*idx < 0) {
+ if (*idx < 0)
+ {
errno = EINVAL;
return 0;
}
check_oob:
len = json_object_array_length(jo);
- if (*idx >= len) {
+ if (*idx >= len)
+ {
errno = ENOENT;
return 0;
}
@@ -80,14 +88,17 @@ check_oob:
return 1;
}
-static int json_pointer_get_single_path(struct json_object *obj, char *path, struct json_object **value)
+static int json_pointer_get_single_path(struct json_object *obj, char *path,
+ struct json_object **value)
{
- if (json_object_is_type(obj, json_type_array)) {
+ if (json_object_is_type(obj, json_type_array))
+ {
int32_t idx;
if (!is_valid_index(obj, path, &idx))
return -1;
obj = json_object_array_get_idx(obj, idx);
- if (obj) {
+ if (obj)
+ {
if (value)
*value = obj;
return 0;
@@ -101,7 +112,8 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path, str
string_replace_all_occurrences_with_char(path, "~1", '/');
string_replace_all_occurrences_with_char(path, "~0", '~');
- if (!json_object_object_get_ex(obj, path, value)) {
+ if (!json_object_object_get_ex(obj, path, value))
+ {
errno = ENOENT;
return -1;
}
@@ -109,12 +121,11 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path, str
return 0;
}
-static int json_pointer_set_single_path(
- struct json_object *parent,
- const char *path,
- struct json_object *value)
+static int json_pointer_set_single_path(struct json_object *parent, const char *path,
+ struct json_object *value)
{
- if (json_object_is_type(parent, json_type_array)) {
+ if (json_object_is_type(parent, json_type_array))
+ {
int32_t idx;
/* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
if (path[0] == '-' && path[1] == '\0')
@@ -137,16 +148,15 @@ static int json_pointer_set_single_path(
return -1;
}
-static int json_pointer_get_recursive(
- struct json_object *obj,
- char *path,
- struct json_object **value)
+static int json_pointer_get_recursive(struct json_object *obj, char *path,
+ struct json_object **value)
{
char *endp;
int rc;
/* All paths (on each recursion level must have a leading '/' */
- if (path[0] != '/') {
+ if (path[0] != '/')
+ {
errno = EINVAL;
return -1;
}
@@ -160,7 +170,8 @@ static int json_pointer_get_recursive(
if ((rc = json_pointer_get_single_path(obj, path, &obj)))
return rc;
- if (endp) {
+ if (endp)
+ {
/* Put the slash back, so that the sanity check passes on next recursion level */
*endp = '/';
return json_pointer_get_recursive(obj, endp, value);
@@ -178,19 +189,22 @@ int json_pointer_get(struct json_object *obj, const char *path, struct json_obje
char *path_copy = NULL;
int rc;
- if (!obj || !path) {
+ if (!obj || !path)
+ {
errno = EINVAL;
return -1;
}
- if (path[0] == '\0') {
+ if (path[0] == '\0')
+ {
if (res)
*res = obj;
return 0;
}
/* pass a working copy to the recursive call */
- if (!(path_copy = strdup(path))) {
+ if (!(path_copy = strdup(path)))
+ {
errno = ENOMEM;
return -1;
}
@@ -206,7 +220,8 @@ int json_pointer_getf(struct json_object *obj, struct json_object **res, const c
int rc = 0;
va_list args;
- if (!obj || !path_fmt) {
+ if (!obj || !path_fmt)
+ {
errno = EINVAL;
return -1;
}
@@ -218,7 +233,8 @@ int json_pointer_getf(struct json_object *obj, struct json_object **res, const c
if (rc < 0)
return rc;
- if (path_copy[0] == '\0') {
+ if (path_copy[0] == '\0')
+ {
if (res)
*res = obj;
goto out;
@@ -238,30 +254,35 @@ int json_pointer_set(struct json_object **obj, const char *path, struct json_obj
struct json_object *set = NULL;
int rc;
- if (!obj || !path) {
+ if (!obj || !path)
+ {
errno = EINVAL;
return -1;
}
- if (path[0] == '\0') {
+ if (path[0] == '\0')
+ {
json_object_put(*obj);
*obj = value;
return 0;
}
- if (path[0] != '/') {
+ if (path[0] != '/')
+ {
errno = EINVAL;
return -1;
}
/* If there's only 1 level to set, stop here */
- if ((endp = strrchr(path, '/')) == path) {
+ if ((endp = strrchr(path, '/')) == path)
+ {
path++;
return json_pointer_set_single_path(*obj, path, value);
}
/* pass a working copy to the recursive call */
- if (!(path_copy = strdup(path))) {
+ if (!(path_copy = strdup(path)))
+ {
errno = ENOMEM;
return -1;
}
@@ -276,7 +297,8 @@ int json_pointer_set(struct json_object **obj, const char *path, struct json_obj
return json_pointer_set_single_path(set, endp, value);
}
-int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt, ...)
+int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
+ ...)
{
char *endp;
char *path_copy = NULL;
@@ -284,7 +306,8 @@ int json_pointer_setf(struct json_object **obj, struct json_object *value, const
va_list args;
int rc = 0;
- if (!obj || !path_fmt) {
+ if (!obj || !path_fmt)
+ {
errno = EINVAL;
return -1;
}
@@ -297,20 +320,23 @@ int json_pointer_setf(struct json_object **obj, struct json_object *value, const
if (rc < 0)
return rc;
- if (path_copy[0] == '\0') {
+ if (path_copy[0] == '\0')
+ {
json_object_put(*obj);
*obj = value;
goto out;
}
- if (path_copy[0] != '/') {
+ if (path_copy[0] != '/')
+ {
errno = EINVAL;
rc = -1;
goto out;
}
/* If there's only 1 level to set, stop here */
- if ((endp = strrchr(path_copy, '/')) == path_copy) {
+ if ((endp = strrchr(path_copy, '/')) == path_copy)
+ {
set = *obj;
goto set_single_path;
}
@@ -328,4 +354,3 @@ out:
free(path_copy);
return rc;
}
-