From af8eb4386b8c2d898366d3892c343105ae9ea4e0 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 1 Jan 2011 21:46:56 +0100 Subject: Added 'find' command to the telnet server and removed find_artist call in music::init(). --- commands.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'commands.cpp') diff --git a/commands.cpp b/commands.cpp index dfeeebb..2401690 100644 --- a/commands.cpp +++ b/commands.cpp @@ -2,6 +2,8 @@ #include "music.h" #include +#include +#include #include @@ -28,10 +30,46 @@ static std::vector ls(const std::vector& args) { 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) { -- cgit v1.2.3