added allocFile to utils

This commit is contained in:
Shaun Inman 2023-01-28 11:11:21 -05:00
parent 9c72a4f261
commit 3d24d19659
2 changed files with 15 additions and 0 deletions

View file

@ -120,6 +120,20 @@ void getFile(char* path, char* buffer, size_t buffer_size) {
buffer[size] = '\0'; 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 getInt(char* path) {
int i = 0; int i = 0;
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");

View file

@ -15,6 +15,7 @@ void trimTrailingNewlines(char* line);
int exists(char* path); int exists(char* path);
void touch(char* path); void touch(char* path);
void putFile(char* path, char* contents); void putFile(char* path, char* contents);
char* allocFile(char* path); // caller must free
void getFile(char* path, char* buffer, size_t buffer_size); void getFile(char* path, char* buffer, size_t buffer_size);
void putInt(char* path, int value); void putInt(char* path, int value);
int getInt(char* path); int getInt(char* path);