diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 17:08:25 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 17:08:25 +0200 |
commit | fefe914802c8592d6539f33865902326fba4525e (patch) | |
tree | c82fe7170bdf3c931d10d27d294bc2a8184e8267 | |
parent | a9e6bcdfbee0767b6375a239c67b979db63d5812 (diff) |
tak: sta standaard invoer toe
-rw-r--r-- | src/tak.c | 62 |
1 files changed, 49 insertions, 13 deletions
@@ -1,3 +1,4 @@ +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -38,25 +39,25 @@ static char** parse_options(int argc, char **argv) { return argv + optind; } -static void process(const char *fname) { - struct map *map = open_map(fname); - if (map == NULL) { - exit(1); - } +struct state { + char *buf; + size_t sz; +}; +static void process(struct state state) { char *lstart, *lend; - lend = &map->addr[map->sb.st_size - 1]; + lend = &state.buf[state.sz - 1]; - while (lend > map->addr) { + while (lend > state.buf) { if (*lend == '\n') { lend--; } lstart = lend; - while (*lstart != '\n' && lstart != map->addr) { + while (*lstart != '\n' && lstart != state.buf) { lstart--; } - if (lstart != map->addr) { + if (lstart != state.buf) { lstart++; } @@ -64,18 +65,53 @@ static void process(const char *fname) { lend = lstart-1; } +} + +static char *read_stdin(size_t *sz) { + const size_t CHUNK_SIZE = 4096; + + size_t cap = CHUNK_SIZE; + char *res = malloc(cap); + + while (!feof(stdin)) { + if (cap-*sz < CHUNK_SIZE) { + cap *= 2; + res = realloc(res, cap); + } + + const size_t n = fread(res+*sz, 1, CHUNK_SIZE, stdin); + *sz += n; + + if (n != CHUNK_SIZE && !feof(stdin)) { + fprintf(stderr, "tak: fout tijdens lezen van standaard invoer.\n"); + exit(1); + } + } - close_map(map); + return res; } int entry_tak(int argc, char **argv) { char **args = parse_options(argc, argv); if (*args == NULL) { - fprintf(stderr, "tak: Standaard invoer niet ondersteund in deze projectie-gebaseerde implementatie\n"); - return 1; + size_t sz = 0; + char *data = read_stdin(&sz); + + struct state state = { .buf = data, .sz = sz }; + process(state); + + free(data); } else { while (*args != NULL) { - process(*args); + struct map *map = open_map(*args); + if (map == NULL) { + return 1; + } + + struct state state = { .buf = map->addr, .sz = map->sb.st_size }; + process(state); + + close_map(map); args++; } } |