[Bf-blender-cvs] [2090ed1] master: Cleanup/refactor spacefile's 'check dir' code.

Bastien Montagne noreply at git.blender.org
Mon Aug 29 16:45:36 CEST 2016


Commit: 2090ed164f674f9b3e29975e7d902eeacae3e55f
Author: Bastien Montagne
Date:   Mon Aug 29 16:42:49 2016 +0200
Branches: master
https://developer.blender.org/rB2090ed164f674f9b3e29975e7d902eeacae3e55f

Cleanup/refactor spacefile's 'check dir' code.

Was kinda split in two different places (one allowed to modify given path to always get a valid one,
the other only checking for validity of given path), not nice - and broken in asset branch case.

So rather extended a bit FileList->checkdirf to handle both cases (modifying and non-modifying path).

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

M	source/blender/editors/space_file/file_intern.h
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/file_utils.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/editors/space_file/filesel.c

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

diff --git a/source/blender/editors/space_file/file_intern.h b/source/blender/editors/space_file/file_intern.h
index a55b18a..71e38f7 100644
--- a/source/blender/editors/space_file/file_intern.h
+++ b/source/blender/editors/space_file/file_intern.h
@@ -128,7 +128,6 @@ void file_panels_register(struct ARegionType *art);
 
 /* file_utils.c */
 void file_tile_boundbox(const ARegion *ar, FileLayout *layout, const int file, rcti *r_bounds);
