pathfuncs.c add path_strip_leading_separators()

added to path_append as well

Change-Id: Ieb6ec4f4c475ca5e60c8246c7f044bcc7651f6bf
This commit is contained in:
William Wilgus 2024-03-29 16:09:56 -04:00 committed by William Wilgus
parent 777098fca9
commit fe00906abb
3 changed files with 24 additions and 8 deletions

View file

@ -70,7 +70,7 @@ static int browser_status = CATBROWSE_NOTHING;
static size_t get_directory(char* dirbuf, size_t dirbuf_sz) static size_t get_directory(char* dirbuf, size_t dirbuf_sz)
{ {
char *pl_dir = PLAYLIST_CATALOG_DEFAULT_DIR; const char *pl_dir = PLAYLIST_CATALOG_DEFAULT_DIR;
/* directory config is of the format: "dir: /path/to/dir" */ /* directory config is of the format: "dir: /path/to/dir" */
if (global_settings.playlist_catalog_dir[0] != '\0') if (global_settings.playlist_catalog_dir[0] != '\0')
@ -79,10 +79,7 @@ static size_t get_directory(char* dirbuf, size_t dirbuf_sz)
} }
/* remove duplicate leading '/' */ /* remove duplicate leading '/' */
if (pl_dir[0] == '/' && pl_dir[1] == '/') path_strip_leading_separators(pl_dir, &pl_dir);
{
pl_dir++;
}
return strlcpy(dirbuf, pl_dir, dirbuf_sz); return strlcpy(dirbuf, pl_dir, dirbuf_sz);
} }

View file

@ -339,6 +339,23 @@ size_t path_dirname(const char *name, const char **nameptr)
return q - name; return q - name;
} }
/* Removes leading separators from a path
* "" *nameptr->NUL, count=0: ""
* "/" *nameptr->/, count=1: "/"
* "//" *nameptr->2nd /, count=2: "/"
* "a/" *nameptr->a/, count=0: "a/"
* "//b//" *nameptr->2nd /, count=2: "/b//"
* "/c/" *nameptr->/, count=1: "/c/"
*/
size_t path_strip_leading_separators(const char *name, const char **nameptr)
{
const char *p = name;
*nameptr = p;
while (*(p) == PATH_SEPCH && *(++p) == PATH_SEPCH)
*nameptr = p;
return p - name;
}
/* Removes trailing separators from a path /* Removes trailing separators from a path
* "" *nameptr->NUL, len=0: "" * "" *nameptr->NUL, len=0: ""
* "/" *nameptr->/, len=1: "/" * "/" *nameptr->/, len=1: "/"
@ -462,7 +479,7 @@ void path_remove_dot_segments (char *dstpath, const char *path)
size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max,
const char *component, size_t bufsize) const char *component, size_t bufsize)
{ {
size_t len; size_t len = 0;
bool separate = false; bool separate = false;
const char *base = basepath && basepath[0] ? basepath : buf; const char *base = basepath && basepath[0] ? basepath : buf;
if (!base) if (!base)
@ -471,7 +488,7 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max,
if (!buf) if (!buf)
bufsize = 0; bufsize = 0;
if (path_is_absolute(component)) if (path_is_absolute(component)) /* starts with a '/' path separator */
{ {
/* 'component' is absolute; replace all */ /* 'component' is absolute; replace all */
basepath = component; basepath = component;
@ -484,8 +501,9 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max,
if (base == buf) if (base == buf)
len = strlen(buf); len = strlen(buf);
else else if (basepath)
{ {
path_strip_leading_separators(basepath, &basepath);
len = strlcpy(buf, basepath, bufsize); len = strlcpy(buf, basepath, bufsize);
if (basepath_max < len) if (basepath_max < len)
{ {

View file

@ -87,6 +87,7 @@ int make_volume_root(int volume, char *dst);
int path_strip_drive(const char *name, const char **nameptr, bool greedy); int path_strip_drive(const char *name, const char **nameptr, bool greedy);
size_t path_basename(const char *name, const char **nameptr); size_t path_basename(const char *name, const char **nameptr);
size_t path_dirname(const char *name, const char **nameptr); size_t path_dirname(const char *name, const char **nameptr);
size_t path_strip_leading_separators(const char *name, const char **nameptr);
size_t path_strip_trailing_separators(const char *name, const char **nameptr); size_t path_strip_trailing_separators(const char *name, const char **nameptr);
void path_correct_separators(char *dstpath, const char *path); void path_correct_separators(char *dstpath, const char *path);
void path_remove_dot_segments(char *dstpath, const char *path); void path_remove_dot_segments(char *dstpath, const char *path);