From 528460889a2ea771e5ee81d6573a0e17d584ab6e Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Mon, 10 Feb 2025 02:36:09 -0500 Subject: make chunk size configurable, remove unneeded copying --- strio.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/strio.h b/strio.h index 20d0ddf..6aaf741 100644 --- a/strio.h +++ b/strio.h @@ -5,17 +5,24 @@ #include "str.h" +#ifndef STRIO_CHUNK_SIZE +#define STRIO_CHUNK_SIZE 256 +#endif + string read_whole_file(FILE *f); #ifdef STDWRM_IMPL_STR +#include <stdio.h> + string read_whole_file(FILE *f) { - char chunk[256]; string s; - DA_INIT(s); + DA_INIT_SIZE(s, STRIO_CHUNK_SIZE); while (!feof(f)) { - size_t n = fread(chunk, 1, sizeof chunk, f); - DA_PUSH_MULT(s, chunk, n); + char *buf = &s[DA_LEN(s)]; + size_t n = fread(buf, 1, STRIO_CHUNK_SIZE, f); + DA_FIT(s, DA_LEN(s) + n); + DA_LEN(s) += n; } DA_PUSH(s, 0); return s; -- cgit 1.4.1-2-gfad0