MCPcopy Create free account
hub / github.com/Netis/cloud-probe / print

Function print

cpworker/src/cJSON/cJSON.c:1196–1261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 6

cJSON_PrintFunction · 0.85
cJSON_PrintUnformattedFunction · 0.85
usageMethod · 0.85
extract_version.pyFile · 0.85

Calls 2

print_valueFunction · 0.85
update_offsetFunction · 0.85

Tested by 1

usageMethod · 0.68