-bool file_is_dir(struct SpaceFile *sfile, const char *path);
 
 #endif /* __FILE_INTERN_H__ */
 
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index c42ff12..9f5e98d 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -1894,7 +1894,7 @@ void file_directory_enter_handle(bContext *C, void *UNUSED(arg_unused), void *UN
 		file_expand_directory(C);
 
 		/* special case, user may have pasted a filepath into the directory */
-		if (!file_is_dir(sfile, sfile->params->dir)) {
+		if (!filelist_is_dir(sfile->files, sfile->params->dir)) {
 			char tdir[FILE_MAX_LIBEXTRA];
 			char *group, *name;
 
@@ -1920,7 +1920,7 @@ void file_directory_enter_handle(bContext *C, void *UNUSED(arg_unused), void *UN
 
 		BLI_cleanup_dir(G.main->name, sfile->params->dir);
 
-		if (file_is_dir(sfile, sfile->params->dir)) {
+		if (filelist_is_dir(sfile->files, sfile->params->dir)) {
 			/* if directory exists, enter it immediately */
 			ED_file_change_dir(C);
 
@@ -1993,7 +1993,7 @@ void file_filename_enter_handle(bContext *C, void *UNUSED(arg_unused), void *arg
 			BLI_join_dirfile(filepath, sizeof(sfile->params->dir), sfile->params->dir, sfile->params->file);
 
 			/* if directory, open it and empty filename field */
-			if (file_is_dir(sfile, filepath)) {
+			if (filelist_is_dir(sfile->files, filepath)) {
 				BLI_cleanup_dir(G.main->name, filepath);
 				BLI_strncpy(sfile->params->dir, filepath, sizeof(sfile->params->dir));
 				sfile->params->file[0] = '\0';
diff --git a/source/blender/editors/space_file/file_utils.c b/source/blender/editors/space_file/file_utils.c
index f19e301..c1caf5a 100644
--- a/source/blender/editors/space_file/file_utils.c
+++ b/source/blender/editors/space_file/file_utils.c
@@ -48,17 +48,3 @@ void file_tile_boundbox(const ARegion *ar, FileLayout *layout, const int file, r
 	BLI_rcti_init(r_bounds, xmin, xmin + layout->tile_w + layout->tile_border_x,
 	              ymax - layout->tile_h - layout->tile_border_y, ymax);
 }
-
-/* Cannot directly use BLI_is_dir in libloading context... */
-bool file_is_dir(struct SpaceFile *sfile, const char *path)
-{
-	if (sfile->params->type == FILE_LOADLIB) {
-		char tdir[FILE_MAX_LIBEXTRA];
-		char *name;
-		if (BLO_library_path_explode(sfile->params->dir, tdir, NULL, &name) && BLI_is_file(tdir)) {
-			/* .blend file itself and group are considered as directories, not final datablock names. */
-			return name ? false : true;
-		}
-	}
-	return BLI_is_dir(path);
-}
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index b6e4991..1471932 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -309,8 +309,9 @@ typedef struct FileList {
 
 	struct BlendHandle *libfiledata;
 
-	/* Set given path as root directory, may change given string in place to a valid value. */
-	void (*checkdirf)(struct FileList *, char *);
+	/* Set given path as root directory, if last bool is true may change given string in place to a valid value.
+	 * Returns True if valid dir. */
+	bool (*checkdirf)(struct FileList *, char *, const bool);
 
 	/* Fill filelist (to be called by read job). */
 	void (*read_jobf)(struct FileList *, const char *, short *, short *, float *, ThreadMutex *);
@@ -942,24 +943,37 @@ int filelist_geticon(struct FileList *filelist, const int index, const bool is_m
 
 /* ********** Main ********** */
 
-static void filelist_checkdir_dir(struct FileList *UNUSED(filelist), char *r_dir)
+static bool filelist_checkdir_dir(struct FileList *UNUSED(filelist), char *r_dir, const bool do_change)
 {
-	BLI_make_exist(r_dir);
+	if (do_change) {
+		BLI_make_exist(r_dir);
+		return true;
+	}
+	else {
+		return BLI_is_dir(r_dir);
+	}
 }
 
-static void filelist_checkdir_lib(struct FileList *UNUSED(filelist), char *r_dir)
+static bool filelist_checkdir_lib(struct FileList *UNUSED(filelist), char *r_dir, const bool do_change)
 {
-	char dir[FILE_MAX_LIBEXTRA];
-	if (!BLO_library_path_explode(r_dir, dir, NULL, NULL)) {
+	char tdir[FILE_MAX_LIBEXTRA];
+	char *name;
+
+	const bool is_valid = (BLI_is_dir(r_dir) ||
+	                       (BLO_library_path_explode(r_dir, tdir, NULL, &name) && BLI_is_file(tdir) && !name));
+
+	if (do_change && !is_valid) {
 		/* if not a valid library, we need it to be a valid directory! */
 		BLI_make_exist(r_dir);
+		return true;
 	}
+	return is_valid;
 }
 
-static void filelist_checkdir_main(struct FileList *filelist, char *r_dir)
+static bool filelist_checkdir_main(struct FileList *filelist, char *r_dir, const bool do_change)
 {
 	/* TODO */
-	filelist_checkdir_lib(filelist, r_dir);
+	return filelist_checkdir_lib(filelist, r_dir, do_change);
 }
 
 static void filelist_entry_clear(FileDirEntry *entry)
@@ -1378,6 +1392,11 @@ const char *filelist_dir(struct FileList *filelist)
 	return filelist->filelist.root;
 }
 
+bool filelist_is_dir(struct FileList *filelist, const char *path)
+{
+	return filelist->checkdirf(filelist, (char *)path, false);
+}
+
 /**
  * May modify in place given r_dir, which is expected to be FILE_MAX_LIBEXTRA length.
  */
@@ -1386,7 +1405,7 @@ void filelist_setdir(struct FileList *filelist, char *r_dir)
 	BLI_assert(strlen(r_dir) < FILE_MAX_LIBEXTRA);
 
 	BLI_cleanup_dir(G.main->name, r_dir);
-	filelist->checkdirf(filelist, r_dir);
+	BLI_assert(filelist->checkdirf(filelist, r_dir, true));
 
 	if (!STREQ(filelist->filelist.root, r_dir)) {
 		BLI_strncpy(filelist->filelist.root, r_dir, sizeof(filelist->filelist.root));
diff --git a/source/blender/editors/space_file/filelist.h b/source/blender/editors/space_file/filelist.h
index d70faab..f430468 100644
--- a/source/blender/editors/space_file/filelist.h
+++ b/source/blender/editors/space_file/filelist.h
@@ -86,6 +86,7 @@ void                filelist_clear_ex(struct FileList *filelist, const bool do_c
 void                filelist_free(struct FileList *filelist);
 
 const char *        filelist_dir(struct FileList *filelist);
+bool                filelist_is_dir(struct FileList *filelist, const char *path);
 void                filelist_setdir(struct FileList *filelist, char *r_dir);
 
 int                 filelist_files_ensure(struct FileList *filelist);
diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c
index 1a558d4..7abe5ff 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -592,7 +592,7 @@ void ED_file_change_dir(bContext *C)
 		sfile->params->filter_search[0] = '\0';
 		sfile->params->active_file = -1;
 
-		if (!file_is_dir(sfile, sfile->params->dir)) {
+		if (!filelist_is_dir(sfile->files, sfile->params->dir)) {
 			BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
 			/* could return but just refresh the current dir */
 		}




More information about the Bf-blender-cvs mailing list