diff options
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 13 | ||||
-rw-r--r-- | source/blender/blenlib/intern/fileops.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 47 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_ops.c | 4 | ||||
-rw-r--r-- | source/blender/imbuf/intern/indexer.c | 4 |
5 files changed, 49 insertions, 21 deletions
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index e0a34e35acc..cb812fe8595 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -89,7 +89,18 @@ void BLI_make_existing_file(const char *name); void BLI_split_dirfile(const char *string, char *dir, char *file, const size_t dirlen, const size_t filelen); void BLI_split_dir_part(const char *string, char *dir, const size_t dirlen); void BLI_split_file_part(const char *string, char *file, const size_t filelen); -void BLI_join_dirfile(char *string, const size_t maxlen, const char *dir, const char *file); +void BLI_path_append(char *__restrict dst, const size_t maxlen, + const char *__restrict file) +#ifdef __GNUC__ +__attribute__((nonnull)) +#endif +; +void BLI_join_dirfile(char *__restrict string, const size_t maxlen, + const char *__restrict dir, const char *__restrict file) +#ifdef __GNUC__ +__attribute__((nonnull)) +#endif +; const char *BLI_path_basename(const char *path); typedef enum bli_rebase_state { diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index 26b9e08c7f6..af0359c0087 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -445,7 +445,7 @@ static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, c size_t len = strlen(dir) + strlen(file) + 1; if (*dst == NULL) - *dst = MEM_callocN(len + 1, "join_dirfile_alloc path"); + *dst = MEM_mallocN(len + 1, "join_dirfile_alloc path"); else if (*alloc_len < len) *dst = MEM_reallocN(*dst, len + 1); diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 762c4845943..2c06a812c8a 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1691,6 +1691,26 @@ void BLI_split_file_part(const char *string, char *file, const size_t filelen) } /** + * Append a filename to a dir, ensuring slash separates. + */ +void BLI_path_append(char *dst, const size_t maxlen, const char *file) +{ + size_t dirlen = BLI_strnlen(dst, maxlen); + + /* inline BLI_add_slash */ + if ((dirlen > 0) && (dst[dirlen - 1] != SEP)) { + dst[dirlen++] = SEP; + dst[dirlen] = '\0'; + } + + if (dirlen >= maxlen) { + return; /* fills the path */ + } + + BLI_strncpy(dst + dirlen, file, maxlen - dirlen); +} + +/** * Simple appending of filename to dir, does not check for valid path! * Puts result into *dst, which may be same area as *dir. */ @@ -1698,15 +1718,16 @@ void BLI_join_dirfile(char *dst, const size_t maxlen, const char *dir, const cha { size_t dirlen = BLI_strnlen(dir, maxlen); - if (dst != dir) { - if (dirlen == maxlen) { - memcpy(dst, dir, dirlen); - dst[dirlen - 1] = '\0'; - return; /* dir fills the path */ - } - else { - memcpy(dst, dir, dirlen + 1); - } + /* args can't match */ + BLI_assert(!ELEM(dst, dir, file)); + + if (dirlen == maxlen) { + memcpy(dst, dir, dirlen); + dst[dirlen - 1] = '\0'; + return; /* dir fills the path */ + } + else { + memcpy(dst, dir, dirlen + 1); } if (dirlen + 1 >= maxlen) { @@ -1723,10 +1744,6 @@ void BLI_join_dirfile(char *dst, const size_t maxlen, const char *dir, const cha return; /* fills the path */ } - if (file == NULL) { - return; - } - BLI_strncpy(dst + dirlen, file, maxlen - dirlen); } @@ -1842,7 +1859,7 @@ int BLI_rebase_path(char *abs, size_t abs_len, /* subdirectories relative to blend_dir */ BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, rel_dir); /* same subdirectories relative to dest_dir */ - BLI_join_dirfile(dest_path, sizeof(dest_path), dest_path, base); + BLI_path_append(dest_path, sizeof(dest_path), base); /* keeping original item basename */ } @@ -2063,7 +2080,7 @@ static void bli_where_am_i(char *fullname, const size_t maxlen, const char *name else { strncpy(filename, path, sizeof(filename)); } - BLI_join_dirfile(fullname, maxlen, fullname, name); + BLI_path_append(fullname, maxlen, name); if (add_win32_extension(filename)) { BLI_strncpy(fullname, filename, maxlen); break; diff --git a/source/blender/editors/interface/interface_ops.c b/source/blender/editors/interface/interface_ops.c index 9f7d1435195..ec6e9bbbfb3 100644 --- a/source/blender/editors/interface/interface_ops.c +++ b/source/blender/editors/interface/interface_ops.c @@ -917,7 +917,7 @@ static void edittranslation_find_po_file(const char *root, const char *uilng, ch /* First, full lang code. */ BLI_snprintf(tstr, sizeof(tstr), "%s.po", uilng); BLI_join_dirfile(path, maxlen, root, uilng); - BLI_join_dirfile(path, maxlen, path, tstr); + BLI_path_append(path, maxlen, tstr); if (BLI_is_file(path)) return; @@ -941,7 +941,7 @@ static void edittranslation_find_po_file(const char *root, const char *uilng, ch BLI_join_dirfile(path, maxlen, root, tstr); strcat(tstr, ".po"); - BLI_join_dirfile(path, maxlen, path, tstr); + BLI_path_append(path, maxlen, tstr); if (BLI_is_file(path)) return; } diff --git a/source/blender/imbuf/intern/indexer.c b/source/blender/imbuf/intern/indexer.c index c66f2d62db2..7acfe8a9dd1 100644 --- a/source/blender/imbuf/intern/indexer.c +++ b/source/blender/imbuf/intern/indexer.c @@ -366,8 +366,8 @@ static void get_index_dir(struct anim *anim, char *index_dir, size_t index_dir_l if (!anim->index_dir[0]) { char fname[FILE_MAXFILE]; BLI_split_dirfile(anim->name, index_dir, fname, index_dir_len, sizeof(fname)); - BLI_join_dirfile(index_dir, index_dir_len, index_dir, "BL_proxy"); - BLI_join_dirfile(index_dir, index_dir_len, index_dir, fname); + BLI_path_append(index_dir, index_dir_len, "BL_proxy"); + BLI_path_append(index_dir, index_dir_len, fname); } else { BLI_strncpy(index_dir, anim->index_dir, index_dir_len); |