diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-12-11 10:49:50 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-12-11 10:51:40 -0500 |
| commit | fa8fa6784559ed0fc8d780e36880273f77e272c4 (patch) | |
| tree | 7456a4e9148d47e409ba837bafdc6238b6c757db /src/utils.c | |
| parent | add ubos (diff) | |
| download | voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.tar.gz voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.tar.bz2 voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.zip | |
i did a lot
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..fdc6aea --- /dev/null +++ b/src/utils.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "types.h" +#include "voxel.h" +#include "utils.h" + +char *read_file(const char *filename) +{ + FILE *file; + long length, read; + char *buffer; + + file = fopen(filename, "r"); + if (file == NULL) { + ERROR("could not read file: %s", filename); + return NULL; + } + + fseek(file, 0, SEEK_END); + length = ftell(file); + fseek(file, 0, SEEK_SET); + + buffer = malloc(length + 1); + read = fread(buffer, 1, length, file); + buffer[length] = 0; + + if (read < length) { + ERROR("could not read file: %s", filename); + free(buffer); + return NULL; + } + + fclose(file); + return buffer; +} + +_Noreturn void die(void) +{ + exit(1); +} + +void *xalloc(usize size) +{ + void *ptr = malloc(size); + if (ptr == NULL && size != 0) + die(); + return ptr; +} + +void *xrealloc(void *ptr, usize size) +{ + ptr = realloc(ptr, size); + if (ptr == NULL && size != 0) + die(); + return ptr; +} + +void *xzalloc(usize size) +{ + void *ptr = xalloc(size); + memset(ptr, 0, size); + return ptr; +} |