mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-07 06:39:23 +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:
@@ -7,8 +7,6 @@ libsub_utils_a_SOURCES = \
|
||||
lockfile.cc \
|
||||
lockfile.h \
|
||||
socket_fd.cc \
|
||||
socket_fd.h \
|
||||
variable_map.cc \
|
||||
variable_map.h
|
||||
socket_fd.h
|
||||
|
||||
INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir)
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
// 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 "rpc/command.h"
|
||||
#include "variable_map.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct variable_map_get_ptr : std::unary_function<VariableMap::value_type&, Command*> {
|
||||
Command* operator () (VariableMap::value_type& value) { return value.second.m_variable; }
|
||||
};
|
||||
|
||||
VariableMap::~VariableMap() {
|
||||
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
|
||||
VariableMap::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("VariableMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
base_type::insert(itr, value_type(key, variable_map_data_type(variable, genericSlot, downloadSlot, flags, parm, doc)));
|
||||
}
|
||||
|
||||
void
|
||||
VariableMap::insert(key_type key, const variable_map_data_type src) {
|
||||
iterator itr = base_type::find(key);
|
||||
|
||||
if (itr != base_type::end())
|
||||
throw torrent::internal_error("VariableMap::insert(...) tried to insert an already existing key.");
|
||||
|
||||
base_type::insert(itr, value_type(key, variable_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 VariableMap::mapped_type
|
||||
VariableMap::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 VariableMap::mapped_type
|
||||
VariableMap::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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
// 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_VARIABLE_MAP_H
|
||||
#define RTORRENT_UTILS_VARIABLE_MAP_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iosfwd>
|
||||
#include <torrent/object.h>
|
||||
|
||||
namespace core {
|
||||
class Download;
|
||||
}
|
||||
|
||||
namespace torrent {
|
||||
class Object;
|
||||
}
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct variable_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; }
|
||||
};
|
||||
|
||||
class Command;
|
||||
|
||||
struct variable_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&);
|
||||
|
||||
variable_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 VariableMap : public std::map<const char*, variable_map_data_type, variable_map_comp> {
|
||||
public:
|
||||
typedef std::map<const char*, variable_map_data_type, variable_map_comp> base_type;
|
||||
|
||||
typedef variable_map_data_type::generic_slot generic_slot;
|
||||
typedef variable_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;
|
||||
|
||||
VariableMap() {}
|
||||
~VariableMap();
|
||||
|
||||
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 variable_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:
|
||||
VariableMap(const VariableMap&);
|
||||
void operator = (const VariableMap&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user