[Bf-blender-cvs] [5fef3c3] master: BLI_storage: Split text/binary version of mem-from-file funcs

Campbell Barton noreply at git.blender.org
Mon Dec 21 08:20:33 CET 2015


Commit: 5fef3c3326dabbe5330647208212ce7ceb311168
Author: Campbell Barton
Date:   Mon Dec 21 18:16:14 2015 +1100
Branches: master
https://developer.blender.org/rB5fef3c3326dabbe5330647208212ce7ceb311168

BLI_storage: Split text/binary version of mem-from-file funcs

Fix T47022, caused by own commit de0119d08

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

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

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

diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index cdc4855..299fea2 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -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;
 	}
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index b842c30..91d139c 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -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 ... */
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index a107c84..5c300e9 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.
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index 542c7fe..3a995be 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -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;




More information about the Bf-blender-cvs mailing list