cusel/compiler/main.c
2026-11-18 23:41:26 +02:00

57 lines
980 B
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include "vector.h"
VEC(char);
[[noreturn]] static void usage(void);
static void read_into_vec(struct vec_char *, FILE *);
int main(int argc, char **argv) {
argv0 = argv[0];
// TODO: proper argument parsing
if (argc != 2) {
usage();
}
auto path = argv[1];
FILE *f = fopen(path, "r");
if (!f) {
die("cannot open file \"%s\"", path);
}
struct vec_char buffer = {};
read_into_vec(&buffer, f);
if (ferror(f)) {
die("failure reading file \"%s\"", path);
}
}
static void usage(void) {
fprintf(stderr, "usage: %s file\n", argv0);
exit(1);
}
static void read_into_vec(
struct vec_char *buffer,
FILE *f
) {
for (;;) {
VEC_ENSURE_FREE_SPACE(*buffer);
auto free_space = VEC_FREE_SPACE(char, *buffer);
auto bytes_read = fread(free_space.data.ptr, 1, LENGTH(free_space), f);
if (bytes_read == 0) {
break;
}
VEC_RESIZE(*buffer, LENGTH(*buffer) + bytes_read);
}
}