size_t _Tny_dumps(const Tny *tny, char *data, size_t pos) { const Tny *next = NULL; uint32_t size = 0; for (next = tny; next != NULL; next = next->next) { /* Add the data type */ data[pos++] = next->type; /* Add the number of elements if this is the root element. */ if (next->type == TNY_ARRAY || next->type == TNY_DICT) { Tny_swapBytes32((uint32_t*)(data + pos), (const char*)&next->size); pos += sizeof(uint32_t); continue; } /* Add the key if this is a dictionary */ if (next->root->type == TNY_DICT) { size = strlen(next->key) + 1; Tny_swapBytes32((uint32_t*)(data + pos), (const char*)&size); pos += sizeof(uint32_t); memcpy((data + pos), next->key, size); pos += size; } /* Add the value */ if (next->type == TNY_OBJ) { if (next->value.tny != NULL) { pos = _Tny_dumps(next->value.tny, data, pos); } else { pos = 0; break; } } else if (next->type == TNY_BIN) { Tny_swapBytes32((uint32_t*)(data + pos), (const char*)&next->size); pos += sizeof(uint32_t); memcpy((data + pos), next->value.ptr, next->size); pos += next->size; } else if (next->type == TNY_CHAR) { data[pos++] = next->value.chr; } else if (next->type == TNY_INT32) { Tny_swapBytes32((uint32_t*)(data + pos), (const char*)&next->value.num); pos += sizeof(uint32_t); } else if (next->type == TNY_INT64) { Tny_swapBytes64((uint64_t*)(data + pos), (const char*)&next->value.num); pos += sizeof(uint64_t); } else if (next->type == TNY_DOUBLE) { Tny_swapBytes64((uint64_t*)(data + pos), (const char*)&next->value.num); pos += sizeof(double); } } return pos; }
size_t Tny_dumps(Tny *tny, void **data) { size_t size = 0; *data = NULL; tny = tny->root; size = Tny_calcSize(tny); if (size > 0) { *data = malloc(size); _Tny_dumps(tny, *data, 0); } return size; }
size_t Tny_dumps(const Tny *tny, void **data) { size_t size = 0; *data = NULL; tny = tny->root; size = tny->docSize; *data = malloc(size); if (*data != NULL) { size = _Tny_dumps(tny, *data, 0); if (size == 0) { free(*data); *data = NULL; } } else { size = 0; } return size; }