diff options
Diffstat (limited to 'commands.cpp')
-rw-r--r-- | commands.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
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 <boost/cast.hpp> +#include <boost/format.hpp> +#include <boost/algorithm/string/join.hpp> #include <iostream> @@ -28,10 +30,46 @@ static std::vector<std::string> ls(const std::vector<std::string>& args) { return result; } +typedef boost::function<std::vector<MusicListing::p> (const std::string artist)> FindFunction; +std::map<std::string, FindFunction> find_handlers; + +static std::vector<std::string> find(const std::vector<std::string>& 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<std::string> types; + for(std::map<std::string, FindFunction>::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<MusicListing::p> ml = ff(args[2]); + if(!ml.size()) { + throw commands::CommandException("no results"); + } + + std::vector<std::string> result; + for(std::vector<MusicListing::p>::iterator it = ml.begin(); it != ml.end(); it++) { + result.push_back((*it)->path.string()); + } + + + return result; +} + std::map<std::string, commands::Handler> commands::handlers; void commands::init() { handlers["ls"] = ls; + handlers["find"] = find; + find_handlers["artist"] = music::find_artist; } std::vector<std::string> commands::execute(const std::vector<std::string>& args) { |