mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
reformatting: cJSONUtils_ApplyPatch
This commit is contained in:
parent
284a8017b7
commit
d883f9b9b0
167
cJSON_Utils.c
167
cJSON_Utils.c
@ -300,73 +300,164 @@ static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch)
|
||||
static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
|
||||
{
|
||||
cJSON *op=0,*path=0,*value=0,*parent=0;int opcode=0;char *parentptr=0,*childptr=0;
|
||||
cJSON *op = 0;
|
||||
cJSON *path = 0;
|
||||
cJSON *value = 0;
|
||||
cJSON *parent = 0;
|
||||
int opcode = 0;
|
||||
char *parentptr = 0;
|
||||
char *childptr = 0;
|
||||
|
||||
op=cJSON_GetObjectItem(patch,"op");
|
||||
path=cJSON_GetObjectItem(patch,"path");
|
||||
if (!op || !path) return 2; /* malformed patch. */
|
||||
|
||||
if (!strcmp(op->valuestring,"add")) opcode=0;
|
||||
else if (!strcmp(op->valuestring,"remove")) opcode=1;
|
||||
else if (!strcmp(op->valuestring,"replace"))opcode=2;
|
||||
else if (!strcmp(op->valuestring,"move")) opcode=3;
|
||||
else if (!strcmp(op->valuestring,"copy")) opcode=4;
|
||||
else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value"));
|
||||
else return 3; /* unknown opcode. */
|
||||
|
||||
if (opcode==1 || opcode==2) /* Remove/Replace */
|
||||
op = cJSON_GetObjectItem(patch, "op");
|
||||
path = cJSON_GetObjectItem(patch, "path");
|
||||
if (!op || !path)
|
||||
{
|
||||
cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); /* Get rid of old. */
|
||||
if (opcode==1) return 0; /* For Remove, this is job done. */
|
||||
/* malformed patch. */
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (opcode==3 || opcode==4) /* Copy/Move uses "from". */
|
||||
/* decode operation */
|
||||
if (!strcmp(op->valuestring, "add"))
|
||||
{
|
||||
cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; /* missing "from" for copy/move. */
|
||||
opcode = 0;
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "remove"))
|
||||
{
|
||||
opcode = 1;
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "replace"))
|
||||
{
|
||||
opcode = 2;
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "move"))
|
||||
{
|
||||
opcode = 3;
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "copy"))
|
||||
{
|
||||
opcode = 4;
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "test"))
|
||||
{
|
||||
/* compare value: {...} with the given path */
|
||||
return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* unknown opcode. */
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring);
|
||||
if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring);
|
||||
if (!value) return 5; /* missing "from" for copy/move. */
|
||||
if (opcode==4) value=cJSON_Duplicate(value,1);
|
||||
if (!value) return 6; /* out of memory for copy/move. */
|
||||
/* Remove/Replace */
|
||||
if ((opcode == 1) || (opcode == 2))
|
||||
{
|
||||
/* Get rid of old. */
|
||||
cJSON_Delete(cJSONUtils_PatchDetach(object, path->valuestring));
|
||||
if (opcode == 1)
|
||||
{
|
||||
/* For Remove, this is job done. */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy/Move uses "from". */
|
||||
if ((opcode == 3) || (opcode == 4))
|
||||
{
|
||||
cJSON *from = cJSON_GetObjectItem(patch, "from");
|
||||
if (!from)
|
||||
{
|
||||
/* missing "from" for copy/move. */
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (opcode == 3)
|
||||
{
|
||||
/* move */
|
||||
value = cJSONUtils_PatchDetach(object, from->valuestring);
|
||||
}
|
||||
if (opcode == 4)
|
||||
{
|
||||
/* copy */
|
||||
value = cJSONUtils_GetPointer(object, from->valuestring);
|
||||
}
|
||||
if (!value)
|
||||
{
|
||||
/* missing "from" for copy/move. */
|
||||
return 5;
|
||||
}
|
||||
if (opcode == 4)
|
||||
{
|
||||
value = cJSON_Duplicate(value, 1);
|
||||
}
|
||||
if (!value)
|
||||
{
|
||||
/* out of memory for copy/move. */
|
||||
return 6;
|
||||
}
|
||||
}
|
||||
else /* Add/Replace uses "value". */
|
||||
{
|
||||
value=cJSON_GetObjectItem(patch,"value");
|
||||
if (!value) return 7; /* missing "value" for add/replace. */
|
||||
value=cJSON_Duplicate(value,1);
|
||||
if (!value) return 8; /* out of memory for add/replace. */
|
||||
value = cJSON_GetObjectItem(patch, "value");
|
||||
if (!value)
|
||||
{
|
||||
/* missing "value" for add/replace. */
|
||||
return 7;
|
||||
}
|
||||
value = cJSON_Duplicate(value, 1);
|
||||
if (!value)
|
||||
{
|
||||
/* out of memory for add/replace. */
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* Now, just add "value" to "path". */
|
||||
|
||||
parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0;
|
||||
parent=cJSONUtils_GetPointer(object,parentptr);
|
||||
/* split pointer in parent and child */
|
||||
parentptr = strdup(path->valuestring);
|
||||
childptr = strrchr(parentptr, '/');
|
||||
if (childptr)
|
||||
{
|
||||
*childptr++ = '\0';
|
||||
}
|
||||
parent = cJSONUtils_GetPointer(object, parentptr);
|
||||
cJSONUtils_InplaceDecodePointerString(childptr);
|
||||
|
||||
/* add, remove, replace, move, copy, test. */
|
||||
if (!parent) {free(parentptr); cJSON_Delete(value); return 9;} /* Couldn't find object to add to. */
|
||||
else if (parent->type==cJSON_Array)
|
||||
if (!parent)
|
||||
{
|
||||
if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value);
|
||||
else cJSON_InsertItemInArray(parent,atoi(childptr),value);
|
||||
/* Couldn't find object to add to. */
|
||||
free(parentptr);
|
||||
cJSON_Delete(value);
|
||||
return 9;
|
||||
}
|
||||
else if (parent->type==cJSON_Object)
|
||||
else if (parent->type == cJSON_Array)
|
||||
{
|
||||
cJSON_DeleteItemFromObject(parent,childptr);
|
||||
cJSON_AddItemToObject(parent,childptr,value);
|
||||
if (!strcmp(childptr, "-"))
|
||||
{
|
||||
cJSON_AddItemToArray(parent, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
cJSON_InsertItemInArray(parent, atoi(childptr), value);
|
||||
}
|
||||
}
|
||||
else if (parent->type == cJSON_Object)
|
||||
{
|
||||
cJSON_DeleteItemFromObject(parent, childptr);
|
||||
cJSON_AddItemToObject(parent, childptr, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
cJSON_Delete(value);
|
||||
}
|
||||
free(parentptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches)
|
||||
{
|
||||
int err;
|
||||
|
Loading…
Reference in New Issue
Block a user