diff options
author | William Wilgus <wilgus.william@gmail.com> | 2024-03-29 16:09:56 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2024-03-30 11:01:53 -0400 |
commit | fe00906abb355a40888e7a2ce785ddfe3cfc525f (patch) | |
tree | 78ebfcc04d1a540636d3574eb3f27925fa88a4bd | |
parent | 777098fca9765900202d8d9c1698c57f136c7338 (diff) | |
download | rockbox-fe00906abb.tar.gz rockbox-fe00906abb.zip |
pathfuncs.c add path_strip_leading_separators()
added to path_append as well
Change-Id: Ieb6ec4f4c475ca5e60c8246c7f044bcc7651f6bf
-rw-r--r-- | apps/playlist_catalog.c | 7 | ||||
-rw-r--r-- | firmware/common/pathfuncs.c | 24 | ||||
-rw-r--r-- | firmware/export/pathfuncs.h | 1 |
3 files changed, 24 insertions, 8 deletions
diff --git a/apps/playlist_catalog.c b/apps/playlist_catalog.c index 1bd4b7ee13..bcca406494 100644 --- a/apps/playlist_catalog.c +++ b/apps/playlist_catalog.c @@ -70,7 +70,7 @@ static int browser_status = CATBROWSE_NOTHING; 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" */ 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 '/' */ - if (pl_dir[0] == '/' && pl_dir[1] == '/') - { - pl_dir++; - } + path_strip_leading_separators(pl_dir, &pl_dir); return strlcpy(dirbuf, pl_dir, dirbuf_sz); } diff --git a/firmware/common/pathfuncs.c b/firmware/common/pathfuncs.c index fa296cc2ed..e9218ea2c7 100644 --- a/firmware/common/pathfuncs.c +++ b/firmware/common/pathfuncs.c @@ -339,6 +339,23 @@ size_t path_dirname(const char *name, const char **nameptr) 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 * "" *nameptr->NUL, len=0: "" * "/" *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, const char *component, size_t bufsize) { - size_t len; + size_t len = 0; bool separate = false; const char *base = basepath && basepath[0] ? basepath : buf; if (!base) @@ -471,7 +488,7 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, if (!buf) bufsize = 0; - if (path_is_absolute(component)) + if (path_is_absolute(component)) /* starts with a '/' path separator */ { /* 'component' is absolute; replace all */ basepath = component; @@ -484,8 +501,9 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, if (base == buf) len = strlen(buf); - else + else if (basepath) { + path_strip_leading_separators(basepath, &basepath); len = strlcpy(buf, basepath, bufsize); if (basepath_max < len) { diff --git a/firmware/export/pathfuncs.h b/firmware/export/pathfuncs.h index 1b18f22d06..03495e5ab2 100644 --- a/firmware/export/pathfuncs.h +++ b/firmware/export/pathfuncs.h @@ -87,6 +87,7 @@ int make_volume_root(int volume, char *dst); 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_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); void path_correct_separators(char *dstpath, const char *path); void path_remove_dot_segments(char *dstpath, const char *path); |