/** * bson_reader_destroy: * @reader: An initialized bson_reader_t. * * Releases resources that were allocated during the use of a bson_reader_t. * This should be called after you have finished using the structure. */ void bson_reader_destroy (bson_reader_t *reader) { bson_return_if_fail (reader); switch (reader->type) { case 0: break; case BSON_READER_FD: { bson_reader_fd_t *fd = (bson_reader_fd_t *)reader; if (fd->close_fd) { bson_close (fd->fd); } bson_free (fd->data); } break; case BSON_READER_DATA: break; default: fprintf (stderr, "No such reader type: %02x\n", reader->type); break; } reader->type = 0; bson_free (reader); }
static bson_t * get_bson (const char *filename) { uint8_t buf[4096]; bson_t *b; ssize_t len; int fd; if (-1 == (fd = bson_open(filename, O_RDONLY))) { fprintf(stderr, "Failed to open: %s\n", filename); abort(); } if ((len = bson_read(fd, buf, sizeof buf)) < 0) { fprintf(stderr, "Failed to read: %s\n", filename); abort(); } assert(len > 0); b = bson_new_from_data(buf, (uint32_t)len); bson_close(fd); return b; }
static void test_reader_from_handle_destroy(void * handle) { bson_close(*(int *)handle); }