* Put both global and download specific commands in a single

CommandMap.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@914 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-06-17 03:14:38 +00:00
parent 297b4b5f1d
commit e9db89e6d6
36 changed files with 330 additions and 345 deletions
+3 -3
View File
@@ -34,12 +34,12 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_VARIABLE_H
#define RTORRENT_UTILS_VARIABLE_H
#ifndef RTORRENT_RPC_VARIABLE_H
#define RTORRENT_RPC_VARIABLE_H
#include <torrent/object.h>
namespace utils {
namespace rpc {
class Command {
public:
+3 -3
View File
@@ -37,11 +37,11 @@
#include "config.h"
#include "core/download.h"
#include "rpc/parse.h"
#include "parse.h"
#include "command_download_slot.h"
namespace utils {
namespace rpc {
const torrent::Object
CommandDownloadSlot::call_unknown(Command* rawCommand, core::Download* download, const torrent::Object& rawArgs) {
@@ -86,7 +86,7 @@ CommandDownloadSlot::call_value_base(Command* rawCommand, core::Download* downlo
{
torrent::Object argValue(torrent::Object::TYPE_VALUE);
if (!utils::parse_whole_value_nothrow(arg.as_string().c_str(), &argValue.as_value(), base, unit))
if (!parse_whole_value_nothrow(arg.as_string().c_str(), &argValue.as_value(), base, unit))
throw torrent::input_error("Not a value.");
return command->m_slot(download, argValue);
+4 -4
View File
@@ -34,8 +34,8 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_COMMAND_DOWNLOAD_SLOT_H
#define RTORRENT_UTILS_COMMAND_DOWNLOAD_SLOT_H
#ifndef RTORRENT_RPC_COMMAND_DOWNLOAD_SLOT_H
#define RTORRENT_RPC_COMMAND_DOWNLOAD_SLOT_H
#include <functional>
#include <limits>
@@ -43,13 +43,13 @@
#include <torrent/object.h>
#include <rak/functional_fun.h>
#include "rpc/command.h"
#include "command.h"
namespace core {
class Download;
}
namespace utils {
namespace rpc {
class CommandDownloadSlot : public Command {
public:
+1 -1
View File
@@ -48,7 +48,7 @@
#include "command.h"
#include "command_map.h"
namespace utils {
namespace rpc {
struct command_map_get_ptr : std::unary_function<CommandMap::value_type&, Command*> {
Command* operator () (CommandMap::value_type& value) { return value.second.m_variable; }
+14 -18
View File
@@ -34,8 +34,8 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_COMMAND_MAP_H
#define RTORRENT_UTILS_COMMAND_MAP_H
#ifndef RTORRENT_RPC_COMMAND_MAP_H
#define RTORRENT_RPC_COMMAND_MAP_H
#include <map>
#include <string>
@@ -46,7 +46,7 @@ namespace core {
class Download;
}
namespace utils {
namespace rpc {
class Command;
@@ -93,10 +93,6 @@ public:
using base_type::end;
using base_type::find;
static const int max_size_key = 128;
static const int max_size_opt = 1024;
static const int max_size_line = max_size_key + max_size_opt + 64;
static const int flag_dont_delete = 0x1;
static const int flag_public_xmlrpc = 0x2;
@@ -112,21 +108,21 @@ public:
void insert(key_type key, const command_map_data_type src);
const mapped_type call_command(key_type key, const mapped_type& arg);
const mapped_type call_command_void(key_type key) { return call_command(key, torrent::Object()); }
const std::string call_command_string(key_type key) { return call_command(key, torrent::Object()).as_string(); }
mapped_value_type call_command_value(key_type key) { return call_command(key, torrent::Object()).as_value(); }
// const mapped_type call_command_void(key_type key) { return call_command(key, torrent::Object()); }
// const std::string call_command_string(key_type key) { return call_command(key, torrent::Object()).as_string(); }
// mapped_value_type call_command_value(key_type key) { return call_command(key, torrent::Object()).as_value(); }
void call_command_set_string(key_type key, const std::string& arg) { call_command(key, mapped_type(arg)); }
void call_command_set_std_string(const std::string& key, const std::string& arg) { call_command(key.c_str(), mapped_type(arg)); }
// void call_command_set_string(key_type key, const std::string& arg) { call_command(key, mapped_type(arg)); }
// void call_command_set_std_string(const std::string& key, const std::string& arg) { call_command(key.c_str(), mapped_type(arg)); }
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg);
const mapped_type call_command_d_void(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()); }
const std::string call_command_d_string(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()).as_string(); }
mapped_value_type call_command_d_value(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()).as_value(); }
// const mapped_type call_command_d_void(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()); }
// const std::string call_command_d_string(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()).as_string(); }
// mapped_value_type call_command_d_value(key_type key, core::Download* download) { return call_command_d(key, download, torrent::Object()).as_value(); }
void call_command_d_set_value(key_type key, core::Download* download, mapped_value_type arg) { call_command_d(key, download, mapped_type(arg)); }
void call_command_d_set_string(key_type key, core::Download* download, const std::string& arg) { call_command_d(key, download, mapped_type(arg)); }
void call_command_d_set_std_string(const std::string& key, core::Download* download, const std::string& arg) { call_command_d(key.c_str(), download, mapped_type(arg)); }
// void call_command_d_set_value(key_type key, core::Download* download, mapped_value_type arg) { call_command_d(key, download, mapped_type(arg)); }
// void call_command_d_set_string(key_type key, core::Download* download, const std::string& arg) { call_command_d(key, download, mapped_type(arg)); }
// void call_command_d_set_std_string(const std::string& key, core::Download* download, const std::string& arg) { call_command_d(key.c_str(), download, mapped_type(arg)); }
private:
CommandMap(const CommandMap&);
+3 -3
View File
@@ -36,11 +36,11 @@
#include "config.h"
#include "rpc/parse.h"
#include "parse.h"
#include "command_slot.h"
namespace utils {
namespace rpc {
const torrent::Object
CommandSlot::call_unknown(Command* rawCommand, const torrent::Object& rawArgs) {
@@ -85,7 +85,7 @@ CommandSlot::call_value_base(Command* rawCommand, const torrent::Object& rawArgs
{
torrent::Object argValue(torrent::Object::TYPE_VALUE);
if (!utils::parse_whole_value_nothrow(arg.as_string().c_str(), &argValue.as_value(), base, unit))
if (!parse_whole_value_nothrow(arg.as_string().c_str(), &argValue.as_value(), base, unit))
throw torrent::input_error("Not a value.");
return command->m_slot(argValue);
+4 -4
View File
@@ -34,8 +34,8 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_COMMAND_SLOT_H
#define RTORRENT_UTILS_COMMAND_SLOT_H
#ifndef RTORRENT_RPC_COMMAND_SLOT_H
#define RTORRENT_RPC_COMMAND_SLOT_H
#include <functional>
#include <limits>
@@ -43,9 +43,9 @@
#include <torrent/object.h>
#include <rak/functional_fun.h>
#include "rpc/command.h"
#include "command.h"
namespace utils {
namespace rpc {
class CommandSlot : public Command {
public:
+2 -3
View File
@@ -36,11 +36,10 @@
#include "config.h"
#include "rpc/parse.h"
#include "parse.h"
#include "command_variable.h"
namespace utils {
namespace rpc {
const torrent::Object
CommandVariable::set_bool(Command* rawCommand, const torrent::Object& rawArgs) {
+4 -4
View File
@@ -34,17 +34,17 @@
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UTILS_COMMAND_VARIABLES_H
#define RTORRENT_UTILS_COMMAND_VARIABLES_H
#ifndef RTORRENT_RPC_COMMAND_VARIABLES_H
#define RTORRENT_RPC_COMMAND_VARIABLES_H
#include <string>
#include <limits>
#include <inttypes.h>
#include <torrent/object.h>
#include "rpc/command.h"
#include "command.h"
namespace utils {
namespace rpc {
class CommandVariable : public Command {
public:
+1 -1
View File
@@ -41,7 +41,7 @@
#include "parse.h"
namespace utils {
namespace rpc {
const char*
parse_skip_wspace(const char* first, const char* last) {
+1 -1
View File
@@ -40,7 +40,7 @@
#include <string>
#include <torrent/object.h>
namespace utils {
namespace rpc {
// parse_* functions do the bare minimum necessary to parse what was
// asked for. If a whitespace is found, it will be treated as empty
+13 -12
View File
@@ -45,9 +45,10 @@
#include "parse.h"
#include "parse_commands.h"
#include "command_map.h"
namespace utils {
namespace rpc {
CommandMap commands;
struct command_map_is_space : std::unary_function<char, bool> {
bool operator () (char c) const {
@@ -74,12 +75,12 @@ parse_command_name(const char* first, const char* last, std::string* dest) {
}
const char*
parse_command_single(CommandMap* varMap, const char* first) {
return parse_command_single(varMap, first, first + std::strlen(first));
parse_command_single(const char* first) {
return parse_command_single(first, first + std::strlen(first));
}
const char*
parse_command_single(CommandMap* varMap, const char* first, const char* last) {
parse_command_single(const char* first, const char* last) {
first = std::find_if(first, last, std::not1(command_map_is_space()));
if (first == last || *first == '#')
@@ -96,13 +97,13 @@ parse_command_single(CommandMap* varMap, const char* first, const char* last) {
torrent::Object args;
parse_whole_list(first + 1, last, &args);
varMap->call_command(key.c_str(), args);
commands.call_command(key.c_str(), args);
return last;
}
const char*
parse_command_d_single(CommandMap* varMap, core::Download* download, const char* first, const char* last) {
parse_command_d_single(core::Download* download, const char* first, const char* last) {
first = std::find_if(first, last, std::not1(command_map_is_space()));
if (first == last || *first == '#')
@@ -118,13 +119,13 @@ parse_command_d_single(CommandMap* varMap, core::Download* download, const char*
torrent::Object args;
parse_whole_list(first + 1, last, &args);
varMap->call_command_d(key.c_str(), download, args);
commands.call_command_d(key.c_str(), download, args);
return last;
}
void
parse_command_multiple(CommandMap* varMap, const char* first) {
parse_command_multiple(const char* first) {
try {
while (first != '\0') {
const char* last = first;
@@ -133,7 +134,7 @@ parse_command_multiple(CommandMap* varMap, const char* first) {
// Should we check the return value? Probably not necessary as
// parse_args throws on unquoted multi-word input.
parse_command_single(varMap, first, last);
parse_command_single(first, last);
if (*last == '\0')
return;
@@ -147,7 +148,7 @@ parse_command_multiple(CommandMap* varMap, const char* first) {
}
bool
parse_command_file(CommandMap* varMap, const std::string& path) {
parse_command_file(const std::string& path) {
std::fstream file(rak::path_expand(path).c_str(), std::ios::in);
if (!file.is_open())
@@ -161,7 +162,7 @@ parse_command_file(CommandMap* varMap, const std::string& path) {
while (file.getline(buffer, 2048).good()) {
lineNumber++;
// Would be nice to make this zero-copy.
parse_command_single(varMap, buffer, buffer + std::strlen(buffer));
parse_command_single(buffer, buffer + std::strlen(buffer));
}
} catch (torrent::input_error& e) {
+33 -11
View File
@@ -39,34 +39,56 @@
#include <string>
#include "command_map.h"
namespace core {
class Download;
}
namespace utils {
namespace rpc {
class CommandMap;
// class CommandMap;
const char* parse_command_name(const char* first, const char* last, std::string* dest);
const char* parse_command_single(CommandMap* varMap, const char* first);
const char* parse_command_single(CommandMap* varMap, const char* first, const char* last);
const char* parse_command_single(const char* first);
const char* parse_command_single(const char* first, const char* last);
const char* parse_command_d_single(CommandMap* varMap, core::Download* download, const char* first, const char* last);
const char* parse_command_d_single(core::Download* download, const char* first, const char* last);
void parse_command_multiple(CommandMap* varMap, const char* first);
bool parse_command_file(CommandMap* varMap, const std::string& path);
void parse_command_multiple(const char* first);
bool parse_command_file(const std::string& path);
inline void
parse_command_single_std(CommandMap* varMap, const std::string& cmd) {
parse_command_single(varMap, cmd.c_str(), cmd.c_str() + cmd.size());
parse_command_single_std(const std::string& cmd) {
parse_command_single(cmd.c_str(), cmd.c_str() + cmd.size());
}
inline void
parse_command_d_single_std(CommandMap* varMap, core::Download* download, const std::string& cmd) {
parse_command_d_single(varMap, download, cmd.c_str(), cmd.c_str() + cmd.size());
parse_command_d_single_std(core::Download* download, const std::string& cmd) {
parse_command_d_single(download, cmd.c_str(), cmd.c_str() + cmd.size());
}
// Move to anoher file?
extern CommandMap commands;
inline torrent::Object call_command(const char* key, const torrent::Object& obj) { return commands.call_command(key, obj); }
inline torrent::Object call_command_void(const char* key) { return commands.call_command(key, torrent::Object()); }
inline std::string call_command_string(const char* key) { return commands.call_command(key, torrent::Object()).as_string(); }
inline int64_t call_command_value(const char* key) { return commands.call_command(key, torrent::Object()).as_value(); }
inline void call_command_set_string(const char* key, const std::string& arg) { commands.call_command(key, torrent::Object(arg)); }
inline void call_command_set_std_string(const std::string& key, const std::string& arg) { commands.call_command(key.c_str(), torrent::Object(arg)); }
inline torrent::Object call_command_d(const char* key, core::Download* download, const torrent::Object& obj) { return commands.call_command_d(key, download, obj); }
inline torrent::Object call_command_d_void(const char* key, core::Download* download) { return commands.call_command_d(key, download, torrent::Object()); }
inline std::string call_command_d_string(const char* key, core::Download* download) { return commands.call_command_d(key, download, torrent::Object()).as_string(); }
inline int64_t call_command_d_value(const char* key, core::Download* download) { return commands.call_command_d(key, download, torrent::Object()).as_value(); }
inline void call_command_d_set_value(const char* key, core::Download* download, int64_t arg) { commands.call_command_d(key, download, torrent::Object(arg)); }
inline void call_command_d_set_string(const char* key, core::Download* download, const std::string& arg) { commands.call_command_d(key, download, torrent::Object(arg)); }
inline void call_command_d_set_std_string(const std::string& key, core::Download* download, const std::string& arg) { commands.call_command_d(key.c_str(), download, torrent::Object(arg)); }
}
#endif
+1 -1
View File
@@ -52,7 +52,7 @@ namespace rpc {
class XmlRpc {
public:
typedef rak::function2<bool, const char*, uint32_t> slot_write;
typedef rak::function2<const torrent::Object, const char*, const torrent::Object&> slot_call_command;
typedef rak::function2<torrent::Object, const char*, const torrent::Object&> slot_call_command;
XmlRpc();
~XmlRpc();