Archeus 0.0.0
A C library and game engine that focuses on documentation
Loading...
Searching...
No Matches
io.c
Go to the documentation of this file.
1#include "arc/std/io.h"
2
3#include "arc/std/errno.h"
4#include "arc/std/string.h"
5#include <stdio.h>
6#include <stdlib.h>
7
8void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length){
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}
42
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}
76
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}
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_IO_FileToStr(ARC_String *path, ARC_String **data)
get string and size from file
Definition io.c:43
void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data)
write string to file
Definition io.c:77
void ARC_IO_ReadFileToUint8t(ARC_String *path, uint8_t **data, uint64_t *length)
get string and size from file
Definition io.c:8
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
substring position within a string
Definition string.h:14
uint64_t length
Definition string.h:16
char * data
Definition string.h:15