From 99896d879c151fbd97c02e40608ceb4e113efbfc Mon Sep 17 00:00:00 2001 From: Kyle Chisholm Date: Mon, 28 Nov 2016 03:21:15 -0500 Subject: [PATCH] function to print and compare to preallocated buffer --- test.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test.c b/test.c index 1952858..7e17dff 100644 --- a/test.c +++ b/test.c @@ -82,6 +82,63 @@ struct record const char *country; }; + +/* Create a bunch of objects as demonstration. */ +int print_preallocated(cJSON *root) +{ + /* declarations */ + char *out = NULL; + char *buf = NULL; + char *buf_fail = NULL; + int len = 0; + int len_fail = 0; + + /* formatted print */ + out = cJSON_Print(root); + + /* create buffer to succeed */ + /* the extra 64 bytes are in case a floating point value is printed */ + len = strlen(out) + 64; + buf = malloc(len); + + /* create buffer to fail */ + len_fail = strlen(out); + buf_fail = malloc(len_fail); + + /* Print to buffer */ + if (cJSON_PrintPreallocated(root, buf, len, 1) != 0) { + printf("cJSON_PrintPreallocated failed!\n"); + if (strcmp(out, buf) != 0) { + printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n"); + printf("cJSON_Print result:\n%s\n", out); + printf("cJSON_PrintPreallocated result:\n%s\n", buf); + } + free(out); + free(buf_fail); + free(buf); + return -1; + } + + /* success */ + printf("%s\n", buf); + + /* force it to fail */ + if (cJSON_PrintPreallocated(root, buf_fail, len_fail, 1) == 0) { + printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n"); + printf("cJSON_Print result:\n%s\n", out); + printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail); + free(out); + free(buf_fail); + free(buf); + return -1; + } + + free(out); + free(buf_fail); + free(buf); + return 0; +} + /* Create a bunch of objects as demonstration. */ void create_objects(void) {