#include "music.h" #include #include namespace music { MusicDirectory *root_directory = NULL; void init(const fs::path root) { root_directory = new MusicDirectory(root); } MusicListing *find(std::string path, MusicDirectory& root) { // remove trailing slash if(boost::algorithm::ends_with(path, "/")) path = path.substr(0, path.size()-1); fs::path full_path = root_directory->path / path; if(root.path == full_path) return &root; for(MusicDirectories::iterator it = root.directories.begin(); it != root.directories.end(); it++) { MusicListing *ml = find(path, *it); if(ml) return ml; } for(MusicTracks::iterator it = root.tracks.begin(); it != root.tracks.end(); it++) { std::string rel_path = it->path.string().substr(root_directory->path.string().size()); std::cout << rel_path << std::endl; if(rel_path == path) return &(*it); } return NULL; } MusicListing *find(std::string path) { return find(path, *root_directory); } }; // namespace music void MusicDirectory::render(std::ostream& os) { for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) { std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); os << boost::format("%s
") % rel_path % rel_path; } for(MusicTracks::iterator it = tracks.begin(); it != tracks.end(); it++) { std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); os << boost::format("%s
") % rel_path % rel_path; } } MusicTrack::MusicTrack(const fs::path path) { std::cout << path << std::endl; this->path = path; } MusicDirectory::MusicDirectory(const fs::path root) { this->path = root; std::cout << root << std::endl; fs::path p(root); fs::directory_iterator end_itr; for(fs::directory_iterator it(p); it != end_itr; it++) { if(fs::is_directory(it->status())) { directories.push_back(MusicDirectory(it->path().string())); } else if(fs::is_regular_file(it->status())) { tracks.push_back(MusicTrack(it->path().string())); } } } void MusicTrack::render(std::ostream& os) { }