2022-10-27 15:16:54 -06:00
|
|
|
#include "arc/std/io.h"
|
|
|
|
|
|
|
|
|
|
#include "arc/std/errno.h"
|
2023-01-17 01:59:08 -07:00
|
|
|
#include "arc/std/string.h"
|
2022-10-27 15:16:54 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2023-01-17 01:59:08 -07:00
|
|
|
void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
|
|
|
|
|
FILE *file = fopen(path->data, "rb");
|
|
|
|
|
if(!file){
|
|
|
|
|
arc_errno = ARC_ERRNO_NULL;
|
2024-01-12 19:48:06 -07:00
|
|
|
return;
|
2023-01-17 01:59:08 -07:00
|
|
|
}
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2023-01-17 01:59:08 -07:00
|
|
|
fseek(file, 0L, SEEK_END);
|
|
|
|
|
uint64_t length = ftell(file);
|
|
|
|
|
rewind(file);
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2023-01-17 01:59:08 -07:00
|
|
|
char *fileData = (char *) calloc(1, length + 1);
|
|
|
|
|
if(fileData == NULL){
|
|
|
|
|
fclose(file);
|
|
|
|
|
arc_errno = ARC_ERRNO_NULL;
|
|
|
|
|
*data = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2023-01-17 01:59:08 -07:00
|
|
|
if(1 != fread(fileData, length, 1, file)){
|
|
|
|
|
fclose(file);
|
|
|
|
|
arc_errno = ARC_ERRNO_COPY;
|
|
|
|
|
*data = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2023-01-17 01:59:08 -07:00
|
|
|
fclose(file);
|
|
|
|
|
ARC_String_Create(data, fileData, length);
|
2023-01-17 02:57:57 -07:00
|
|
|
free(fileData);
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|