mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
rework cJSONUtils_GetPointer to avoid malloc/free by using strcmp with pointer-encoding awareness.
git-svn-id: svn://svn.code.sf.net/p/cjson/code@64 e3330c51-1366-4df0-8b21-3ccf24e3d50e
This commit is contained in:
parent
c0f5e2056b
commit
858671cc7f
@ -1,39 +1,35 @@
|
|||||||
#include <stdio.h>
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "cJSON_Utils.h"
|
#include "cJSON_Utils.h"
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
|
cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
|
||||||
{
|
{
|
||||||
cJSON *target=object;int which=0;const char *s=0;int len=0;char *element=0,*e=0;
|
cJSON *target=object;int which=0;const char *element=0;
|
||||||
|
|
||||||
while (*pointer=='/' && object)
|
while (*pointer=='/' && object)
|
||||||
{
|
{
|
||||||
pointer++;
|
pointer++;
|
||||||
if (object->type==cJSON_Array)
|
if (object->type==cJSON_Array)
|
||||||
{
|
{
|
||||||
which=0;
|
which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
|
||||||
while (*pointer>='0' && *pointer<='9') {which*=10;which+=*pointer++ - '0';}
|
|
||||||
if (*pointer && *pointer!='/') return 0;
|
if (*pointer && *pointer!='/') return 0;
|
||||||
object=cJSON_GetArrayItem(object,which);
|
object=cJSON_GetArrayItem(object,which);
|
||||||
}
|
}
|
||||||
else if (object->type==cJSON_Object)
|
else if (object->type==cJSON_Object)
|
||||||
{
|
{
|
||||||
|
element=pointer; while (*pointer && *pointer!='/') pointer++;
|
||||||
s=pointer;len=0;
|
object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next; // GetObjectItem.
|
||||||
while (*s && *s!='/') {if (*s!='~') len++; s++;}
|
|
||||||
e=element=malloc(len+1); if (!element) return 0;
|
|
||||||
element[len]=0;
|
|
||||||
|
|
||||||
while (*pointer && *pointer!='/')
|
|
||||||
{
|
|
||||||
if (*pointer=='~' && pointer[1]=='0') *e++='~',pointer+=2;
|
|
||||||
else if (*pointer=='~' && pointer[1]=='1') *e++='/',pointer+=2;
|
|
||||||
else if (*pointer=='~') {free(element); return 0;} // Invalid encoding.
|
|
||||||
else *e++=*pointer++;
|
|
||||||
}
|
|
||||||
object=cJSON_GetObjectItem(object,element);
|
|
||||||
free(element);
|
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user