aboutsummaryrefslogtreecommitdiffhomepage
path: root/io.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-11 15:19:18 +0900
committernsfisis <nsfisis@gmail.com>2025-08-15 10:06:21 +0900
commit9c12de31ab03385cea3b7bc78582ef4fdb9b22cc (patch)
tree715f6d472dbf110d864806039f743977c6c90ba7 /io.c
parentfd7d82869eb42d086174ec02938b49e4f233c319 (diff)
downloadducc-9c12de31ab03385cea3b7bc78582ef4fdb9b22cc.tar.gz
ducc-9c12de31ab03385cea3b7bc78582ef4fdb9b22cc.tar.zst
ducc-9c12de31ab03385cea3b7bc78582ef4fdb9b22cc.zip
feat: grow dynamic array
Diffstat (limited to 'io.c')
-rw-r--r--io.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/io.c b/io.c
index 6c92b20..ae02a0d 100644
--- a/io.c
+++ b/io.c
@@ -14,11 +14,24 @@ InFile* read_all(const char* filename) {
if (!in) {
return NULL;
}
- char* buf = calloc(1024 * 1024, sizeof(char));
+
+ size_t buf_size = 1024 * 10;
+ char* buf = calloc(buf_size, sizeof(char));
char* cur = buf;
char* tmp = calloc(1024, sizeof(char));
+
while (fgets(tmp, 1024, in)) {
size_t len = strlen(tmp);
+ size_t used_size = cur - buf;
+
+ if (buf_size <= used_size + len) {
+ size_t old_size = buf_size;
+ buf_size *= 2;
+ buf = realloc(buf, buf_size);
+ memset(buf + old_size, 0, buf_size - old_size);
+ cur = buf + used_size;
+ }
+
memcpy(cur, tmp, len);
cur += len;
}