From 7b9bdf0f92eac5f0effa96c9f0488de96860a98d Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sat, 27 Jan 2024 03:14:04 -0700 Subject: [PATCH] read in bytes --- include/arc/std/io.h | 10 ++++++++++ src/std/io.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/arc/std/io.h b/include/arc/std/io.h index 59bb217..9a79e5d 100644 --- a/include/arc/std/io.h +++ b/include/arc/std/io.h @@ -8,6 +8,16 @@ extern "C" { #include #include "arc/std/string.h" +/** + * @brief get string and size from file + * + * @param path a string to path of target file + * @param data pointer to where uint8_t array will be created + * this will need to be freed once done using it + * @param length length of the data read in +*/ +void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length); + /** * @brief get string and size from file * diff --git a/src/std/io.c b/src/std/io.c index c400f3f..7779852 100644 --- a/src/std/io.c +++ b/src/std/io.c @@ -5,6 +5,41 @@ #include #include +void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length){ + FILE *file = fopen(path->data, "rb"); + if(!file){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data); + *length = 0; + *data = NULL; + return; + } + + fseek(file, 0L, SEEK_END); + *length = ftell(file); + rewind(file); + + *data = (uint8_t *) calloc(1, *length + 1); + if(*data == NULL){ + fclose(file); + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL"); + *length = 0; + return; + } + + if(1 != fread(*data, *length, 1, file)){ + fclose(file); + arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data"); + *length = 0; + *data = NULL; + return; + } + + fclose(file); +} + void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ FILE *file = fopen(path->data, "rb"); if(!file){