[Bf-blender-cvs] [c728f53] asset-experiments: Fix issues when data name have slash inside.

Bastien Montagne noreply at git.blender.org
Thu Dec 4 18:49:47 CET 2014


Commit: c728f537ae3e65ecdba260c97347b81fa89c7c46
Author: Bastien Montagne
Date:   Thu Dec 4 16:51:46 2014 +0100
Branches: asset-experiments
https://developer.blender.org/rBc728f537ae3e65ecdba260c97347b81fa89c7c46

Fix issues when data name have slash inside.

Adding `BLO_library_path_explode` simplies things. We might end getting rid of
`BLO_is_a_library` (or rather, merge back both things) later, though.

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

M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/windowmanager/intern/wm_operators.c

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

diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index 4b7b9ce..11f4379 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -199,6 +199,15 @@ BLO_blendhandle_close(BlendHandle *bh);
 bool BLO_has_bfile_extension(const char *str);
 
 /**
+ * \param path the full path to explode.
+ * \param r_dir the string that'll contain path up to blend file itself ('library' path).
+ * \param r_group the string that'll contain 'group' part of the path, if any. May be NULL.
+ * \param r_name the string that'll contain data's name part of the path, if any. May be NULL.
+ * \return true if path contains a blend file.
+ */
+bool BLO_library_path_explode(const char *path, char *r_dir, char *r_group, char *r_name);
+
+/**
  * return ok when a blenderfile, in dir is the filename,
  * in group the type of libdata
  */
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index ac903c7..bec5276 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -1138,12 +1138,61 @@ void blo_freefiledata(FileData *fd)
 
 /* ************ DIV ****************** */
 
+/* XXX All this should be made safer regarding mem and strings handling! */
+
 bool BLO_has_bfile_extension(const char *str)
 {
 	const char *ext_test[4] = {".blend", ".ble", ".blend.gz", NULL};
 	return BLI_testextensie_array(str, ext_test);
 }
 
