summaryrefslogtreecommitdiff
path: root/commands.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-01-01 21:04:17 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2011-01-01 21:04:17 +0100
commit14500d43760661ffc3ffb67d929088c27fe46c64 (patch)
tree75d9471a61b00eb56b7ee75b3a785a392edb0dad /commands.cpp
parent0e7f2cef26bde782a5758b5e9a3dfe20f745df8f (diff)
Implemented a simple 'ls' command for the telnet server.
Diffstat (limited to 'commands.cpp')
-rw-r--r--commands.cpp45
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);
+}