summaryrefslogtreecommitdiff
path: root/commands.cpp
blob: 76ce2ffd5a702d3fbaae39316815a95534a8d845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "commands.h"

#include <boost/format.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <iostream>

namespace qi = boost::spirit::qi;

std::vector<std::string> Commands::ls() {
	if(args.size() != 2) {
		throw CommandException("usage: ls DIR");
	}

	MusicDirectory::p dir = music::get_directory(args[1]);
	if(!dir) {
		throw CommandException("no such directory");
	}
	std::cout << dir->path << std::endl;
	std::vector<std::string> result;
	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;
}

typedef std::map<std::string, std::string> stringmap;

static bool find_parse(std::string str, stringmap& foo) {
	typedef std::string::const_iterator Iterator;

	Iterator begin = str.begin();
	Iterator end = str.end();

	return qi::parse(begin, end,
		// pairs wrapped in "quotes"
		(('"' >> +(qi::char_ - ':') >> ':' >> +(qi::char_ - '"') >> '"') |
		// simple key:value pairs
		(+(qi::char_ - ':' - ' ') >> ':' >> +(qi::char_ - ' ')))
		// skip spaces between pairs
		% +qi::char_(' '),
		foo) && begin == end;
}

std::vector<std::string> Commands::find() {
	if(args.size() < 2) {
		throw CommandException("usage: find SEARCH");
	}

	stringmap find_args;
	args.erase(args.begin());
	std::string search = boost::algorithm::join(args, " ");
	std::vector<MusicListing::p> ml;
	if(find_parse(search, find_args)) {
		ml = music::find(find_args);
	} else {
		ml = music::find(search);
	}

	if(!ml.size()) {
		throw 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::vector<std::string> Commands::update() {
	if(args.size() != 2) {
		throw CommandException("usage: update DIR");
	}

	io_service.post(boost::bind(music::begin_update, args[1]));

	std::vector<std::string> v;
	return v;
}

Commands::Commands(boost::asio::io_service& io_service_, std::vector<std::string>& args_) : io_service(io_service_), args(args_) {
	handlers["ls"] = &Commands::ls;
	handlers["find"] = &Commands::find;
	handlers["update"] = &Commands::update;
}

/** Invokes the command and returns its results.
 *  Throws CommandException if the command isn't found.
 */
std::vector<std::string> Commands::operator()() {
	assert(args.size());
	Handler h = handlers[args[0]];
	if(!h) {
		throw CommandException(boost::str(boost::format("unknown command \"%s\"") % args[0]));
	}

	return h(this);
}