aboutsummaryrefslogtreecommitdiff
path: root/src/std/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/std/file.c')
-rw-r--r--src/std/file.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/std/file.c b/src/std/file.c
index 7de2b6c..84f4b0c 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -192,11 +192,12 @@ static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) {
return 0;
}
-int read_whole_file(const char *filepath, char **data, usize *size) {
+int read_whole_file(const char *filepath, Buffer *data_result) {
FileType file_type;
FILE *file;
int result;
usize bytes_read;
+ usize size;
return_if_error(file_get_type(filepath, &file_type));
if(file_type != REGULAR) {
@@ -218,24 +219,24 @@ int read_whole_file(const char *filepath, char **data, usize *size) {
goto cleanup;
}
- *size = ftell(file);
+ size = ftell(file);
result = fseek(file, 0, SEEK_SET);
if(result != 0) {
result = ferror(file);
goto cleanup;
}
- if(*size > MAX_FILE_SIZE) {
+ if(size > MAX_FILE_SIZE) {
amal_log_error("File %s is too large (larger than 48 megabytes)", filepath);
result = -1;
goto cleanup;
}
- cleanup_if_error(am_malloc(*size, (void**)data));
- bytes_read = fread(*data, 1, *size, file);
- if(bytes_read != *size) {
+ assert(data_result->capacity == 0 && data_result->size == 0);
+ cleanup_if_error(buffer_append_empty(data_result, size));
+ bytes_read = fread(data_result->data, 1, data_result->size, file);
+ if(bytes_read != size)
result = ferror(file);
- }
cleanup:
fclose(file);