cJSONUtils_GeneratePatch -> compose_patch

This commit is contained in:
Max Bruckner 2017-04-30 16:39:30 +02:00
parent 997ca129a2
commit 1d65f33d3d

View File

@ -848,7 +848,7 @@ CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * co
return 0; return 0;
} }
static void cJSONUtils_GeneratePatch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value) static void compose_patch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value)
{ {
cJSON *patch = cJSON_CreateObject(); cJSON *patch = cJSON_CreateObject();
if (patch == NULL) if (patch == NULL)
@ -883,7 +883,7 @@ static void cJSONUtils_GeneratePatch(cJSON * const patches, const unsigned char
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value) CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value)
{ {
cJSONUtils_GeneratePatch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value); compose_patch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value);
} }
static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to) static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to)
@ -895,7 +895,7 @@ static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char
if ((from->type & 0xFF) != (to->type & 0xFF)) if ((from->type & 0xFF) != (to->type & 0xFF))
{ {
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to); compose_patch(patches, (const unsigned char*)"replace", path, 0, to);
return; return;
} }
@ -904,14 +904,14 @@ static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char
case cJSON_Number: case cJSON_Number:
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble)) if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
{ {
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, NULL, to); compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
} }
return; return;
case cJSON_String: case cJSON_String:
if (strcmp(from->valuestring, to->valuestring) != 0) if (strcmp(from->valuestring, to->valuestring) != 0)
{ {
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, NULL, to); compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
} }
return; return;
@ -949,12 +949,12 @@ static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char
return; return;
} }
sprintf((char*)new_path, "%lu", (unsigned long)index); sprintf((char*)new_path, "%lu", (unsigned long)index);
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, new_path, NULL); compose_patch(patches, (const unsigned char*)"remove", path, new_path, NULL);
} }
/* add new elements in 'to' that were not in 'from' */ /* add new elements in 'to' that were not in 'from' */
for (; (to_child != NULL); (void)(to_child = to_child->next), index++) for (; (to_child != NULL); (void)(to_child = to_child->next), index++)
{ {
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child); compose_patch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child);
} }
free(new_path); free(new_path);
return; return;
@ -1006,14 +1006,14 @@ static void cJSONUtils_CompareToPatch(cJSON * const patches, const unsigned char
else if (diff < 0) else if (diff < 0)
{ {
/* object element doesn't exist in 'to' --> remove it */ /* object element doesn't exist in 'to' --> remove it */
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL); compose_patch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL);
from_child = from_child->next; from_child = from_child->next;
} }
else else
{ {
/* object element doesn't exist in 'from' --> add it */ /* object element doesn't exist in 'from' --> add it */
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child); compose_patch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child);
to_child = to_child->next; to_child = to_child->next;
} }