Loading...
Searching...
No Matches
io.c
Go to the documentation of this file.
#include "arc/std/io.h"
#include "arc/std/errno.h"
#include "arc/std/string.h"
#include <stdio.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_ERROR_WITH_VARIABLES("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_LOG_ERROR("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_LOG_ERROR("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){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data);
return;
}
fseek(file, 0L, SEEK_END);
uint64_t length = ftell(file);
rewind(file);
char *fileData = (char *) calloc(1, length + 1);
if(fileData == NULL){
fclose(file);
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL");
*data = NULL;
return;
}
if(1 != fread(fileData, length, 1, file)){
fclose(file);
arc_errno = ARC_ERRNO_COPY;
ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data");
*data = NULL;
return;
}
fclose(file);
ARC_String_Create(data, fileData, length);
free(fileData);
}
void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data){
FILE *file = fopen(path->data, "wb");
if(!file){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data), could not open file \"%s\"", path->data);
return;
}
if(1 != fwrite(data->data, data->length, 1, file)){
fclose(file);
arc_errno = ARC_ERRNO_COPY;
ARC_DEBUG_LOG_ERROR("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String **data), could not write file data");
return;
}
fclose(file);
}
errno.h
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES
#define ARC_DEBUG_LOG_ERROR_WITH_VARIABLES(STR,...)
Definition errno.h:40
ARC_ERRNO_COPY
#define ARC_ERRNO_COPY
Definition errno.h:8
ARC_ERRNO_NULL
#define ARC_ERRNO_NULL
Definition errno.h:6
ARC_DEBUG_LOG_ERROR
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
ARC_IO_FileToStr
void ARC_IO_FileToStr(ARC_String *path, ARC_String **data)
get string and size from file
Definition io.c:43
ARC_IO_WriteStrToFile
void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data)
write string to file
Definition io.c:77
ARC_IO_ReadFileToUint8t
void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length)
get string and size from file
Definition io.c:8
io.h
string.h
ARC_String_Create
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
ARC_String
substring position within a string
Definition string.h:14
ARC_String::length
uint64_t length
Definition string.h:16
ARC_String::data
char * data
Definition string.h:15