cJSON_Utils.c 1.0 KB
Newer Older
1
#include <ctype.h>
2 3
#include "cJSON_Utils.h"

4 5 6 7 8 9 10 11 12 13 14 15
static int cJSONUtils_Pstrcasecmp(const char *a,const char *e)
{
	if (!a || !e) return (a==e)?0:1;
	for (;*a && *e && *e!='/';a++,e++) {
		if (*e=='~') {if (!(e[1]=='0' && *a=='~') && !(e[1]=='1' && *a=='/')) return 1;  else e++;}
		else if (tolower(*a)!=tolower(*e)) return 1;
	}
	if ((*e!=0 && *e!='/') != (*a!=0)) return 1;
	return 0;
}


16 17
cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
{
18
	cJSON *target=object;int which=0;const char *element=0;
19 20 21 22 23 24
	
	while (*pointer=='/' && object)
	{
		pointer++;
		if (object->type==cJSON_Array)
		{
25
			which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
26 27 28 29 30
			if (*pointer && *pointer!='/') return 0;
			object=cJSON_GetArrayItem(object,which);
		}
		else if (object->type==cJSON_Object)
		{
31 32
			element=pointer; while (*pointer && *pointer!='/') pointer++;
			object=object->child;	while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next;	// GetObjectItem.
33 34 35 36 37 38 39
		}
		else return 0;
	}
	return object;
}