#include "commands.h" #include "music.h" #include #include static std::vector ls(const std::vector& args) { if(args.size() != 2) { throw commands::CommandException("usage: ls DIR"); } MusicListing::p ml = music::get(args[1]); if(!ml || !fs::is_directory(ml->path)) { throw commands::CommandException("no such directory"); } std::vector result; MusicDirectory *dir = boost::polymorphic_downcast(&(*ml)); for(MusicDirectory::PathListings::iterator it = dir->directories.begin(); it != dir->directories.end(); it++) { std::string rel_path = it->string().substr(music::root_directory.string().size()); result.push_back(rel_path); } for(MusicDirectory::PathListings::iterator it = dir->tracks.begin(); it != dir->tracks.end(); it++) { std::string rel_path = it->string().substr(music::root_directory.string().size()); result.push_back(rel_path); } return result; } std::map commands::handlers; void commands::init() { handlers["ls"] = ls; } std::vector commands::execute(const std::vector& args) { assert(args.size()); Handler h = commands::handlers[args[0]]; if(!h) { throw CommandException("unknown command"); } return h(args); }