From 35520ef17720778f7140c085c6c2bd005ed802f8 Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 23 Dec 2024 16:21:22 +1100 Subject: [PATCH] Make media directories case insensitive --- src/tangara/database/database.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/tangara/database/database.cpp b/src/tangara/database/database.cpp index ce074ef9..9851b041 100644 --- a/src/tangara/database/database.cpp +++ b/src/tangara/database/database.cpp @@ -532,28 +532,34 @@ static constexpr char kAudiobookMediaPath[] = "/Audiobooks/"; auto Database::calculateMediaType(TrackTags& tags, std::string_view path) -> MediaType { + + auto equalsIgnoreCase = [&](char lhs, char rhs) { + return std::tolower(lhs) == std::tolower(rhs); + }; + // Use the filepath first, since it's the most explicit way for the user to // tell us what this track is. - if (path.starts_with(kMusicMediaPath)) { + auto checkPathPrefix = [&](std::string_view path, std::string prefix) { + auto res = std::mismatch(prefix.begin(), prefix.end(), path.begin(), path.end(), equalsIgnoreCase); + return res.first == prefix.end(); + }; + + if (checkPathPrefix(path, kMusicMediaPath)) { return MediaType::kMusic; } - if (path.starts_with(kPodcastMediaPath)) { + if (checkPathPrefix(path, kPodcastMediaPath)) { return MediaType::kPodcast; } - if (path.starts_with(kAudiobookMediaPath)) { + if (checkPathPrefix(path, kAudiobookMediaPath)) { return MediaType::kAudiobook; } // Podcasts may (rarely!) have a genre tag that tells us what they are. Look // for it. - auto equalsIgnoreCase = [&](char lhs, char rhs) { - // NB: not really safe across languages, but genre tags tend to be in - // English anyway. - return std::tolower(lhs) == std::tolower(rhs); - }; - auto genres = tags.genres(); for (const auto& genre : genres) { + // NB: not really safe across languages, but genre tags tend to be in + // English anyway. if (std::ranges::equal(genre, "podcast", equalsIgnoreCase)) { return MediaType::kPodcast; }