BLI_path: add BLI_path_append_dir (appends and ensures trailing slash)

Avoid copying the string then calling BLI_path_slash_ensure afterwards.
This commit is contained in:
Campbell Barton
2022-10-30 15:44:17 +11:00
parent d66f24cfe3
commit 5392f220f0
3 changed files with 27 additions and 9 deletions

View File

@@ -68,8 +68,15 @@ const char *BLI_path_extension(const char *filepath) ATTR_NONNULL();
/**
* Append a filename to a dir, ensuring slash separates.
* \return The new length of `dst`.
*/
void BLI_path_append(char *__restrict dst, size_t maxlen, const char *__restrict file)
size_t BLI_path_append(char *__restrict dst, size_t maxlen, const char *__restrict file)
ATTR_NONNULL();
/**
* A version of #BLI_path_append that ensures a trailing slash if there is space in `dst`.
* \return The new length of `dst`.
*/
size_t BLI_path_append_dir(char *__restrict dst, size_t maxlen, const char *__restrict dir)
ATTR_NONNULL();
/**

View File

@@ -1431,21 +1431,34 @@ const char *BLI_path_extension(const char *filepath)
return extension;
}
void BLI_path_append(char *__restrict dst, const size_t maxlen, const char *__restrict file)
size_t BLI_path_append(char *__restrict dst, const size_t maxlen, const char *__restrict file)
{
size_t dirlen = BLI_strnlen(dst, maxlen);
/* inline BLI_path_slash_ensure */
/* Inline #BLI_path_slash_ensure. */
if ((dirlen > 0) && (dst[dirlen - 1] != SEP)) {
dst[dirlen++] = SEP;
dst[dirlen] = '\0';
}
if (dirlen >= maxlen) {
return; /* fills the path */
return dirlen; /* fills the path */
}
BLI_strncpy(dst + dirlen, file, maxlen - dirlen);
return dirlen + BLI_strncpy_rlen(dst + dirlen, file, maxlen - dirlen);
}
size_t BLI_path_append_dir(char *__restrict dst, const size_t maxlen, const char *__restrict dir)
{
size_t dirlen = BLI_path_append(dst, maxlen, dir);
if (dirlen + 1 < maxlen) {
/* Inline #BLI_path_slash_ensure. */
if ((dirlen > 0) && (dst[dirlen - 1] != SEP)) {
dst[dirlen++] = SEP;
dst[dirlen] = '\0';
}
}
return dirlen;
}
size_t BLI_path_join_array(char *__restrict dst,

View File

@@ -202,8 +202,7 @@ static FileSelect file_select_do(bContext *C, int selected_idx, bool do_diropen)
}
else {
BLI_path_normalize_dir(BKE_main_blendfile_path(bmain), params->dir, sizeof(params->dir));
strcat(params->dir, file->relpath);
BLI_path_slash_ensure(params->dir, sizeof(params->dir));
BLI_path_append_dir(params->dir, sizeof(params->dir), file->relpath);
}
ED_file_change_dir(C);
@@ -1797,8 +1796,7 @@ static bool file_execute(bContext *C, SpaceFile *sfile)
}
else {
BLI_path_normalize(BKE_main_blendfile_path(bmain), params->dir);
BLI_path_append(params->dir, sizeof(params->dir), file->relpath);
BLI_path_slash_ensure(params->dir, sizeof(params->dir));
BLI_path_append_dir(params->dir, sizeof(params->dir), file->relpath);
}
ED_file_change_dir(C);
}