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
path: root/source
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2015-12-21 10:16:14 +0300
committerCampbell Barton <ideasman42@gmail.com>2015-12-21 10:16:14 +0300
commit5fef3c3326dabbe5330647208212ce7ceb311168 (patch)
tree3d7d2e4aa76e12faeef3c4aef7df864a19f0b7f4 /source
parent46af314bd96a9d1e267698df5c074dd4fc4cc6e2 (diff)
BLI_storage: Split text/binary version of mem-from-file funcs
Fix T47022, caused by own commit de0119d08
Diffstat (limited to 'source')
-rw-r--r--source/blender/blenkernel/intern/text.c4
-rw-r--r--source/blender/blenlib/BLI_fileops.h3
-rw-r--r--source/blender/blenlib/intern/storage.c46
-rw-r--r--source/blender/editors/curve/editfont.c2
4 files changed, 48 insertions, 7 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index cdc48551b11..299fea2e897 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 b842c30c975..91d139c7085 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 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.
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index 542c7fe5d90..3a995be0f7c 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;