Skip to content

Commit

Permalink
SdCard.cpp -> simplify 'SdCard_ParseM3UPlaylist' and fix parsing (#351)
Browse files Browse the repository at this point in the history
* SdCard.cpp -> 'SdCard_ParseM3UPlaylist' has no longer optional parameter

* 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.
  • Loading branch information
h4kun4m4t4t4 authored Nov 17, 2024
1 parent 36822d0 commit f3afa51
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/SdCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,43 +266,28 @@ static bool SdCard_allocAndSave(Playlist *playlist, const String &s) {
return true;
};

static std::optional<Playlist *> SdCard_ParseM3UPlaylist(File f, bool forceExtended = false) {
const String line = f.readStringUntil('\n');
const bool extended = line.startsWith("#EXTM3U") || forceExtended;
static std::optional<Playlist *> 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;
}
Expand Down

0 comments on commit f3afa51

Please sign in to comment.