added allocFile to utils
This commit is contained in:
parent
9c72a4f261
commit
3d24d19659
2 changed files with 15 additions and 0 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue