From 65f6a2753e34ad2cff72c0ddb7aa753baeead454 Mon Sep 17 00:00:00 2001 From: h4kun4m4t4t4 Date: Sat, 16 Nov 2024 21:36:25 +0100 Subject: [PATCH] SdCard.cpp -> 'SdCard_ParseM3UPlaylist': use same parsing routine for 'normal' and 'extended' m3u filelist. And thereby fixing an issue parsing 'normal' m3u files containing 'carriage return and linefeed' at the same time. --- src/SdCard.cpp | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/SdCard.cpp b/src/SdCard.cpp index 724773e4..3bb9e938 100644 --- a/src/SdCard.cpp +++ b/src/SdCard.cpp @@ -266,43 +266,28 @@ static bool SdCard_allocAndSave(Playlist *playlist, const String &s) { return true; }; -static std::optional SdCard_ParseM3UPlaylist(File f) { - const String line = f.readStringUntil('\n'); - const bool extended = line.startsWith("#EXTM3U"); +static std::optional SdCard_ParseM3UPlaylist(File file) { Playlist *playlist = new Playlist(); // reserve a sane amount of memory to reduce heap fragmentation playlist->reserve(64); - if (extended) { - // extended m3u file format - // ignore all lines starting with '#' - - while (f.available()) { - String line = f.readStringUntil('\n'); - if (!line.startsWith("#")) { - // this something we have to save - line.trim(); - // save string - if (!SdCard_allocAndSave(playlist, line)) { - return std::nullopt; - } + // normal m3u is just a bunch of filenames, 1 / line + // extended m3u file format can also include comments or special directives, prefaced by the "#" character + // -> ignore all lines starting with '#' + + while (file.available()) { + String line = file.readStringUntil('\n'); + if (!line.startsWith("#")) { + // this something we have to save + line.trim(); + // save string + if (!SdCard_allocAndSave(playlist, line)) { + return std::nullopt; } } - // resize std::vector memory to fit our count - playlist->shrink_to_fit(); - return playlist; } - // normal m3u is just a bunch of filenames, 1 / line - f.seek(0); - while (f.available()) { - String line = f.readStringUntil('\n'); - // save string - if (!SdCard_allocAndSave(playlist, line)) { - return std::nullopt; - } - } - // resize memory to fit our count + // resize std::vector memory to fit our count playlist->shrink_to_fit(); return playlist; }