提交 158ddcea 编写于 作者: M Max Bruckner

reformatting: cJSONUtils_FindPointerFromObjectTo

上级 c6cb991e
...@@ -98,36 +98,51 @@ static void cJSONUtils_PointerEncodedstrcpy(char *d, const char *s) ...@@ -98,36 +98,51 @@ static void cJSONUtils_PointerEncodedstrcpy(char *d, const char *s)
*d = '\0'; *d = '\0';
} }
char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target) char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
{ {
int type=object->type,c=0;cJSON *obj=0; int type = object->type;
int c = 0;
cJSON *obj = 0;
if (object==target) return strdup(""); if (object == target)
{
/* found */
return strdup("");
}
for (obj=object->child;obj;obj=obj->next,c++) /* recursively search all children of the object */
for (obj = object->child; obj; obj = obj->next, c++)
{ {
char *found=cJSONUtils_FindPointerFromObjectTo(obj,target); char *found = cJSONUtils_FindPointerFromObjectTo(obj, target);
if (found) if (found)
{ {
if (type==cJSON_Array) if (type == cJSON_Array)
{ {
char *ret=(char*)malloc(strlen(found)+23); /* reserve enough memory for a 64 bit integer + '/' and '\0' */
sprintf(ret,"/%d%s",c,found); char *ret = (char*)malloc(strlen(found) + 23);
sprintf(ret, "/%d%s", c, found); /* /<array_index><path> */
free(found); free(found);
return ret; return ret;
} }
else if (type==cJSON_Object) else if (type == cJSON_Object)
{ {
char *ret=(char*)malloc(strlen(found)+cJSONUtils_PointerEncodedstrlen(obj->string)+2); char *ret = (char*)malloc(strlen(found) + cJSONUtils_PointerEncodedstrlen(obj->string) + 2);
*ret='/';cJSONUtils_PointerEncodedstrcpy(ret+1,obj->string); *ret = '/';
strcat(ret,found); cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string);
strcat(ret, found);
free(found); free(found);
return ret; return ret;
} }
/* reached leaf of the tree, found nothing */
free(found); free(found);
return 0; return 0;
} }
} }
/* not found */
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册