Loading...
Searching...
No Matches
io.c File Reference
`#include "arc/std/io.h"`
`#include "arc/std/errno.h"`
`#include "arc/std/string.h"`
`#include <stdio.h>`
`#include <stdlib.h>`

Go to the source code of this file.

## Functions

void | ARC_IO_ReadFileToUint8t (ARC_String *path, uint8_t **data, uint64_t *length)
| get string and size from file
void | ARC_IO_FileToStr (ARC_String *path, ARC_String **data)
| get string and size from file
void | ARC_IO_WriteStrToFile (ARC_String *path, ARC_String *data)
| write string to file

## Function Documentation

## ARC_IO_FileToStr()

void ARC_IO_FileToStr | ( | ARC_String * | path,
|  | ARC_String ** | data )

get string and size from file

Parameters

  path | a string to path of target file
  data | pointer to where string will be created this will need to be freed once done using it

Definition at line 43 of file io.c.

{
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);
}
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_String_Create
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
ARC_String::data
char * data
Definition string.h:15

References ARC_DEBUG_LOG_ERROR, ARC_DEBUG_LOG_ERROR_WITH_VARIABLES, arc_errno, ARC_ERRNO_COPY, ARC_ERRNO_NULL, ARC_String_Create(), and ARC_String::data.

Referenced by ARC_Config_FileIO(), ARC_Lexer_LexFile(), and ARC_Parser_ParseFile().

## ARC_IO_ReadFileToUint8t()

void ARC_IO_ReadFileToUint8t | ( | ARC_String * | path,
|  | uint8_t ** | data,
|  | uint64_t * | length )

get string and size from file

Parameters

  path | a string to path of target file
  data | pointer to where uint8_t array will be created this will need to be freed once done using it
  length | length of the data read in

Definition at line 8 of file io.c.

{
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);
}

References ARC_DEBUG_LOG_ERROR, ARC_DEBUG_LOG_ERROR_WITH_VARIABLES, arc_errno, ARC_ERRNO_COPY, ARC_ERRNO_NULL, and ARC_String::data.

## ARC_IO_WriteStrToFile()

void ARC_IO_WriteStrToFile | ( | ARC_String * | path,
|  | ARC_String * | data )

write string to file

Parameters

  path | a string to path of target file
  data | data to be written

Definition at line 77 of file io.c.

{
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);
}
ARC_String::length
uint64_t length
Definition string.h:16

References ARC_DEBUG_LOG_ERROR, ARC_DEBUG_LOG_ERROR_WITH_VARIABLES, arc_errno, ARC_ERRNO_COPY, ARC_ERRNO_NULL, ARC_String::data, and ARC_String::length.