reformatting: cJSONUtils_FindPointerFromObjectTo

This commit is contained in:
Max Bruckner 2016-10-14 00:05:58 +07:00
parent c6cb991e3f
commit 158ddceab3

View File

@ -100,10 +100,17 @@ static void cJSONUtils_PointerEncodedstrcpy(char *d, const char *s)
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("");
}
/* recursively search all children of the object */
for (obj = object->child; obj; obj = obj->next, c++) for (obj = object->child; obj; obj = obj->next, c++)
{ {
char *found = cJSONUtils_FindPointerFromObjectTo(obj, target); char *found = cJSONUtils_FindPointerFromObjectTo(obj, target);
@ -111,23 +118,31 @@ char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target)
{ {
if (type == cJSON_Array) if (type == cJSON_Array)
{ {
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
char *ret = (char*)malloc(strlen(found) + 23); char *ret = (char*)malloc(strlen(found) + 23);
sprintf(ret,"/%d%s",c,found); 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 = '/';
cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string);
strcat(ret, found); 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;
} }