From 903ae3ab04eda63df286bbb32fe83603ffbbd0e0 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sat, 21 Oct 2023 23:23:13 +0100 Subject: Helper darr functions to read/write bytes from FILE * --- src/darr.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/darr.c') diff --git a/src/darr.c b/src/darr.c index 1a55ea5..e9e4ca5 100644 --- a/src/darr.c +++ b/src/darr.c @@ -10,6 +10,7 @@ * Description: Dynamically sized byte array */ +#include #include #include @@ -56,3 +57,23 @@ byte darr_at(darr_t *darr, size_t index) return 0; return darr->data[index]; } + +void darr_write_file(darr_t *bytes, FILE *fp) +{ + size_t size = fwrite(bytes->data, bytes->used, 1, fp); + fclose(fp); + assert(size == 1); +} + +darr_t darr_read_file(FILE *fp) +{ + darr_t darr = {0}; + fseek(fp, 0, SEEK_END); + long size = ftell(fp); + darr_init(&darr, size); + fseek(fp, 0, SEEK_SET); + size_t read = fread(darr.data, size, 1, fp); + fclose(fp); + assert(read == 1); + return darr; +} -- cgit v1.2.3-13-gbd6f