#include "commands.h" #include "music.h" #include #include #include static std::vector ls(const std::vector& args) { if(args.size() != 2) { throw commands::CommandException("usage: ls DIR"); } MusicDirectory::p dir = music::get_directory(args[1]); if(!dir) { throw commands::CommandException("no such directory"); } std::cout << dir->path << std::endl; std::vector result; 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; } typedef boost::function (const std::string artist)> FindFunction; std::map find_handlers; static std::vector find(const std::vector& args) { if(args.size() != 3) { throw commands::CommandException("usage: find TYPE SEARCH"); } FindFunction ff; if(find_handlers.find(args[1]) != find_handlers.end()) { ff = find_handlers[args[1]]; } else { std::vector types; for(std::map::iterator it = find_handlers.begin(); it != find_handlers.end(); it++) { types.push_back(it->first); } std::string s = boost::str(boost::format("unknown search type, must be one of %s") % boost::algorithm::join(types, ", ")); throw commands::CommandException(s.c_str()); } std::vector ml = ff(args[2]); if(!ml.size()) { throw commands::CommandException("no results"); } std::vector result; for(std::vector::iterator it = ml.begin(); it != ml.end(); it++) { result.push_back((*it)->path.string()); } return result; } std::map commands::handlers; void commands::init() { handlers["ls"] = ls; handlers["find"] = find; find_handlers["artist"] = music::find_artist; } 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); }