MCPcopy Create free account
hub / github.com/antirez/botlib / print

Function print

cJSON.c:1183–1245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

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