MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / print

Function print

libapp2sys/src/main/cpp/cjson/cJSON.c:1090–1152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1088#define cjson_min(a, b) ((a < b) ? a : b)
1089
1090static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1091{
1092 static const size_t default_buffer_size = 256;
1093 printbuffer buffer[1];
1094 unsigned char *printed = NULL;
1095
1096 memset(buffer, 0, sizeof(buffer));
1097
1098 /* create buffer */
1099 buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1100 buffer->length = default_buffer_size;
1101 buffer->format = format;
1102 buffer->hooks = *hooks;
1103 if (buffer->buffer == NULL)
1104 {
1105 goto fail;
1106 }
1107
1108 /* print the value */
1109 if (!print_value(item, buffer))
1110 {
1111 goto fail;
1112 }
1113 update_offset(buffer);
1114
1115 /* check if reallocate is available */
1116 if (hooks->reallocate != NULL)
1117 {
1118 printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1119 if (printed == NULL) {
1120 goto fail;
1121 }
1122 buffer->buffer = NULL;
1123 }
1124 else /* otherwise copy the JSON over to a new buffer */
1125 {
1126 printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1127 if (printed == NULL)
1128 {
1129 goto fail;
1130 }
1131 memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1132 printed[buffer->offset] = '\0'; /* just to be sure */
1133
1134 /* free the buffer */
1135 hooks->deallocate(buffer->buffer);
1136 }
1137
1138 return printed;
1139
1140fail:
1141 if (buffer->buffer != NULL)
1142 {
1143 hooks->deallocate(buffer->buffer);
1144 }
1145
1146 if (printed != NULL)
1147 {

Callers 2

cJSON_PrintFunction · 0.85
cJSON_PrintUnformattedFunction · 0.85

Calls 4

print_valueFunction · 0.85
update_offsetFunction · 0.85
allocateMethod · 0.80
deallocateMethod · 0.80

Tested by

no test coverage detected