add api function BLI_path_append to add to a path (and ensure a seperator), replaces BLI_join_dirfile when the dir and the destimation were the same.

This commit is contained in:
Campbell Barton
2013-07-24 21:25:06 +00:00
parent 9c0e331f81
commit ec3fce8e27
5 changed files with 49 additions and 21 deletions

View File

@@ -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 {

View File

@@ -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);

View File

@@ -1690,6 +1690,26 @@ void BLI_split_file_part(const char *string, char *file, const size_t filelen)
BLI_split_dirfile(string, NULL, file, 0, 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;

View File

@@ -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;
}

View File

@@ -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);