MCPcopy Create free account
hub / github.com/esnet/iperf / print

Function print

src/cjson.c:1198–1260  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1196#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
1197
1198static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1199{
1200 static const size_t default_buffer_size = 256;
1201 printbuffer buffer[1];
1202 unsigned char *printed = NULL;
1203
1204 memset(buffer, 0, sizeof(buffer));
1205
1206 /* create buffer */
1207 buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1208 buffer->length = default_buffer_size;
1209 buffer->format = format;
1210 buffer->hooks = *hooks;
1211 if (buffer->buffer == NULL)
1212 {
1213 goto fail;
1214 }
1215
1216 /* print the value */
1217 if (!print_value(item, buffer))
1218 {
1219 goto fail;
1220 }
1221 update_offset(buffer);
1222
1223 /* check if reallocate is available */
1224 if (hooks->reallocate != NULL)
1225 {
1226 printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1227 if (printed == NULL) {
1228 goto fail;
1229 }
1230 buffer->buffer = NULL;
1231 }
1232 else /* otherwise copy the JSON over to a new buffer */
1233 {
1234 printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1235 if (printed == NULL)
1236 {
1237 goto fail;
1238 }
1239 memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1240 printed[buffer->offset] = '\0'; /* just to be sure */
1241
1242 /* free the buffer */
1243 hooks->deallocate(buffer->buffer);
1244 }
1245
1246 return printed;
1247
1248fail:
1249 if (buffer->buffer != NULL)
1250 {
1251 hooks->deallocate(buffer->buffer);
1252 }
1253
1254 if (printed != NULL)
1255 {

Callers 2

cJSON_PrintFunction · 0.85
cJSON_PrintUnformattedFunction · 0.85

Calls 2

print_valueFunction · 0.85
update_offsetFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…