+bool BLO_library_path_explode(const char *path, char *r_dir, char *r_group, char *r_name)
+{
+	/* We might get some data names with slashes, so we have to go up in path until we find blend file itself,
+	 * then we now next path item is group, and everything else is data name. */
+	char *slash, *prev_slash = NULL;
+
+	strcpy(r_dir, path);
+	if (r_group) {
+		r_group[0] = '\0';
+	}
+	if (r_name) {
+		r_name[0] = '\0';
+	}
+
+	while ((slash = (char *)BLI_last_slash(r_dir))) {
+		*slash = '\0';
+		if (BLO_has_bfile_extension(r_dir)) {
+			break;
+		}
+
+		if (prev_slash) {
+			*prev_slash = '/';
+		}
+		prev_slash = slash;
+	}
+
+	if (r_dir[0] == '\0') {
+		return false;
+	}
+
+	if (slash) {
+		BLI_assert(strlen(slash + 1) <= BLO_GROUP_MAX);
+		if (r_group) {
+			strcpy(r_group, slash + 1);
+		}
+	}
+
+	if (prev_slash) {
+		BLI_assert(strlen(prev_slash + 1) <= MAX_ID_NAME - 2);
+		if (r_name) {
+			strcpy(r_name, prev_slash + 1);
+		}
+	}
+
+	return true;
+}
+
 bool BLO_is_a_library(const char *path, char *dir, char *group)
 {
 	/* return ok when a blenderfile, in dir is the filename,
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index c99d73e..1d90237 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -47,6 +47,8 @@
 #include "BKE_global.h"
 #include "BKE_main.h"
 
+#include "BLO_readfile.h"
+
 #include "BLF_translation.h"
 
 #include "IMB_imbuf_types.h"
@@ -254,7 +256,7 @@ static void file_draw_string(int sx, int sy, const char *string, float width, in
 
 	fs.align = align;
 
-	BLI_strncpy(fname, BLI_path_basename(string), FILE_MAXFILE);
+	BLI_strncpy(fname, string, FILE_MAXFILE);
 	file_shorten_string(fname, width + 1.0f, 0);
 
 	/* no text clipping needed, UI_fontstyle_draw does it but is a bit too strict (for buttons it works) */
@@ -469,12 +471,21 @@ void file_draw_list(const bContext *C, ARegion *ar)
 	align = (FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
 
 	for (i = offset; (i < numfiles) && (i < offset + numfiles_layout); i++) {
+		char path[FILE_MAX_LIBEXTRA], dir[FILE_MAXDIR], group[BLO_GROUP_MAX], name_buff[MAX_ID_NAME], *name;
 		ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
 		sx += (int)(v2d->tot.xmin + 0.1f * UI_UNIT_X);
 		sy = (int)(v2d->tot.ymax - sy);
 
 		file = filelist_file(files, i);
-		
+
+		BLI_join_dirfile(path, sizeof(path), filelist_dir(files), file->relname);
+		if (BLO_library_path_explode(path, dir, group, name_buff)) {
+			name = (name_buff[0] != '\0') ? name_buff : group;
+		}
+		else {
+			name = file->relname;
+		}
+
 		UI_ThemeColor4(TH_TEXT);
 
 
@@ -537,7 +548,7 @@ void file_draw_list(const bContext *C, ARegion *ar)
 
 		if (!(file->selflag & EDITING_FILE)) {
 			int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
-			file_draw_string(sx + 1, tpos, file->relname, (float)textwidth, textheight, align);
+			file_draw_string(sx + 1, tpos, name, (float)textwidth, textheight, align);
 		}
 
 		if (params->display == FILE_SHORTDISPLAY) {
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index df16dac..9729ef3 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -747,13 +747,10 @@ int filelist_geticon(struct FileList *filelist, const int index)
 		return ICON_FILE_TEXT;
 	else {
 		char path[FILE_MAX_LIBEXTRA], dir[FILE_MAXDIR], group[BLO_GROUP_MAX];
-		char *slash;
 
 		BLI_join_dirfile(path, sizeof(path), filelist->dir, file->relname);
-		slash = (char *)BLI_last_slash(path);
-		slash[1] = '\0';
 
-		if (BLO_is_a_library(path, dir, group)) {
+		if (BLO_library_path_explode(path, dir, group, NULL)) {
 			int idcode = groupname_to_code(group);
 
 			/* TODO: this should most likely be completed and moved to UI_interface_icons.h ? unless it already exists somewhere... */
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index d387349..a1e2945 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -2567,32 +2567,21 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
 	BlendHandle *bh;
 	Library *lib;
 	PropertyRNA *prop;
-	char name[FILE_MAXFILE], root_dir[FILE_MAXDIR], dir[FILE_MAXDIR], libname[FILE_MAX], group[BLO_GROUP_MAX];
-	char *slash;
+	char path[FILE_MAX_LIBEXTRA], dir[FILE_MAXDIR], libname[FILE_MAX], group[BLO_GROUP_MAX], name[FILE_MAXFILE];
 	int idcode, totfiles = 0;
 	short flag;
 
 	RNA_string_get(op->ptr, "filename", name);
-	RNA_string_get(op->ptr, "directory", root_dir);
+	RNA_string_get(op->ptr, "directory", dir);
 
-	if ((slash = BLI_last_slash(name))) {
-		*slash = '\0';
-		BLI_strncpy(dir, root_dir, sizeof(dir) - 1);
-		BLI_path_append(dir, sizeof(dir) - 1, name);
-		BLI_add_slash(dir);
-		slash++;
-		BLI_strncpy(name, slash, sizeof(name) - (slash - name));
-	}
-	else {
-		BLI_strncpy(dir, root_dir, sizeof(dir));
-	}
+	BLI_join_dirfile(path, sizeof(path), dir, name);
 
 	/* test if we have a valid data */
-	if (BLO_is_a_library(dir, libname, group) == 0) {
+	if (!BLO_library_path_explode(path, libname, group, name)) {
 		BKE_report(op->reports, RPT_ERROR, "Not a library");
 		return OPERATOR_CANCELLED;
 	}
-	else if (group[0] == 0) {
+	else if (group[0] == '\0') {
 		BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
 		return OPERATOR_CANCELLED;
 	}
@@ -2660,24 +2649,13 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
 		BLO_library_append_named_part_ex(C, mainl, &bh, name, idcode, flag);
 	}
 	else {
-		char tmp_dir[FILE_MAXDIR];
 		RNA_BEGIN (op->ptr, itemptr, "files")
 		{
 			RNA_string_get(&itemptr, "name", name);
 
-			if ((slash = BLI_last_slash(name))) {
-				*slash = '\0';
-				BLI_strncpy(dir, root_dir, sizeof(dir) - 1);
-				BLI_path_append(dir, sizeof(dir) - 1, name);
-				BLI_add_slash(dir);
-				slash++;
-				BLI_strncpy(name, slash, sizeof(name) - (slash - name));
-			}
-			else {
-				BLI_strncpy(dir, root_dir, sizeof(dir));
-			}
+			BLI_join_dirfile(path, sizeof(path), dir, name);
 
-			if (BLO_is_a_library(dir, libname, group)) {
+			if (BLO_library_path_explode(path, libname, group, name)) {
 				idcode = BKE_idcode_from_name(group);
 				BLO_library_append_named_part_ex(C, mainl, &bh, name, idcode, flag);
 			}




More information about the Bf-blender-cvs mailing list