MCPcopy Create free account
hub / github.com/DaveGamble/cJSON / print

Function print

cJSON.c:1234–1299  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1232#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
1233
1234static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1235{
1236 static const size_t default_buffer_size = 256;
1237 printbuffer buffer[1];
1238 unsigned char *printed = NULL;
1239
1240 memset(buffer, 0, sizeof(buffer));
1241
1242 /* create buffer */
1243 buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1244 buffer->length = default_buffer_size;
1245 buffer->format = format;
1246 buffer->hooks = *hooks;
1247 if (buffer->buffer == NULL)
1248 {
1249 goto fail;
1250 }
1251
1252 /* print the value */
1253 if (!print_value(item, buffer))
1254 {
1255 goto fail;
1256 }
1257 update_offset(buffer);
1258
1259 /* check if reallocate is available */
1260 if (hooks->reallocate != NULL)
1261 {
1262 printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1263 if (printed == NULL) {
1264 goto fail;
1265 }
1266 buffer->buffer = NULL;
1267 }
1268 else /* otherwise copy the JSON over to a new buffer */
1269 {
1270 printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1271 if (printed == NULL)
1272 {
1273 goto fail;
1274 }
1275 memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1276 printed[buffer->offset] = '\0'; /* just to be sure */
1277
1278 /* free the buffer */
1279 hooks->deallocate(buffer->buffer);
1280 buffer->buffer = NULL;
1281 }
1282
1283 return printed;
1284
1285fail:
1286 if (buffer->buffer != NULL)
1287 {
1288 hooks->deallocate(buffer->buffer);
1289 buffer->buffer = NULL;
1290 }
1291

Callers 6

cJSON_PrintFunction · 0.85
cJSON_PrintUnformattedFunction · 0.85
usageMethod · 0.85
usageMethod · 0.85
unity_to_junit.pyFile · 0.85

Calls 2

print_valueFunction · 0.85
update_offsetFunction · 0.85

Tested by 1

usageMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…