From 2033721c220ff176b53d4a121fe2b00a48a6a0ab Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Fri, 19 Sep 2025 14:58:43 +1000 Subject: [PATCH] add basic read syscall wrappers for terminfo db --- src/read.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/read.h | 6 ++++++ 2 files changed, 57 insertions(+) create mode 100644 src/read.c create mode 100644 src/read.h diff --git a/src/read.c b/src/read.c new file mode 100644 index 0000000..e9091e0 --- /dev/null +++ b/src/read.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include "read.h" + +static struct ti_db { + const char *restrict const file; + // const FILE *restrict const stream; + const long size; +}; + +/* + * Returns 0 on success, else 1 on failure and sets errno. + */ +static inline int fsize(FILE *restrict const stream, long *restrict const size, + const bool offset) { + long initpos = (offset) ? ftell(stream) : 0L; + + if (fseek(stream, 0L, SEEK_END)) + return 1; + *size = ftell(stream); + if (offset && fseek(stream, initpos, SEEK_SET)) + return 1; + + return 0; +} + +/* Read the entire file at once to avoid half a million individual read syscalls. + */ +static inline int freaddb(const char *restrict const file, char *restrict *restrict const db, + long *restrict const size) { + FILE *restrict const stream = fopen(file, "rb"); + if (fsize(stream, size, false)) + return 1; + + *db = (char*)malloc(*size); + if (fread(*db, *size, 1UL, stream) != *size) + return 2; + + return 0; +} + +/* + */ +static inline int dbread(char *restrict const db, void *buf, const size_t size) { + +} + +int tireadi16() {} diff --git a/src/read.h b/src/read.h new file mode 100644 index 0000000..878fa1b --- /dev/null +++ b/src/read.h @@ -0,0 +1,6 @@ +#ifndef CURSETREE_READ_H +#define CURSETREE_READ_H + +#include + +#endif /* CURSETREE_READ_H */