diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-21 23:23:13 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-21 23:23:13 +0100 |
commit | 903ae3ab04eda63df286bbb32fe83603ffbbd0e0 (patch) | |
tree | 821808b948fed66174e0b0e4118d222395ed865e /src/darr.c | |
parent | dcedb70a5ccead41574cf857b369d264c87dd7e0 (diff) | |
download | ovm-903ae3ab04eda63df286bbb32fe83603ffbbd0e0.tar.gz ovm-903ae3ab04eda63df286bbb32fe83603ffbbd0e0.tar.bz2 ovm-903ae3ab04eda63df286bbb32fe83603ffbbd0e0.zip |
Helper darr functions to read/write bytes from FILE *
Diffstat (limited to 'src/darr.c')
-rw-r--r-- | src/darr.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -10,6 +10,7 @@ * Description: Dynamically sized byte array */ +#include <assert.h> #include <malloc.h> #include <string.h> @@ -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; +} |