diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-12-21 10:16:14 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-12-21 10:16:14 +0300 |
commit | 5fef3c3326dabbe5330647208212ce7ceb311168 (patch) | |
tree | 3d7d2e4aa76e12faeef3c4aef7df864a19f0b7f4 /source/blender/blenlib/intern/storage.c | |
parent | 46af314bd96a9d1e267698df5c074dd4fc4cc6e2 (diff) |
BLI_storage: Split text/binary version of mem-from-file funcs
Fix T47022, caused by own commit de0119d08
Diffstat (limited to 'source/blender/blenlib/intern/storage.c')
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index a107c84d4d0..5c300e94097 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -287,7 +287,7 @@ bool BLI_is_file(const char *path) return (mode && !S_ISDIR(mode)); } -void *BLI_file_read_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size) +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; @@ -305,13 +305,21 @@ void *BLI_file_read_as_mem(const char *filepath, size_t pad_bytes, size_t *r_siz goto finally; } - if (fread(mem, 1, filelen, fp) != filelen) { + const long int filelen_read = fread(mem, 1, filelen, fp); + if ((filelen_read < 0) || (!feof(fp) ) || ferror(fp)) { MEM_freeN(mem); mem = NULL; goto finally; } - *r_size = filelen; + if (filelen_read < filelen) { + mem = MEM_reallocN(mem, filelen_read + pad_bytes); + if (mem == NULL) { + goto finally; + } + } + + *r_size = filelen_read; } finally: @@ -319,6 +327,38 @@ finally: return mem; } +void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size) +{ + FILE *fp = BLI_fopen(filepath, "rb"); + void *mem = NULL; + + if (fp) { + fseek(fp, 0L, SEEK_END); + const long int filelen = ftell(fp); + if (filelen == -1) { + goto finally; + } + fseek(fp, 0L, SEEK_SET); + + 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) || (!feof(fp) ) || ferror(fp)) { + MEM_freeN(mem); + mem = NULL; + goto finally; + } + + *r_size = filelen_read; + } + +finally: + fclose(fp); + return mem; +} /** * Reads the contents of a text file and returns the lines in a linked list. |