Define basic IO abstractions, implement for FD

This commit is contained in:
Daniel Thorpe
2024-06-20 14:23:33 +01:00
parent e38e0f8415
commit d7fbb0b4f0
5 changed files with 59 additions and 0 deletions

View File

@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 3.13)
project(gemhadar LANGUAGES C)
add_subdirectory(lib)
add_executable(gemhadar
"main.c"
)
set_property(TARGET gemhadar PROPERTY C_STANDARD 11)
target_link_libraries(gemhadar gemhadar_lib)

5
lib/CMakeLists.txt Normal file
View File

@@ -0,0 +1,5 @@
add_library(gemhadar_lib
"fd_io.c"
)
target_include_directories(gemhadar_lib PUBLIC "include")
set_property(TARGET gemhadar_lib PROPERTY C_STANDARD 11)

25
lib/fd_io.c Normal file
View File

@@ -0,0 +1,25 @@
#include "io_abs.h"
#include <unistd.h>
static ssize_t fd_reader_read(void *ctx, uint8_t *buf, size_t len)
{
return read((intptr_t)ctx, buf, len);
}
void fd_reader(int fd, struct io_reader *reader_out)
{
reader_out->ctx = (void *)(intptr_t)fd;
reader_out->read = fd_reader_read;
}
static ssize_t fd_writer_write(void *ctx, const uint8_t *buf, size_t len)
{
return write((intptr_t)ctx, buf, len);
}
void fd_writer(int fd, struct io_writer *writer_out)
{
writer_out->ctx = (void *)(intptr_t)fd;
writer_out->write = fd_writer_write;
}

9
lib/include/fd_io.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef FD_IO_H
#define FD_IO_H
#include "io_abs.h"
void fd_reader(int fd, struct io_reader *reader_out);
void fd_writer(int fd, struct io_writer *writer_out);
#endif

17
lib/include/io_abs.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef IO_ABS_H
#define IO_ABS_H
#include <stdlib.h>
#include <stdint.h>
struct io_reader {
void *ctx;
ssize_t (*read)(void *ctx, uint8_t *buf, size_t len);
};
struct io_writer {
void *ctx;
ssize_t (*write)(void *ctx, const uint8_t *buf, size_t len);
};
#endif