void mpack_writer_init_growable(mpack_writer_t* writer, char** target_data, size_t* target_size) { mpack_assert(target_data != NULL, "cannot initialize writer without a destination for the data"); mpack_assert(target_size != NULL, "cannot initialize writer without a destination for the size"); *target_data = NULL; *target_size = 0; MPACK_STATIC_ASSERT(sizeof(mpack_growable_writer_t) <= sizeof(writer->reserved), "not enough reserved space for growable writer!"); mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*)mpack_writer_get_reserved(writer); growable_writer->target_data = target_data; growable_writer->target_size = target_size; size_t capacity = MPACK_BUFFER_SIZE; char* buffer = (char*)MPACK_MALLOC(capacity); if (buffer == NULL) { mpack_writer_init_error(writer, mpack_error_memory); return; } mpack_writer_init(writer, buffer, capacity); mpack_writer_set_context(writer, growable_writer); mpack_writer_set_flush(writer, mpack_growable_writer_flush); mpack_writer_set_teardown(writer, mpack_growable_writer_teardown); }
void mpack_writer_set_flush(mpack_writer_t* writer, mpack_writer_flush_t flush) { MPACK_STATIC_ASSERT(MPACK_WRITER_MINIMUM_BUFFER_SIZE >= MPACK_MAXIMUM_TAG_SIZE, "minimum buffer size must fit any tag!"); if (writer->size < MPACK_WRITER_MINIMUM_BUFFER_SIZE) { mpack_break("buffer size is %i, but minimum buffer size for flush is %i", (int)writer->size, MPACK_WRITER_MINIMUM_BUFFER_SIZE); mpack_writer_flag_error(writer, mpack_error_bug); return; } writer->flush = flush; }
void mpack_reader_set_fill(mpack_reader_t* reader, mpack_reader_fill_t fill) { MPACK_STATIC_ASSERT(MPACK_READER_MINIMUM_BUFFER_SIZE >= MPACK_MAXIMUM_TAG_SIZE, "minimum buffer size must fit any tag!"); if (reader->size == 0) { mpack_break("cannot use fill function without a writeable buffer!"); mpack_reader_flag_error(reader, mpack_error_bug); return; } if (reader->size < MPACK_READER_MINIMUM_BUFFER_SIZE) { mpack_break("buffer size is %i, but minimum buffer size for fill is %i", (int)reader->size, MPACK_READER_MINIMUM_BUFFER_SIZE); mpack_reader_flag_error(reader, mpack_error_bug); return; } reader->fill = fill; }