From 3d24d1965908fe30fdf84ce65ccf421a6a900e94 Mon Sep 17 00:00:00 2001 From: Shaun Inman Date: Sat, 28 Jan 2023 11:11:21 -0500 Subject: [PATCH] added allocFile to utils --- src/common/utils.c | 14 ++++++++++++++ src/common/utils.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/common/utils.c b/src/common/utils.c index 0a2de9f..2b712b8 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -120,6 +120,20 @@ void getFile(char* path, char* buffer, size_t buffer_size) { buffer[size] = '\0'; } } +char* allocFile(char* path) { // caller must free! + char* contents = NULL; + FILE *file = fopen(path, "r"); + if (file) { + fseek(file, 0L, SEEK_END); + size_t size = ftell(file); + contents = calloc(size+1, sizeof(char)); + fseek(file, 0L, SEEK_SET); + fread(contents, sizeof(char), size, file); + fclose(file); + contents[size] = '\0'; + } + return contents; +} int getInt(char* path) { int i = 0; FILE *file = fopen(path, "r"); diff --git a/src/common/utils.h b/src/common/utils.h index e3962e1..0d14697 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -15,6 +15,7 @@ void trimTrailingNewlines(char* line); int exists(char* path); void touch(char* path); void putFile(char* path, char* contents); +char* allocFile(char* path); // caller must free void getFile(char* path, char* buffer, size_t buffer_size); void putInt(char* path, int value); int getInt(char* path);