[Bf-blender-cvs] [56d8aff] master: BLI_path_suffix() - new path util functon to add a suffix to a filepath (before the extension)
Dalai Felinto
noreply at git.blender.org
Sat May 3 02:19:35 CEST 2014
Commit: 56d8affe422a0e20037ee94c7d0d12747bbbb81d
Author: Dalai Felinto
Date: Wed Apr 16 14:25:23 2014 -0300
https://developer.blender.org/rB56d8affe422a0e20037ee94c7d0d12747bbbb81d
BLI_path_suffix() - new path util functon to add a suffix to a filepath (before the extension)
Revision: D465
Reviewd by Campbell Barton
===================================================================
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 f207329..7741837 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -701,6 +701,48 @@ 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];
+ bool has_extension = false;
+
+ if (string_len + sep_len + suffix_len >= maxlen)
+ return false;
+
+ for (a = string_len - 1; a >= 0; a--) {
+ if (string[a] == '.') {
+ has_extension = true;
+ break;
+ }
+ else if (ELEM(string[a], '/', '\\')) {
+ break;
+ }
+ }
+
+ if (!has_extension)
+ a = string_len;
+
+ BLI_strncpy(extension, string + a, sizeof(extension));
+ 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