esmp3/src/playlist_manager.cpp

61 lines
1.5 KiB
C++

#include "playlist_manager.h"
#include <SD.h>
#include "spi_master.h"
PlaylistManager::PlaylistManager() {
SPIMaster::select_sd();
File root = SD.open("/");
File entry;
while (entry = root.openNextFile()) {
String foldername = entry.name();
// Remove trailing slash
foldername.remove(foldername.length());
TRACE("Looking at %s...", foldername.c_str());
if (!entry.isDirectory() || foldername.startsWith("/.")) continue;
if (!SD.exists(foldername + "/ids.txt")) {
TRACE("No ids.txt -> ignoring\n");
continue;
}
File f = SD.open(foldername + "/ids.txt");
String buffer = "";
if (f.available()) {
do {
char c = f.read();
if (!f.available() && c!='\n' && c!='\r') {
buffer.concat(c);
c='\n';
}
if (c=='\n' || c=='\r') {
if (buffer.length() > 0) {
_map[buffer] = foldername;
TRACE(" ID %s", buffer.c_str());
buffer="";
}
} else {
buffer.concat(c);
}
} while(f.available());
}
f.close();
entry.close();
}
root.close();
SPIMaster::select_sd(false);
}
Playlist* PlaylistManager::get_playlist_for_id(String id) {
if (!_map.count(id)) return NULL;
String folder = _map[id];
if (!_playlists.count(folder)) {
_playlists[folder] = new Playlist(folder);
}
return _playlists[folder];
}
void PlaylistManager::dump_ids() {
for (std::map<String, String>::iterator it = _map.begin(); it!=_map.end(); it++) {
INFO(" %s -> %s\n", it->first.c_str(), it->second.c_str());
}
}