BLI_storage: Split text/binary version of mem-from-file funcs
Fix T47022, caused by own commit de0119d08
This commit is contained in:
parent
46af314bd9
commit
5fef3c3326
Notes:
blender-bot
2023-02-14 08:21:50 +01:00
Referenced by issue #47022, Blender: Bug with opening templates Referenced by issue #46992, Can't reload .py from blender\2.76\scripts\addons folder
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 ... */
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue