[Bf-blender-cvs] [2735f6f] master: Replace 'BLO_is_a_library' by 'BLO_library_path_explode'.

Bastien Montagne noreply at git.blender.org
Tue Aug 18 13:21:09 CEST 2015


Commit: 2735f6fda66076c34f853afaccfc4dd78f66ca1f
Author: Bastien Montagne
Date:   Tue Aug 18 13:18:50 2015 +0200
Branches: master
https://developer.blender.org/rB2735f6fda66076c34f853afaccfc4dd78f66ca1f

Replace 'BLO_is_a_library' by 'BLO_library_path_explode'.

This new func will be fully used by upcomming code (it mostly adds
the extraction of library item name as well as library file and ID group).

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

M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
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 6fdcf70..5f881c0 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -195,10 +195,13 @@ BLO_blendhandle_close(BlendHandle *bh);
 bool BLO_has_bfile_extension(const char *str);
 
 /**
- * return ok when a blenderfile, in dir is the filename,
- * in group the type of libdata
+ * \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_is_a_library(const char *path, char *dir, char *group);
+bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name);
 
 
 /**
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 32b736b..6581fb3 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -1234,47 +1234,60 @@ bool BLO_has_bfile_extension(const char *str)
 	return BLI_testextensie_array(str, ext_test);
 }
 
-bool BLO_is_a_library(const char *path, char *dir, char *group)
+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
-	 */
-	int len;
-	char *fd;
-	
-	/* if path leads to a directory we can be sure we're not in a library */
-	if (BLI_is_dir(path)) return 0;
+	/* 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 = NULL, *prev_slash = NULL, c = '\0';
 
-	strcpy(dir, path);
-	len = strlen(dir);
-	if (len < 7) return 0;
-	if ((dir[len - 1] != '/') && (dir[len - 1] != '\\')) return 0;
-	
-	group[0] = '\0';
-	dir[len - 1] = '\0';
+	r_dir[0] = '\0';
+	if (r_group) {
+		*r_group = NULL;
+	}
+	if (r_name) {
+		*r_name = NULL;
+	}
+
+	/* if path leads to an existing directory, we can be sure we're not (in) a library */
+	if (BLI_is_dir(path)) {
+		return false;
+	}
+
+	strcpy(r_dir, path);
 
-	/* Find the last slash */
-	fd = (char *)BLI_last_slash(dir);
+	while ((slash = (char *)BLI_last_slash(r_dir))) {
+		char tc = *slash;
+		*slash = '\0';
+		if (BLO_has_bfile_extension(r_dir)) {
+			break;
+		}
 
-	if (fd == NULL) return 0;
-	*fd = 0;
-	if (BLO_has_bfile_extension(fd+1)) {
-		/* the last part of the dir is a .blend file, no group follows */
-		*fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
+		if (prev_slash) {
+			*prev_slash = c;
+		}
+		prev_slash = slash;
+		c = tc;
 	}
-	else {
-		const char * const gp = fd + 1; // in case we have a .blend file, gp points to the group
-		
-		/* Find the last slash */
-		fd = (char *)BLI_last_slash(dir);
-		if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
-		
-		/* now we know that we are in a blend file and it is safe to 
-		 * assume that gp actually points to a group */
-		if (!STREQ("Screen", gp))
-			BLI_strncpy(group, gp, BLO_GROUP_MAX);
+
+	if (!slash) {
+		return false;
 	}
-	return 1;
+
+	if (slash[1] != '\0') {
+		BLI_assert(strlen(slash + 1) < BLO_GROUP_MAX);
+		if (r_group) {
+			*r_group = slash + 1;
+		}
+	}
+
+	if (prev_slash && (prev_slash[1] != '\0')) {
+		BLI_assert(strlen(prev_slash + 1) < MAX_ID_NAME - 2);
+		if (r_name) {
+			*r_name = prev_slash + 1;
+		}
+	}
+
+	return true;
 }
 
 /* ************** OLD POINTERS ******************* */
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index dddda43..6bbde1e 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -1347,8 +1347,8 @@ int file_parent_exec(bContext *C, wmOperator *UNUSED(unused))
 			BLI_cleanup_dir(G.main->name, sfile->params->dir);
 			/* if not browsing in .blend file, we still want to check whether the path is a directory */
 			if (sfile->params->type == FILE_LOADLIB) {
-				char tdir[FILE_MAX], tgroup[FILE_MAX];
-				if (BLO_is_a_library(sfile->params->dir, tdir, tgroup)) {
+				char tdir[FILE_MAX];
+				if (BLO_library_path_explode(sfile->params->dir, tdir, NULL, NULL)) {
 					ED_file_change_dir(C, false);
 				}
 				else {
@@ -1842,9 +1842,9 @@ void file_filename_enter_handle(bContext *C, void *UNUSED(arg_unused), void *arg
 				WM_event_add_notifier(C, NC_SPACE | ND_SPACE_FILE_PARAMS, NULL);
 			}
 			else if (sfile->params->type == FILE_LOADLIB) {
-				char tdir[FILE_MAX], tgroup[FILE_MAX];
+				char tdir[FILE_MAX];
 				BLI_add_slash(filepath);
-				if (BLO_is_a_library(filepath, tdir, tgroup)) {
+				if (BLO_library_path_explode(filepath, tdir, NULL, NULL)) {
 					BLI_cleanup_dir(G.main->name, filepath);
 					BLI_strncpy(sfile->params->dir, filepath, sizeof(sfile->params->dir));
 					sfile->params->file[0] = '\0';
@@ -2022,8 +2022,8 @@ static int file_rename_poll(bContext *C)
 			poll = 0;
 		}
 		else {
-			char dir[FILE_MAX], group[FILE_MAX];
-			if (filelist_islibrary(sfile->files, dir, group)) poll = 0;
+			char dir[FILE_MAX];
+			if (filelist_islibrary(sfile->files, dir, NULL)) poll = 0;
 		}
 	}
 	else
@@ -2050,12 +2050,12 @@ static int file_delete_poll(bContext *C)
 	SpaceFile *sfile = CTX_wm_space_file(C);
 
 	if (sfile && sfile->params) {
-		char dir[FILE_MAX], group[FILE_MAX];
+		char dir[FILE_MAX];
 		int numfiles = filelist_numfiles(sfile->files);
 		int i;
 		int num_selected = 0;
 
-		if (filelist_islibrary(sfile->files, dir, group)) poll = 0;
+		if (filelist_islibrary(sfile->files, dir, NULL)) poll = 0;
 		for (i = 0; i < numfiles; i++) {
 			if (filelist_is_selected(sfile->files, i, CHECK_FILES)) {
 				num_selected++;
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 3358812..21a072d 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -470,9 +470,9 @@ static bool is_filtered_file(struct direntry *file, const char *UNUSED(root), Fi
 static bool is_filtered_lib(struct direntry *file, const char *root, FileListFilter *filter)
 {
 	bool is_filtered = !is_hidden_file(file->relname, filter);
-	char dir[FILE_MAXDIR], group[BLO_GROUP_MAX];
+	char dir[FILE_MAXDIR];
 
-	if (BLO_is_a_library(root, dir, group)) {
+	if (BLO_library_path_explode(root, dir, NULL, NULL)) {
 		is_filtered = !is_hidden_file(file->relname, filter);
 		if (is_filtered && filter->filter && !FILENAME_IS_CURRPAR(file->relname)) {
 			if (is_filtered && (filter->filter_search[0] != '\0')) {
@@ -1044,9 +1044,9 @@ bool filelist_is_selected(struct FileList *filelist, int index, FileCheckType ch
 }
 
 
-bool filelist_islibrary(struct FileList *filelist, char *dir, char *group)
+bool filelist_islibrary(struct FileList *filelist, char *dir, char **group)
 {
-	return BLO_is_a_library(filelist->dir, dir, group);
+	return BLO_library_path_explode(filelist->dir, dir, group, NULL);
 }
 
 static int groupname_to_code(const char *group)
@@ -1068,10 +1068,10 @@ static void filelist_from_library(struct FileList *filelist)
 	struct ImBuf *ima;
 	int ok, i, nprevs, nnames, idcode;
 	char filename[FILE_MAX];
-	char dir[FILE_MAX], group[BLO_GROUP_MAX];
+	char dir[FILE_MAX], *group;
 
 	/* name test */
-	ok = filelist_islibrary(filelist, dir, group);
+	ok = filelist_islibrary(filelist, dir, &group);
 	if (!ok) {
 		/* free */
 		if (filelist->libfiledata) BLO_blendhandle_close(filelist->libfiledata);
@@ -1088,7 +1088,7 @@ static void filelist_from_library(struct FileList *filelist)
 		if (filelist->libfiledata == NULL) return;
 	}
 	
-	idcode = groupname_to_code(group);
+	idcode = group ? groupname_to_code(group) : 0;
 
 	/* memory for strings is passed into filelist[i].relname
 	 * and freed in freefilelist */
diff --git a/source/blender/editors/space_file/filelist.h b/source/blender/editors/space_file/filelist.h
index 44e0a51..83f7718 100644
--- a/source/blender/editors/space_file/filelist.h
+++ b/source/blender/editors/space_file/filelist.h
@@ -98,7 +98,7 @@ void                filelist_select_file(struct FileList *filelist, int index, F
 bool                filelist_is_selected(struct FileList *filelist, int index, FileCheckType check);
 
 struct BlendHandle *filelist_lib(struct FileList *filelist);
-bool                filelist_islibrary(struct FileList *filelist, char *dir, char *group);
+bool                filelist_islibrary(struct FileList *filelist, char *dir, char **group);
 void                filelist_freelib(struct FileList *filelist);
 
 bool                filelist_need_thumbnails(struct FileList *filelist);
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index f5706c1..6443651 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -2610,7 +2610,7 @@ static int wm_link_append_exec(bContext *C, wmOperator *op)
 	RNA_string_get(op->ptr, "directory", dir);
 
 	/* test if we have a valid data */
-	if (BLO_is_a_library(dir, libname, group) == 0) {
+	if (BLO_library_path_explode(dir, libname, group, NULL) == 0) {
 		BKE_report(op->reports, RPT_ERROR, "Not a library");
 		return OPERATOR_CANCELLED;
 	}




More information about the Bf-blender-cvs mailing list