read in bytes

This commit is contained in:
herbglitch 2024-01-27 03:14:04 -07:00
parent 7dc58b60a6
commit 7b9bdf0f92
2 changed files with 45 additions and 0 deletions

View file

@ -8,6 +8,16 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#include "arc/std/string.h" #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 * @brief get string and size from file
* *

View file

@ -5,6 +5,41 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
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){ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
FILE *file = fopen(path->data, "rb"); FILE *file = fopen(path->data, "rb");
if(!file){ if(!file){