Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorDalai Felinto <dfelinto@gmail.com>2014-04-16 21:25:23 +0400
committerDalai Felinto <dfelinto@gmail.com>2014-05-03 03:56:16 +0400
commit56d8affe422a0e20037ee94c7d0d12747bbbb81d (patch)
tree99dc2d8898d00dffce512eeb767fb2e9b698f596 /source
parent0111f3505f0263a35a4196535db26365651d30e7 (diff)
BLI_path_suffix() - new path util functon to add a suffix to a filepath (before the extension)
Revision: D465 Reviewd by Campbell Barton
Diffstat (limited to 'source')
-rw-r--r--source/blender/blenlib/BLI_path_util.h2
-rw-r--r--source/blender/blenlib/intern/path_util.c42
2 files changed, 44 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h
index 6e77faf1204..3e98e2ceeae 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 f207329e33d..77418377f11 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.
*/