From a345f56ce3331a0f1e2436142ac11654626752fe Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 30 Jul 2019 10:43:07 +1000 Subject: Cleanup: de-duplicate file reading code Also remove goto's. --- source/blender/blenlib/intern/storage.c | 123 ++++++++++++++------------------ 1 file changed, 52 insertions(+), 71 deletions(-) (limited to 'source/blender/blenlib/intern/storage.c') diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index 39af73ac175..4ba198ac0b8 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -297,56 +297,72 @@ bool BLI_is_file(const char *path) return (mode && !S_ISDIR(mode)); } -void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size) +/** + * Use for both text and binary file reading. + */ +static void *file_read_data_as_mem_impl(FILE *fp, + bool read_size_exact, + size_t pad_bytes, + size_t *r_size) { - FILE *fp = BLI_fopen(filepath, "r"); - void *mem = NULL; + struct stat st; + if (fstat(fileno(fp), &st) == -1) { + return NULL; + } + if (S_ISDIR(st.st_mode)) { + return NULL; + } + if (fseek(fp, 0L, SEEK_END) == -1) { + return NULL; + } + /* Don't use the 'st_size' because it may be the symlink. */ + const long int filelen = ftell(fp); + if (filelen == -1) { + return NULL; + } + if (fseek(fp, 0L, SEEK_SET) == -1) { + return NULL; + } - if (fp) { - struct stat st; - if (fstat(fileno(fp), &st) == -1) { - goto finally; - } - if (S_ISDIR(st.st_mode)) { - goto finally; - } - if (fseek(fp, 0L, SEEK_END) == -1) { - goto finally; - } - /* Don't use the 'st_size' because it may be the symlink. */ - const long int filelen = ftell(fp); - if (filelen == -1) { - goto finally; - } - if (fseek(fp, 0L, SEEK_SET) == -1) { - goto finally; - } + void *mem = MEM_mallocN(filelen + pad_bytes, __func__); + if (mem == NULL) { + return NULL; + } - mem = MEM_mallocN(filelen + pad_bytes, __func__); - if (mem == NULL) { - goto finally; - } + const long int filelen_read = fread(mem, 1, filelen, fp); + if ((filelen_read < 0) || ferror(fp)) { + MEM_freeN(mem); + return NULL; + } - const long int filelen_read = fread(mem, 1, filelen, fp); - if ((filelen_read < 0) || ferror(fp)) { + if (read_size_exact) { + if (filelen_read != filelen) { MEM_freeN(mem); - mem = NULL; - goto finally; + return NULL; } - + } + else { if (filelen_read < filelen) { mem = MEM_reallocN(mem, filelen_read + pad_bytes); if (mem == NULL) { - goto finally; + return NULL; } } + } - *r_size = filelen_read; + *r_size = filelen_read; + + return mem; +} - finally: +void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size) +{ + FILE *fp = BLI_fopen(filepath, "r"); + void *mem = NULL; + if (fp) { + mem = file_read_data_as_mem_impl(fp, true, pad_bytes, r_size); fclose(fp); } - return mem; } @@ -354,45 +370,10 @@ void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t { FILE *fp = BLI_fopen(filepath, "rb"); void *mem = NULL; - if (fp) { - struct stat st; - if (fstat(fileno(fp), &st) == -1) { - goto finally; - } - if (S_ISDIR(st.st_mode)) { - goto finally; - } - if (fseek(fp, 0L, SEEK_END) == -1) { - goto finally; - } - /* Don't use the 'st_size' because it may be the symlink. */ - const long int filelen = ftell(fp); - if (filelen == -1) { - goto finally; - } - if (fseek(fp, 0L, SEEK_SET) == -1) { - goto finally; - } - - mem = MEM_mallocN(filelen + pad_bytes, __func__); - if (mem == NULL) { - goto finally; - } - - const long int filelen_read = fread(mem, 1, filelen, fp); - if ((filelen_read != filelen) || ferror(fp)) { - MEM_freeN(mem); - mem = NULL; - goto finally; - } - - *r_size = filelen_read; - - finally: + mem = file_read_data_as_mem_impl(fp, false, pad_bytes, r_size); fclose(fp); } - return mem; } -- cgit v1.2.3