MCPcopy Create free account
hub / github.com/TheForceEngine/TheForceEngine / print

Function print

TheForceEngine/TFE_System/cJSON.c:1192–1254  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

cJSON_PrintFunction · 0.70
cJSON_PrintUnformattedFunction · 0.70

Calls 4

print_valueFunction · 0.85
update_offsetFunction · 0.85
reallocateMethod · 0.80
allocateMethod · 0.45

Tested by

no test coverage detected