[Bf-blender-cvs] [0333cf00baf] master: Fix BLI_path_frame_strip

Sybren A. Stüvel noreply at git.blender.org
Wed Mar 20 13:45:44 CET 2019


Commit: 0333cf00baf4d5b796347334af1f15d5bb9a2df4
Author: Sybren A. Stüvel
Date:   Wed Mar 20 12:59:11 2019 +0100
Branches: master
https://developer.blender.org/rB0333cf00baf4d5b796347334af1f15d5bb9a2df4

Fix BLI_path_frame_strip

The `BLI_path_frame_strip` function was completely broken, unless the
number of digits in the sequence number was the same as the length of
the extension. In other words, it would work fine for `file.0001.abc` (4
digit `0001` and 4 char `.abc`), but other combinations would truncate
to the shortest (`file.001.abc` would become `file.###.ab` and
`file.00001.a` would become `file.##.a`). The dependency between the
sequence number and the file extension is now removed.

The behaviour has changed a little bit in the case where there are no
numbers in the filename. Previously, `path="filename.abc"` would result
in `path="filename.abc"` and `ext=""`, but now it results in
`path="filename"` and `ext=".abc"`. This way `ext` always contains the
extension, and the behaviour is consistent regardless of whether there
were any numbers found.

Furthermore, I've removed the `bool set_frame_char` parameter, because
it was unclear, probably also buggy, and most importantly, never used.

I've also added a unit test for the `BLI_path_frame_strip` function.

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

M	source/blender/blenkernel/intern/cachefile.c
M	source/blender/blenlib/BLI_path_util.h
M	source/blender/blenlib/intern/path_util.c
M	source/blender/editors/space_sequencer/sequencer_add.c
M	tests/gtests/blenlib/BLI_path_util_test.cc

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

diff --git a/source/blender/blenkernel/intern/cachefile.c b/source/blender/blenkernel/intern/cachefile.c
index b4f64087f8c..2c9e4966fc9 100644
--- a/source/blender/blenkernel/intern/cachefile.c
+++ b/source/blender/blenkernel/intern/cachefile.c
@@ -210,7 +210,7 @@ bool BKE_cachefile_filepath_get(
 
 	if (cache_file->is_sequence && BLI_path_frame_get(r_filepath, &fframe, &frame_len)) {
 		char ext[32];
-		BLI_path_frame_strip(r_filepath, true, ext);
+		BLI_path_frame_strip(r_filepath, ext);
 		BLI_path_frame(r_filepath, frame, frame_len);
 		BLI_path_extension_ensure(r_filepath, FILE_MAX, ext);
 
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h
index ac87fd21a92..ddb9828129c 100644
--- a/source/blender/blenlib/BLI_path_util.h
+++ b/source/blender/blenlib/BLI_path_util.h
@@ -92,7 +92,7 @@ bool BLI_path_abs(char *path, const char *basepath)  ATTR_NONNULL();
 bool BLI_path_frame(char *path, int frame, int digits) ATTR_NONNULL();
 bool BLI_path_frame_range(char *path, int sta, int end, int digits) ATTR_NONNULL();
 bool BLI_path_frame_get(char *path, int *r_frame, int *numdigits) ATTR_NONNULL();
-void BLI_path_frame_strip(char *path, bool set_frame_char, char *ext) ATTR_NONNULL();
+void BLI_path_frame_strip(char *path, char *ext) ATTR_NONNULL();
 bool BLI_path_frame_check_chars(const char *path) ATTR_NONNULL();
 bool BLI_path_cwd(char *path, const size_t maxlen) ATTR_NONNULL();
 void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 77b137a9d6d..6d69f29a228 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -862,7 +862,7 @@ bool BLI_path_frame_get(char *path, int *r_frame, int *r_numdigits)
 	return false;
 }
 
-void BLI_path_frame_strip(char *path, bool set_frame_char, char *ext)
+void BLI_path_frame_strip(char *path, char *r_ext)
 {
 	if (*path) {
 		char *file = (char *)BLI_last_slash(path);
@@ -896,16 +896,14 @@ void BLI_path_frame_strip(char *path, bool set_frame_char, char *ext)
 
 		c++;
 
-		if (numdigits) {
-			/* replace the number with the suffix and terminate the string */
-			while (numdigits--) {
-				*ext++ = *suffix;
-				*c++ = set_frame_char ? '#' : *suffix;
-				suffix++;
-			}
-			*c = '\0';
-			*ext = '\0';
+		int suffix_length = len - (suffix - file);
+		BLI_strncpy(r_ext, suffix, suffix_length+1);
+
+		/* replace the number with the suffix and terminate the string */
+		while (numdigits--) {
+			*c++ = '#';
 		}
+		*c = '\0';
 	}
 }
 
diff --git a/source/blender/editors/space_sequencer/sequencer_add.c b/source/blender/editors/space_sequencer/sequencer_add.c
index b408369dc04..34211f4d907 100644
--- a/source/blender/editors/space_sequencer/sequencer_add.c
+++ b/source/blender/editors/space_sequencer/sequencer_add.c
@@ -835,7 +835,7 @@ void sequencer_image_seq_reserve_frames(wmOperator *op, StripElem *se, int len,
 		char ext[PATH_MAX];
 		char filename_stripped[PATH_MAX];
 		/* strip the frame from filename and substitute with # */
-		BLI_path_frame_strip(filename, true, ext);
+		BLI_path_frame_strip(filename, ext);
 
 		for (i = 0; i < len; i++, se++) {
 			BLI_strncpy(filename_stripped, filename, sizeof(filename_stripped));
diff --git a/tests/gtests/blenlib/BLI_path_util_test.cc b/tests/gtests/blenlib/BLI_path_util_test.cc
index 41fad661ea9..1cd1cbc345d 100644
--- a/tests/gtests/blenlib/BLI_path_util_test.cc
+++ b/tests/gtests/blenlib/BLI_path_util_test.cc
@@ -462,3 +462,25 @@ TEST(path_util, SplitDirfile)
 		EXPECT_STREQ("", file);
 	}
 }
+
+#define PATH_FRAME_STRIP(input_path, expect_path, expect_ext) \
+{ \
+	char path[FILE_MAX]; \
+	char ext[FILE_MAX]; \
+	BLI_strncpy(path, (input_path), FILE_MAX); \
+	BLI_path_frame_strip(path, ext); \
+	EXPECT_STREQ(path, expect_path); \
+	EXPECT_STREQ(ext, expect_ext); \
+}
+
+/* BLI_path_frame_strip */
+TEST(path_util, PathFrameStrip)
+{
+	PATH_FRAME_STRIP("", "", "");
+	PATH_FRAME_STRIP("nonum.abc", "nonum", ".abc");
+	PATH_FRAME_STRIP("fileonly.001.abc", "fileonly.###", ".abc");
+	PATH_FRAME_STRIP("/abspath/to/somefile.001.abc", "/abspath/to/somefile.###", ".abc");
+	PATH_FRAME_STRIP("/ext/longer/somefile.001.alembic", "/ext/longer/somefile.###", ".alembic");
+	PATH_FRAME_STRIP("/ext/shorter/somefile.123001.abc", "/ext/shorter/somefile.######", ".abc");
+}
+#undef PATH_FRAME_STRIP



More information about the Bf-blender-cvs mailing list