From: Lukas Krickl Date: Wed, 11 Mar 2026 09:59:17 +0000 (+0100) Subject: file: Added file read utility X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=d715e299de2047f7d5367daa42b9ebfb9fa0d36a;p=lrts%2F.git file: Added file read utility --- diff --git a/src/lrts_impl.h b/src/lrts_impl.h index 6bd2ac7..d6a2311 100644 --- a/src/lrts_impl.h +++ b/src/lrts_impl.h @@ -54,6 +54,7 @@ #include "t_update.c" #include "l_lsl.c" #include "u_debug.c" +#include "u_file.c" #endif diff --git a/src/p_pc/u_stdio.c b/src/p_pc/u_stdio.c index c69e614..f5b3a54 100644 --- a/src/p_pc/u_stdio.c +++ b/src/p_pc/u_stdio.c @@ -47,3 +47,16 @@ int u_eprintf(const char *fmt, ...) { return res; } + + +U_FILE* u_fopen(const char *path, const char *mode) { + return fopen(path, mode); +} + +void u_fclose(U_FILE *f) { + fclose(f); +} + +int u_fgetc(U_FILE *f) { + return fgetc(f); +} diff --git a/src/u_file.c b/src/u_file.c new file mode 100644 index 0000000..538ba27 --- /dev/null +++ b/src/u_file.c @@ -0,0 +1,22 @@ +#include "u_file.h" + +const char *u_file_read(const char *path) { + u32 len = 16; + u32 index = 0; + char *buf = u_malloc(len); + char c; + U_FILE *f = u_fopen(path, "r"); + if (f == LRTS_NULL) { + return LRTS_NULL; + } + + while ((c = u_fgetc(f)) >= 0) { + if (index >= len) { + len *= 2; + buf = u_realloc(buf, len); + } + buf[index++] = c; + } + + return buf; +} diff --git a/src/u_file.h b/src/u_file.h new file mode 100644 index 0000000..53201cd --- /dev/null +++ b/src/u_file.h @@ -0,0 +1,14 @@ +#ifndef U_FILE_H__ +#define U_FILE_H__ + +U_FILE* u_fopen(const char *path, const char *mode); + +void u_fclose(U_FILE *f); + +int u_fgetc(U_FILE *f); + +/* reads a file into a memory buffer + * allocated with u_malloc */ +const char *u_file_read(const char *path); + +#endif