pawengine/io.c

48 lines
1 KiB
C
Raw Normal View History

2025-02-09 18:48:25 +01:00
#include "log.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
char *read_text_file(char *path) {
FILE *file = fopen(path, "r");
if (file == NULL) {
meow("Could not open file %s", path);
return NULL;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = malloc((size + 1) * sizeof(char));
long count = fread(content, sizeof(char), size, file);
if (count < size) {
content = realloc(content, count + 1);
}
content[count] = 0x00;
fclose(file);
return content;
}
uint32_t *read_binary_file(char *path, int *size) {
FILE *file = fopen(path, "r");
if (file == NULL) {
meow("Could not open file %s", path);
return NULL;
}
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
*size = fsize;
fseek(file, 0, SEEK_SET);
char *content = malloc((fsize) * sizeof(char));
long count = fread(content, sizeof(char), fsize, file);
if (count < fsize) {
content = realloc(content, count);
}
fclose(file);
return (uint32_t *)content;
}