From 76a9a524c08ffe6b76321b556b4872cf1b75d76d Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 3 Apr 2024 23:10:07 -0400 Subject: skin_tokens.c get_dir() improve path detection get_dir grabs a component of a path it now handles multiple slashes, no leading slashes Change-Id: I6c4a377796652808a02b44f6da0a63b201dd8f46 --- apps/gui/skin_engine/skin_tokens.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/gui/skin_engine/skin_tokens.c b/apps/gui/skin_engine/skin_tokens.c index f6c166b140..12277aa0ea 100644 --- a/apps/gui/skin_engine/skin_tokens.c +++ b/apps/gui/skin_engine/skin_tokens.c @@ -87,6 +87,7 @@ static const char* get_codectype(const struct mp3entry* id3) * level - what to extract. 0 is file name, 1 is parent of file, 2 is * parent of parent, etc. * + * path does not need to be absolute, ignores multiple slashes * Returns buf if the desired level was found, NULL otherwise. */ char* get_dir(char* buf, int buf_size, const char* path, int level) @@ -94,7 +95,7 @@ char* get_dir(char* buf, int buf_size, const char* path, int level) const char* sep; const char* last_sep; int len; - + buf[0] = '\0'; sep = path + strlen(path); last_sep = sep; @@ -105,15 +106,19 @@ char* get_dir(char* buf, int buf_size, const char* path, int level) if (!level) break; - level--; last_sep = sep - 1; + if (*last_sep != '/') /* ignore multiple separators */ + level--; } } - if (level || (last_sep <= sep)) + if (level || (last_sep <= sep)) /* level was not found */ return NULL; - len = MIN(last_sep - sep, buf_size - 1); + if (sep == path && *sep != '/') /* for paths without leading separator */ + sep = path - 1; + + len = MIN((last_sep - sep), buf_size - 1); strmemccpy(buf, sep + 1, len + 1); return buf; } -- cgit v1.2.3