Adjusted to new formatting

This commit is contained in:
Daniel Schubert
2016-10-07 21:11:45 +02:00
parent 82cea68696
commit cf97213334
2 changed files with 31 additions and 15 deletions

66
cJSON.c
View File

@ -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); 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)
{
if (!s1)
{ {
int i;
if (!s1) {
return (s1 == s2) ? 0 : 1; return (s1 == s2) ? 0 : 1;
} }
if (!s2) { if (!s2)
{
return 1; return 1;
} }
for (i = 0; tolower(*s1) == tolower(*s2); ++s1, ++s2, ++i) { for (; tolower(*s1) == tolower(*s2); ++s1, ++s2, comparelen--)
if (*s1 == 0 || i + 1 == comparelen) { {
if (*s1 == 0 || comparelen == 1)
{
return 0; return 0;
} }
} }
@ -1632,31 +1635,42 @@ cJSON *cJSON_GetObjectItem(cJSON *object, const char *string)
return c; return c;
} }
cJSON *cJSON_GetSizedObjectItem(cJSON *object, const char *string, size_t 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)
{
size_t node_name_length;
const char *position;
position = strchr(string, '.');
if (position != NULL)
{
node_name_length = position - string;
object = cJSON_GetSizedObjectItem(object, string, node_name_length);
return cJSON_GetDotObjectItem(object, position + 1);
}
else
{
return cJSON_GetObjectItem(object, string);
}
}
int cJSON_HasObjectItem(cJSON *object, const char *string) int cJSON_HasObjectItem(cJSON *object, const char *string)
{ {
return cJSON_GetObjectItem(object, string) ? 1 : 0; return cJSON_GetObjectItem(object, string) ? 1 : 0;
} }
cJSON *cJSON_GetSizedObjectItem(cJSON *object,const char *string, int length) { int cJSON_HasDotObjectItem(cJSON *object, const char* string)
cJSON *c = object ? object->child : 0; {
while (c && cJSON_strncasecmp(c->string, string, length)) return cJSON_GetDotObjectItem(object, string) ? 1 : 0;
c = c->next;
return c;
}
cJSON *cJSON_GetDotObjectItem(cJSON *object,const char *string) {
int node_name_length;
const char *position;
position = strchr(string, '.');
if (position != NULL) {
node_name_length = position - string;
object = cJSON_GetSizedObjectItem(object, string, node_name_length);
return cJSON_GetDotObjectItem(object, position + 1);
}
else {
return cJSON_GetObjectItem(object, string);
}
} }
/* Utility for array list handling. */ /* Utility for array list handling. */

View File

@ -92,6 +92,8 @@ extern cJSON *cJSON_GetArrayItem(cJSON *array, int item);
extern cJSON *cJSON_GetObjectItem(cJSON *object, const char *string); extern cJSON *cJSON_GetObjectItem(cJSON *object, const char *string);
extern cJSON *cJSON_GetDotObjectItem(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_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. */ /* 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); extern const char *cJSON_GetErrorPtr(void);