#include #include "file.h" FILE *file_open(str s_path, const char *mode) { NULTERM(s_path, path) return fopen(path, mode); } static bool file_size(FILE *file, size_t *size) { if (fseek(file, 0, SEEK_END) == -1) return false; long end = ftell(file); fseek(file, 0, SEEK_SET); if (end == -1) return false; *size = end; return true; } str file_read(FILE *file) { size_t size; if (file_size(file, &size)) { str s; array_alloc(&s, size); s.len = fread(s.ptr, 1, size, file); return s; } else { strbuf buf = {}; size_t n_read; do { arraybuf_grow(&buf, BUFSIZ); buf.len += (n_read = fread(buf.ptr+buf.len, 1, BUFSIZ, file)); } while (n_read == BUFSIZ); return (str) arraybuf_cast(buf); } }