summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWormHeamer2025-02-10 02:36:09 -0500
committerWormHeamer2025-02-10 02:37:36 -0500
commit528460889a2ea771e5ee81d6573a0e17d584ab6e (patch)
tree9694c28c1a4de075c01f99e7f09db07b49ed6d1f
parent1055461bcf7f0d20f8e6823094624138145d23ac (diff)
make chunk size configurable, remove unneeded copying
-rw-r--r--strio.h15
1 files 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;