aboutsummaryrefslogtreecommitdiffhomepage
path: root/io.c
diff options
context:
space:
mode:
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;
}