[Bf-blender-cvs] [b2ffdf9] bake-cycles: BLI_path_suffix() - new path util functon to add a suffix to a filepath (before the extension)

Dalai Felinto noreply at git.blender.org
Wed Apr 23 02:48:09 CEST 2014


Commit: b2ffdf9974be8acd2460d3d73dd12d881c5e94f8
Author: Dalai Felinto
Date:   Wed Apr 16 14:25:23 2014 -0300
https://developer.blender.org/rBb2ffdf9974be8acd2460d3d73dd12d881c5e94f8

BLI_path_suffix() - new path util functon to add a suffix to a filepath (before the extension)

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

M	source/blender/blenlib/BLI_path_util.h
M	source/blender/blenlib/intern/path_util.c

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

diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h
index 6e77faf..3e98e2c 100644
--- a/source/blender/blenlib/BLI_path_util.h
+++ b/source/blender/blenlib/BLI_path_util.h
@@ -172,6 +172,8 @@ void BLI_cleanup_unc_16(wchar_t *path_16);
 void BLI_cleanup_unc(char *path_16, int maxlen);
 #endif
 
+bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char *sep) ATTR_NONNULL();
+
 /* path string comparisons: case-insensitive for Windows, case-sensitive otherwise */
 #if defined(WIN32)
 #  define BLI_path_cmp BLI_strcasecmp
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 118cd50..938b95b 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -702,6 +702,44 @@ void BLI_path_rel(char *file, const char *relfile)
 }
 
 /**
+ * Appends a suffix to the string, fitting it before the extension
+ *
+ * string = Foo.png, suffix = 123, separator = _
+ * Foo.png -> Foo_123.png
+ *
+ * \param string  original (and final) string
+ * \param maxlen  Maximum length of string
+ * \param suffix  String to append to the original string
+ * \param sep Optional separator character
+ * \return  true if succeded
+ */
+bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char *sep)
+{
+	const size_t string_len = strlen(string);
+	const size_t suffix_len = strlen(suffix);
+	const size_t sep_len = strlen(sep);
+	ssize_t a;
+	char extension[FILE_MAX];
+
+	if (string_len + sep_len + suffix_len >= maxlen)
+		return false;
+
+	for (a = string_len - 1; a >= 0; a--) {
+		if (ELEM3(string[a], '.', '/', '\\')) {
+			break;
+		}
+	}
+
+	if ((a < 0) || (string[a] != '.')) {
+		a = string_len;
+	}
+
+	strcpy(extension, string + a);
+	sprintf(string + a, "%s%s%s", sep, suffix, extension);
+	return true;
+}
+
+/**
  * Replaces path with the path of its parent directory, returning true if
  * it was able to find a parent directory within the pathname.
  */




More information about the Bf-blender-cvs mailing list