From cf97213334a2ca01b96d3b57989151b795b7ca66 Mon Sep 17 00:00:00 2001 From: Daniel Schubert Date: Fri, 7 Oct 2016 21:11:45 +0200 Subject: [PATCH] Adjusted to new formatting --- cJSON.c | 44 +++++++++++++++++++++++++++++--------------- cJSON.h | 2 ++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cJSON.c b/cJSON.c index 8396278..a79606c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -61,17 +61,20 @@ static int cJSON_strcasecmp(const char *s1, const char *s2) return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); } -static int cJSON_strncasecmp(const char *s1, const char *s2, int comparelen) +static int cJSON_strncasecmp(const char *s1, const char *s2, size_t comparelen) { - int i; - if (!s1) { + if (!s1) + { return (s1 == s2) ? 0 : 1; } - if (!s2) { + if (!s2) + { return 1; } - for (i = 0; tolower(*s1) == tolower(*s2); ++s1, ++s2, ++i) { - if (*s1 == 0 || i + 1 == comparelen) { + for (; tolower(*s1) == tolower(*s2); ++s1, ++s2, comparelen--) + { + if (*s1 == 0 || comparelen == 1) + { return 0; } } @@ -1632,33 +1635,44 @@ cJSON *cJSON_GetObjectItem(cJSON *object, const char *string) return c; } -int cJSON_HasObjectItem(cJSON *object,const char *string) +cJSON *cJSON_GetSizedObjectItem(cJSON *object, const char *string, size_t length) { - return cJSON_GetObjectItem(object, string) ? 1 : 0; -} - -cJSON *cJSON_GetSizedObjectItem(cJSON *object,const char *string, int length) { cJSON *c = object ? object->child : 0; while (c && cJSON_strncasecmp(c->string, string, length)) + { c = c->next; + } return c; } -cJSON *cJSON_GetDotObjectItem(cJSON *object,const char *string) { - int node_name_length; +cJSON *cJSON_GetDotObjectItem(cJSON *object, const char *string) +{ + size_t node_name_length; const char *position; position = strchr(string, '.'); - if (position != NULL) { + if (position != NULL) + { node_name_length = position - string; object = cJSON_GetSizedObjectItem(object, string, node_name_length); return cJSON_GetDotObjectItem(object, position + 1); } - else { + else + { return cJSON_GetObjectItem(object, string); } } +int cJSON_HasObjectItem(cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +int cJSON_HasDotObjectItem(cJSON *object, const char* string) +{ + return cJSON_GetDotObjectItem(object, string) ? 1 : 0; +} + /* Utility for array list handling. */ static void suffix_object(cJSON *prev, cJSON *item) { diff --git a/cJSON.h b/cJSON.h index 336c96e..91bf10d 100644 --- a/cJSON.h +++ b/cJSON.h @@ -92,6 +92,8 @@ extern cJSON *cJSON_GetArrayItem(cJSON *array, int item); extern cJSON *cJSON_GetObjectItem(cJSON *object, const char *string); extern cJSON *cJSON_GetDotObjectItem(cJSON *object, const char *string); extern int cJSON_HasObjectItem(cJSON *object, const char *string); +extern int cJSON_HasDotObjectItem(cJSON *object, const char* string); + /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ extern const char *cJSON_GetErrorPtr(void);