From 7f524c427597cc998f243769b0e22e4f450c55cf Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 24 Apr 2019 21:22:53 +0200 Subject: Progressing on bytecode (to c), fix ssa resolving multiple times --- src/std/file.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/std/file.c') 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); -- cgit v1.2.3