mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-09 07:39:21 +00:00
* Renamed VariableMap to CommandMap.
git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@913 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -2,6 +2,8 @@ noinst_LIBRARIES = libsub_rpc.a
|
||||
|
||||
libsub_rpc_a_SOURCES = \
|
||||
command.h \
|
||||
command_map.cc \
|
||||
command_map.h \
|
||||
command_slot.cc \
|
||||
command_slot.h \
|
||||
command_variable.cc \
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
#include <fstream>
|
||||
#include <rak/functional.h>
|
||||
#include <rak/path.h>
|
||||
#include <torrent/exceptions.h>
|
||||
#include <torrent/object.h>
|
||||
|
||||
#include "command.h"
|
||||
#include "command_map.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct command_map_get_ptr : std::unary_function<CommandMap::value_type&, Command*> {
|
||||
Command* operator () (CommandMap::value_type& value) { return value.second.m_variable; }
|
||||
};
|
||||
|
||||
CommandMap::~CommandMap() {
|
||||
for (iterator itr = base_type::begin(), last = base_type::end(); itr != last; itr++)
|
||||
if (!(itr->second.m_flags & flag_dont_delete))
|
||||
delete itr->second.m_variable;
|
||||
}
|
||||
|
||||
void
|
||||
CommandMap::insert(key_type key, Command* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
|
||||
const char* parm, const char* doc) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr != base_type::end())
|
||||
throw torrent::internal_error("CommandMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
base_type::insert(itr, value_type(key, command_map_data_type(variable, genericSlot, downloadSlot, flags, parm, doc)));
|
||||
}
|
||||
|
||||
void
|
||||
CommandMap::insert(key_type key, const command_map_data_type src) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr != base_type::end())
|
||||
throw torrent::internal_error("CommandMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
base_type::insert(itr, value_type(key, command_map_data_type(src.m_variable, src.m_genericSlot, src.m_downloadSlot,
|
||||
src.m_flags | flag_dont_delete, src.m_parm, src.m_doc)));
|
||||
}
|
||||
|
||||
const CommandMap::mapped_type
|
||||
CommandMap::call_command(key_type key, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Command \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
if (itr->second.m_genericSlot == NULL)
|
||||
throw torrent::input_error("Command does not have a generic slot.");
|
||||
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
}
|
||||
|
||||
const CommandMap::mapped_type
|
||||
CommandMap::call_command_d(key_type key, core::Download* download, const mapped_type& arg) {
|
||||
const_iterator itr = base_type::find(key);
|
||||
|
||||
if (itr == base_type::end())
|
||||
throw torrent::input_error("Command \"" + std::string(key) + "\" does not exist.");
|
||||
|
||||
if (itr->second.m_downloadSlot == NULL) {
|
||||
if (itr->second.m_genericSlot == NULL)
|
||||
throw torrent::input_error("Command does not have a generic slot.");
|
||||
|
||||
return itr->second.m_genericSlot(itr->second.m_variable, arg);
|
||||
}
|
||||
|
||||
return itr->second.m_downloadSlot(itr->second.m_variable, download, arg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
// rTorrent - BitTorrent client
|
||||
// Copyright (C) 2006, Jari Sundell
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations
|
||||
// including the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for
|
||||
// all of the code used other than OpenSSL. If you modify file(s)
|
||||
// with this exception, you may extend this exception to your version
|
||||
// of the file(s), but you are not obligated to do so. If you do not
|
||||
// wish to do so, delete this exception statement from your version.
|
||||
// If you delete this exception statement from all source files in the
|
||||
// program, then also delete it here.
|
||||
//
|
||||
// Contact: Jari Sundell <jaris@ifi.uio.no>
|
||||
//
|
||||
// Skomakerveien 33
|
||||
// 3185 Skoppum, NORWAY
|
||||
|
||||
#ifndef RTORRENT_UTILS_COMMAND_MAP_H
|
||||
#define RTORRENT_UTILS_COMMAND_MAP_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <torrent/object.h>
|
||||
|
||||
namespace core {
|
||||
class Download;
|
||||
}
|
||||
|
||||
namespace utils {
|
||||
|
||||
class Command;
|
||||
|
||||
struct command_map_comp : public std::binary_function<const char*, const char*, bool> {
|
||||
bool operator () (const char* arg1, const char* arg2) const { return std::strcmp(arg1, arg2) < 0; }
|
||||
};
|
||||
|
||||
struct command_map_data_type {
|
||||
// Some commands will need to share data, like get/set a variable. So
|
||||
// instead of using a single virtual member function, each command
|
||||
// will register a member function pointer to be used instead.
|
||||
typedef const torrent::Object (*generic_slot)(Command*, const torrent::Object&);
|
||||
typedef const torrent::Object (*download_slot)(Command*, core::Download*, const torrent::Object&);
|
||||
|
||||
command_map_data_type(Command* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
|
||||
const char* parm, const char* doc) :
|
||||
m_variable(variable), m_genericSlot(genericSlot), m_downloadSlot(downloadSlot), m_flags(flags), m_parm(parm), m_doc(doc) {}
|
||||
|
||||
Command* m_variable;
|
||||
generic_slot m_genericSlot;
|
||||
download_slot m_downloadSlot;
|
||||
|
||||
int m_flags;
|
||||
|
||||
const char* m_parm;
|
||||
const char* m_doc;
|
||||
};
|
||||
|
||||
class CommandMap : public std::map<const char*, command_map_data_type, command_map_comp> {
|
||||
public:
|
||||
typedef std::map<const char*, command_map_data_type, command_map_comp> base_type;
|
||||
|
||||
typedef command_map_data_type::generic_slot generic_slot;
|
||||
typedef command_map_data_type::download_slot download_slot;
|
||||
|
||||
typedef torrent::Object mapped_type;
|
||||
typedef mapped_type::value_type mapped_value_type;
|
||||
|
||||
using base_type::iterator;
|
||||
using base_type::key_type;
|
||||
using base_type::value_type;
|
||||
|
||||
using base_type::begin;
|
||||
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;
|
||||
|
||||
CommandMap() {}
|
||||
~CommandMap();
|
||||
|
||||
bool has(const char* key) const { return base_type::find(key) != base_type::end(); }
|
||||
bool has(const std::string& key) const { return has(key.c_str()); }
|
||||
|
||||
void insert(key_type key, Command* variable, generic_slot genericSlot, download_slot downloadSlot, int flags,
|
||||
const char* parm, const char* doc);
|
||||
|
||||
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(); }
|
||||
|
||||
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(); }
|
||||
|
||||
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&);
|
||||
void operator = (const CommandMap&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
+12
-12
@@ -45,17 +45,17 @@
|
||||
|
||||
#include "parse.h"
|
||||
#include "parse_commands.h"
|
||||
#include "utils/variable_map.h"
|
||||
#include "command_map.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct variable_map_is_space : std::unary_function<char, bool> {
|
||||
struct command_map_is_space : std::unary_function<char, bool> {
|
||||
bool operator () (char c) const {
|
||||
return std::isspace(c);
|
||||
}
|
||||
};
|
||||
|
||||
struct variable_map_is_newline : std::unary_function<char, bool> {
|
||||
struct command_map_is_newline : std::unary_function<char, bool> {
|
||||
bool operator () (char c) const {
|
||||
return c == '\n' || c == '\0';
|
||||
}
|
||||
@@ -74,13 +74,13 @@ parse_command_name(const char* first, const char* last, std::string* dest) {
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_command_single(VariableMap* varMap, const char* first) {
|
||||
parse_command_single(CommandMap* varMap, const char* first) {
|
||||
return parse_command_single(varMap, first, first + std::strlen(first));
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_command_single(VariableMap* varMap, const char* first, const char* last) {
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
parse_command_single(CommandMap* varMap, const char* first, const char* last) {
|
||||
first = std::find_if(first, last, std::not1(command_map_is_space()));
|
||||
|
||||
if (first == last || *first == '#')
|
||||
return last;
|
||||
@@ -88,7 +88,7 @@ parse_command_single(VariableMap* varMap, const char* first, const char* last) {
|
||||
// Avoid using a string here?
|
||||
std::string key;
|
||||
first = parse_command_name(first, last, &key);
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
first = std::find_if(first, last, std::not1(command_map_is_space()));
|
||||
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
@@ -102,15 +102,15 @@ parse_command_single(VariableMap* varMap, const char* first, const char* last) {
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_command_d_single(VariableMap* varMap, core::Download* download, const char* first, const char* last) {
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
parse_command_d_single(CommandMap* varMap, 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 == '#')
|
||||
return last;
|
||||
|
||||
std::string key;
|
||||
first = parse_command_name(first, last, &key);
|
||||
first = std::find_if(first, last, std::not1(variable_map_is_space()));
|
||||
first = std::find_if(first, last, std::not1(command_map_is_space()));
|
||||
|
||||
if (first == last || *first != '=')
|
||||
throw torrent::input_error("Could not find '='.");
|
||||
@@ -124,7 +124,7 @@ parse_command_d_single(VariableMap* varMap, core::Download* download, const char
|
||||
}
|
||||
|
||||
void
|
||||
parse_command_multiple(VariableMap* varMap, const char* first) {
|
||||
parse_command_multiple(CommandMap* varMap, const char* first) {
|
||||
try {
|
||||
while (first != '\0') {
|
||||
const char* last = first;
|
||||
@@ -147,7 +147,7 @@ parse_command_multiple(VariableMap* varMap, const char* first) {
|
||||
}
|
||||
|
||||
bool
|
||||
parse_command_file(VariableMap* varMap, const std::string& path) {
|
||||
parse_command_file(CommandMap* varMap, const std::string& path) {
|
||||
std::fstream file(rak::path_expand(path).c_str(), std::ios::in);
|
||||
|
||||
if (!file.is_open())
|
||||
|
||||
@@ -45,25 +45,25 @@ namespace core {
|
||||
|
||||
namespace utils {
|
||||
|
||||
class VariableMap;
|
||||
class CommandMap;
|
||||
|
||||
const char* parse_command_name(const char* first, const char* last, std::string* dest);
|
||||
|
||||
const char* parse_command_single(VariableMap* varMap, const char* first);
|
||||
const char* parse_command_single(VariableMap* varMap, const char* first, const char* last);
|
||||
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_d_single(VariableMap* varMap, core::Download* download, const char* first, const char* last);
|
||||
const char* parse_command_d_single(CommandMap* varMap, core::Download* download, const char* first, const char* last);
|
||||
|
||||
void parse_command_multiple(VariableMap* varMap, const char* first);
|
||||
bool parse_command_file(VariableMap* varMap, const std::string& path);
|
||||
void parse_command_multiple(CommandMap* varMap, const char* first);
|
||||
bool parse_command_file(CommandMap* varMap, const std::string& path);
|
||||
|
||||
inline void
|
||||
parse_command_single_std(VariableMap* varMap, const std::string& cmd) {
|
||||
parse_command_single_std(CommandMap* varMap, const std::string& cmd) {
|
||||
parse_command_single(varMap, cmd.c_str(), cmd.c_str() + cmd.size());
|
||||
}
|
||||
|
||||
inline void
|
||||
parse_command_d_single_std(VariableMap* varMap, core::Download* download, const std::string& cmd) {
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user