Archeus 0.0.0
A C library and game engine that focuses on documentation
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
patha string to path of target file
datapointer to where string will be created this will need to be freed once done using it

Definition at line 43 of file io.c.

43 {
44 FILE *file = fopen(path->data, "rb");
45 if(!file){
47 ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data);
48 return;
49 }
50
51 fseek(file, 0L, SEEK_END);
52 uint64_t length = ftell(file);
53 rewind(file);
54
55 char *fileData = (char *) calloc(1, length + 1);
56 if(fileData == NULL){
57 fclose(file);
59 ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL");
60 *data = NULL;
61 return;
62 }
63
64 if(1 != fread(fileData, length, 1, file)){
65 fclose(file);
67 ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data");
68 *data = NULL;
69 return;
70 }
71
72 fclose(file);
73 ARC_String_Create(data, fileData, length);
74 free(fileData);
75}
int32_t arc_errno
Definition errno.c:5
#define ARC_DEBUG_LOG_ERROR_WITH_VARIABLES(STR,...)
Definition errno.h:40
#define ARC_ERRNO_COPY
Definition errno.h:8
#define ARC_ERRNO_NULL
Definition errno.h:6
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
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
patha string to path of target file
datapointer to where uint8_t array will be created this will need to be freed once done using it
lengthlength of the data read in

Definition at line 8 of file io.c.

8 {
9 FILE *file = fopen(path->data, "rb");
10 if(!file){
12 ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data);
13 *length = 0;
14 *data = NULL;
15 return;
16 }
17
18 fseek(file, 0L, SEEK_END);
19 *length = ftell(file);
20 rewind(file);
21
22 *data = (uint8_t *) calloc(1, *length + 1);
23 if(*data == NULL){
24 fclose(file);
26 ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL");
27 *length = 0;
28 return;
29 }
30
31 if(1 != fread(*data, *length, 1, file)){
32 fclose(file);
34 ARC_DEBUG_LOG_ERROR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data");
35 *length = 0;
36 *data = NULL;
37 return;
38 }
39
40 fclose(file);
41}

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
patha string to path of target file
datadata to be written

Definition at line 77 of file io.c.

77 {
78 FILE *file = fopen(path->data, "wb");
79 if(!file){
81 ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data), could not open file \"%s\"", path->data);
82 return;
83 }
84
85 if(1 != fwrite(data->data, data->length, 1, file)){
86 fclose(file);
88 ARC_DEBUG_LOG_ERROR("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String **data), could not write file data");
89 return;
90 }
91
92 fclose(file);
93}
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.