Explorar o código

reformatting: cJSONUtils_PatchDetach

Max Bruckner %!s(int64=8) %!d(string=hai) anos
pai
achega
1235c62235
Modificáronse 1 ficheiros con 32 adicións e 10 borrados
  1. 32 10
      cJSON_Utils.c

+ 32 - 10
cJSON_Utils.c

@@ -205,19 +205,41 @@ static void cJSONUtils_InplaceDecodePointerString(char *string)
     *s2 = '\0';
 }
 
-static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path)
+static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
 {
-	char *parentptr=0,*childptr=0;cJSON *parent=0,*ret=0;
+    char *parentptr = 0;
+    char *childptr = 0;
+    cJSON *parent = 0;
+    cJSON *ret = 0;
+
+    /* copy path and split it in parent and child */
+    parentptr = strdup(path);
+    childptr = strrchr(parentptr, '/'); /* last '/' */
+    if (childptr)
+    {
+        /* split strings */
+        *childptr++ = '\0';
+    }
+    parent = cJSONUtils_GetPointer(object, parentptr);
+    cJSONUtils_InplaceDecodePointerString(childptr);
 
-	parentptr=strdup(path);	childptr=strrchr(parentptr,'/');	if (childptr) *childptr++=0;
-	parent=cJSONUtils_GetPointer(object,parentptr);
-	cJSONUtils_InplaceDecodePointerString(childptr);
+    if (!parent)
+    {
+        /* Couldn't find object to remove child from. */
+        ret = 0;
+    }
+    else if (parent->type == cJSON_Array)
+    {
+        ret = cJSON_DetachItemFromArray(parent, atoi(childptr));
+    }
+    else if (parent->type == cJSON_Object)
+    {
+        ret = cJSON_DetachItemFromObject(parent, childptr);
+    }
+    free(parentptr);
 
-	if (!parent) ret=0;	/* Couldn't find object to remove child from. */
-	else if (parent->type==cJSON_Array)		ret=cJSON_DetachItemFromArray(parent,atoi(childptr));
-	else if (parent->type==cJSON_Object)	ret=cJSON_DetachItemFromObject(parent,childptr);
-	free(parentptr);
-	return ret;
+    /* return the detachted item */
+    return ret;
 }
 
 static int cJSONUtils_Compare(cJSON *a,cJSON *b)