[Bf-blender-cvs] [a345f56ce33] master: Cleanup: de-duplicate file reading code

Campbell Barton noreply at git.blender.org
Tue Jul 30 02:46:11 CEST 2019


Commit: a345f56ce3331a0f1e2436142ac11654626752fe
Author: Campbell Barton
Date:   Tue Jul 30 10:43:07 2019 +1000
Branches: master
https://developer.blender.org/rBa345f56ce3331a0f1e2436142ac11654626752fe

Cleanup: de-duplicate file reading code

Also remove goto's.

===================================================================

M	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;
 }



More information about the Bf-blender-cvs mailing list