Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2019-07-30 03:43:07 +0300
committerCampbell Barton <ideasman42@gmail.com>2019-07-30 03:43:07 +0300
commita345f56ce3331a0f1e2436142ac11654626752fe (patch)
treee140cdf63508e23b38986266e250b8093dc46d0a /source/blender/blenlib
parentf1516ae637e6e86443fbaa95849c2553ab6a421c (diff)
Cleanup: de-duplicate file reading code
Also remove goto's.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/intern/storage.c123
1 files changed, 52 insertions, 71 deletions
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;
}