diff options
Diffstat (limited to 'commands.cpp')
-rw-r--r-- | commands.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/commands.cpp b/commands.cpp new file mode 100644 index 0000000..dfeeebb --- /dev/null +++ b/commands.cpp @@ -0,0 +1,45 @@ +#include "commands.h" +#include "music.h" + +#include <boost/cast.hpp> + +#include <iostream> + +static std::vector<std::string> ls(const std::vector<std::string>& 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<std::string> result; + MusicDirectory *dir = boost::polymorphic_downcast<MusicDirectory*>(&(*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<std::string, commands::Handler> commands::handlers; + +void commands::init() { + handlers["ls"] = ls; +} + +std::vector<std::string> commands::execute(const std::vector<std::string>& args) { + assert(args.size()); + Handler h = commands::handlers[args[0]]; + if(!h) { + throw CommandException("unknown command"); + } + + return h(args); +} |