gemhadar/test/test_io.c
2024-06-20 14:52:52 +01:00

36 lines
1.1 KiB
C

#include "test_io.h"
#include <string.h>
static ssize_t test_reader_read(void *ctx, uint8_t *buf, size_t len)
{
struct test_reader_config *config = (struct test_reader_config *)ctx;
const unsigned available = config->len - config->pos;
const unsigned to_read = len <= available ? len : available;
memcpy(buf, &config->buf[config->pos], to_read);
config->pos += to_read;
return to_read;
}
void test_reader(struct test_reader_config *config, struct io_reader *reader_out)
{
reader_out->ctx = config;
reader_out->read = test_reader_read;
}
static ssize_t test_writer_write(void *ctx, const uint8_t *buf, size_t len)
{
struct test_writer_config *config = (struct test_writer_config *)ctx;
const unsigned max = config->len - config->pos;
const unsigned to_write = len <= max ? len : max;
memcpy(&config->buf[config->pos], buf, to_write);
config->pos += to_write;
return to_write;
}
void test_writer(struct test_writer_config *config, struct io_writer *writer_out)
{
writer_out->ctx = config;
writer_out->write = test_writer_write;
}