From 14500d43760661ffc3ffb67d929088c27fe46c64 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 1 Jan 2011 21:04:17 +0100 Subject: Implemented a simple 'ls' command for the telnet server. --- commands.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 commands.cpp (limited to 'commands.cpp') 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 + +#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); +} -- cgit v1.2.3