BLI_storage: Split text/binary version of mem-from-file funcs
authorCampbell Barton <ideasman42@gmail.com>
Mon, 21 Dec 2015 07:16:14 +0000 (18:16 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 21 Dec 2015 07:16:14 +0000 (18:16 +1100)
Fix T47022, caused by own commit de0119d08

source/blender/blenkernel/intern/text.c
source/blender/blenlib/BLI_fileops.h
source/blender/blenlib/intern/storage.c
source/blender/editors/curve/editfont.c

index cdc48551b11e44365295fbae45a91adf817cdfa8..299fea2e89789bda9c318d6a5143110a00492aa0 100644 (file)
@@ -362,7 +362,7 @@ bool BKE_text_reload(Text *text)
        BLI_strncpy(filepath_abs, text->name, FILE_MAX);
        BLI_path_abs(filepath_abs, G.main->name);
        
-       buffer = BLI_file_read_as_mem(filepath_abs, 0, &buffer_len);
+       buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len);
        if (buffer == NULL) {
                return false;
        }
@@ -401,7 +401,7 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
        if (relpath) /* can be NULL (bg mode) */
                BLI_path_abs(filepath_abs, relpath);
        
-       buffer = BLI_file_read_as_mem(filepath_abs, 0, &buffer_len);
+       buffer = BLI_file_read_text_as_mem(filepath_abs, 0, &buffer_len);
        if (buffer == NULL) {
                return false;
        }
index b842c30c97528800e8fd51d36bb4513100d22020..91d139c7085876ad16c3db66b624ff4a67b10eeb 100644 (file)
@@ -130,7 +130,8 @@ bool   BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RES
 
 /* read ascii file as lines, empty list if reading fails */
 struct LinkNode *BLI_file_read_as_lines(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
-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);
+void  *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
 void   BLI_file_free_lines(struct LinkNode *lines);
 
 /* this weirdo pops up in two places ... */
index a107c84d4d046f389d3d4adfbe461d615704b348..5c300e94097384bcc776de058a80972f3821a58e 100644 (file)
@@ -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.
index 542c7fe5d90f4ea1ce9be8ddd36d4a391df84276..3a995be0f7c748e658352dbe8bf238b04f54d2c6 100644 (file)
@@ -349,7 +349,7 @@ static int paste_from_file(bContext *C, ReportList *reports, const char *filenam
        size_t filelen;
        int retval;
 
-       strp = BLI_file_read_as_mem(filename, 1, &filelen);
+       strp = BLI_file_read_text_as_mem(filename, 1, &filelen);
        if (strp == NULL) {
                BKE_reportf(reports, RPT_ERROR, "Failed to open file '%s'", filename);
                return OPERATOR_CANCELLED;