Merge branch 'master' into feature/clang-format

This commit is contained in:
Jari Sundell
2024-11-13 19:51:04 +09:00
committed by GitHub
7 changed files with 47 additions and 174 deletions
+1 -67
View File
@@ -1,39 +1,3 @@
// rak - Rakshasa's toolbox
// Copyright (C) 2005-2007, 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
// priority_queue is a priority queue implemented using a binary
// heap. It can contain multiple instances of a value.
@@ -85,7 +49,7 @@ public:
template <typename Key>
iterator find(const Key& key) {
return std::find_if(begin(), end(), std::bind2nd(m_equal, key));
return std::find_if(begin(), end(), [&](auto& value) { return m_equal(value, key); });
}
template <typename Key>
@@ -110,36 +74,6 @@ private:
Equal m_equal;
};
// Iterate while the top node has higher priority, as 'Compare'
// returns false.
template <typename Queue, typename Compare>
class queue_pop_iterator
: public std::iterator<std::forward_iterator_tag, void, void, void, void> {
public:
typedef Queue container_type;
queue_pop_iterator() : m_queue(NULL) {}
queue_pop_iterator(Queue* q, Compare c) : m_queue(q), m_compare(c) {}
queue_pop_iterator& operator ++ () { m_queue->pop(); return *this; }
queue_pop_iterator& operator ++ (int) { m_queue->pop(); return *this; }
typename container_type::const_reference operator * () { return m_queue->top(); }
bool operator != (const queue_pop_iterator& itr) { return !m_queue->empty() && !m_compare(m_queue->top()); }
bool operator == (const queue_pop_iterator& itr) { return m_queue->empty() || m_compare(m_queue->top()); }
private:
Queue* m_queue;
Compare m_compare;
};
template <typename Queue, typename Compare>
inline queue_pop_iterator<Queue, Compare>
queue_popper(Queue& queue, Compare comp) {
return queue_pop_iterator<Queue, Compare>(&queue, comp);
}
}
#endif
+15 -15
View File
@@ -142,11 +142,11 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
throw torrent::input_error("Invalid argument count.");
torrent::Object::list_const_iterator itrArgs = args.begin();
const std::string& rawKey = (itrArgs++)->as_string();
const std::string& raw_key = (itrArgs++)->as_string();
if (rawKey.empty() ||
control->object_storage()->find_raw_string(torrent::raw_string::from_string(rawKey)) != control->object_storage()->end() ||
rpc::commands.has(rawKey) || rpc::commands.has(rawKey + ".set"))
if (raw_key.empty() ||
control->object_storage()->find_raw_string(torrent::raw_string::from_string(raw_key)) != control->object_storage()->end() ||
rpc::commands.has(raw_key) || rpc::commands.has(raw_key + ".set"))
throw torrent::input_error("Invalid key.");
torrent::Object value;
@@ -171,7 +171,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
}
// We must initialize this varriable to prevent a critical memory leak
int cmd_flags = rpc::CommandMap::flag_delete_key;
int cmd_flags = 0;
if (!(flags & rpc::object_storage::flag_static))
cmd_flags |= rpc::CommandMap::flag_modifiable;
@@ -185,16 +185,16 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
if ((itrArgs)->is_list())
valueListType = (itrArgs)->as_list();
control->object_storage()->insert_str(rawKey, valueList, flags);
control->object_storage()->insert_str(raw_key, valueList, flags);
} else {
control->object_storage()->insert_str(rawKey, value, flags);
control->object_storage()->insert_str(raw_key, value, flags);
}
if ((flags & rpc::object_storage::mask_type) == rpc::object_storage::flag_function_type ||
(flags & rpc::object_storage::mask_type) == rpc::object_storage::flag_multi_type) {
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call<rpc::target_type>>::type>(
create_new_key(rawKey),
raw_key,
std::bind(&rpc::object_storage::call_function_str, control->object_storage(), rawKey, std::placeholders::_1, std::placeholders::_2),
&rpc::command_base_call<rpc::target_type>,
cmd_flags,
@@ -203,7 +203,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
} else {
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call<rpc::target_type>>::type>(
create_new_key(rawKey),
raw_key,
std::bind(&rpc::object_storage::get_str, control->object_storage(), rawKey),
&rpc::command_base_call<rpc::target_type>,
cmd_flags,
@@ -228,7 +228,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
switch (flags & rpc::object_storage::mask_type) {
case rpc::object_storage::flag_bool_type:
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call_value<rpc::target_type>>::type>(
create_new_key<5>(rawKey, ".set"),
raw_key + ".set",
std::bind(&rpc::object_storage::set_str_bool, control->object_storage(), rawKey, std::placeholders::_2),
&rpc::command_base_call_value<rpc::target_type>,
cmd_flags,
@@ -237,7 +237,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
break;
case rpc::object_storage::flag_value_type:
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call_value<rpc::target_type>>::type>(
create_new_key<5>(rawKey, ".set"),
raw_key + ".set",
std::bind(&rpc::object_storage::set_str_value, control->object_storage(), rawKey, std::placeholders::_2),
&rpc::command_base_call_value<rpc::target_type>,
cmd_flags,
@@ -246,7 +246,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
break;
case rpc::object_storage::flag_string_type:
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call_string<rpc::target_type>>::type>(
create_new_key<5>(rawKey, ".set"),
raw_key + ".set",
std::bind(&rpc::object_storage::set_str_string, control->object_storage(), rawKey, std::placeholders::_2),
&rpc::command_base_call_string<rpc::target_type>,
cmd_flags,
@@ -255,7 +255,7 @@ system_method_insert_object(const torrent::Object::list_type& args, int flags) {
break;
case rpc::object_storage::flag_list_type:
rpc::commands.insert_slot<rpc::command_base_is_type<rpc::command_base_call_list<rpc::target_type>>::type>(
create_new_key<5>(rawKey, ".set"),
raw_key + ".set",
std::bind(&rpc::object_storage::set_str_list, control->object_storage(), rawKey, std::placeholders::_2),
&rpc::command_base_call_list<rpc::target_type>,
cmd_flags,
@@ -298,7 +298,7 @@ system_method_insert(const torrent::Object::list_type& args) {
if (rawKey.empty() || rpc::commands.has(rawKey))
throw torrent::input_error("Invalid key.");
int flags = rpc::CommandMap::flag_delete_key | rpc::CommandMap::flag_modifiable | rpc::CommandMap::flag_public_xmlrpc;
int flags = rpc::CommandMap::flag_modifiable | rpc::CommandMap::flag_public_xmlrpc;
int new_flags = rpc::parse_option_flags(itrArgs->as_string(), std::bind(&object_storage_parse_flag, std::placeholders::_1));
if ((new_flags & rpc::object_storage::flag_private))
@@ -356,7 +356,7 @@ system_method_redirect(const torrent::Object::list_type& args) {
std::string new_key = torrent::object_create_string(args.front());
std::string dest_key = torrent::object_create_string(args.back());
rpc::commands.create_redirect(create_new_key(new_key), create_new_key(dest_key), rpc::CommandMap::flag_public_xmlrpc | rpc::CommandMap::flag_delete_key | rpc::CommandMap::flag_modifiable);
rpc::commands.create_redirect(new_key, dest_key, rpc::CommandMap::flag_public_xmlrpc | rpc::CommandMap::flag_modifiable);
return torrent::Object();
}
+4 -24
View File
@@ -148,12 +148,12 @@ void initialize_commands();
rpc::commands.create_redirect(from_key, to_key, rpc::CommandMap::flag_public_xmlrpc | rpc::CommandMap::flag_tracker_target | rpc::CommandMap::flag_dont_delete);
#define CMD2_REDIRECT_GENERIC_STR(from_key, to_key) \
rpc::commands.create_redirect(create_new_key(from_key), create_new_key(to_key), \
rpc::CommandMap::flag_public_xmlrpc | rpc::CommandMap::flag_no_target | rpc::CommandMap::flag_delete_key);
rpc::commands.create_redirect(from_key, to_key, \
rpc::CommandMap::flag_public_xmlrpc | rpc::CommandMap::flag_no_target);
#define CMD2_REDIRECT_GENERIC_STR_NO_EXPORT(from_key, to_key) \
rpc::commands.create_redirect(create_new_key(from_key), create_new_key(to_key), \
rpc::CommandMap::flag_no_target | rpc::CommandMap::flag_delete_key);
rpc::commands.create_redirect(from_key, to_key, \
rpc::CommandMap::flag_no_target);
//
// Conversion of return types:
@@ -188,24 +188,4 @@ template <typename T>
object_convert_type<T, void>
object_convert_void(T f) { return f; }
//
// Key creation:
//
template <int postfix_size>
inline const char*
create_new_key(const std::string& key, const char postfix[postfix_size]) {
char *buffer = new char[key.size() + std::max(postfix_size, 1)];
std::memcpy(buffer, key.c_str(), key.size() + 1);
std::memcpy(buffer + key.size(), postfix, postfix_size);
return buffer;
}
inline const char*
create_new_key(const std::string& key) {
char *buffer = new char[key.size() + 1];
std::memcpy(buffer, key.c_str(), key.size() + 1);
return buffer;
}
#endif
+1 -1
View File
@@ -32,7 +32,7 @@ torrent::Object
apply_log_open(int output_flags, const torrent::Object::list_type& args) {
if (args.size() < 2)
throw torrent::input_error("Invalid number of arguments.");
torrent::Object::list_const_iterator itr = args.begin();
std::string output_id = (itr++)->as_string();
+1 -1
View File
@@ -135,7 +135,7 @@ initialize_xmlrpc() {
if (!(itr->second.m_flags & rpc::CommandMap::flag_public_xmlrpc))
continue;
rpc::xmlrpc.insert_command(itr->first, itr->second.m_parm, itr->second.m_doc);
rpc::xmlrpc.insert_command(itr->first.c_str(), itr->second.m_parm, itr->second.m_doc);
}
lt_log_print(torrent::LOG_RPC_EVENTS, "XMLRPC initialized with %u functions.", count);
+8 -41
View File
@@ -36,7 +36,6 @@
#include "config.h"
#include <vector>
#include <torrent/exceptions.h>
#include <torrent/object.h>
#include <torrent/data/file_list_iterator.h>
@@ -57,23 +56,8 @@ namespace rpc {
command_base::stack_type command_base::current_stack;
CommandMap::~CommandMap() {
std::vector<const char*> keys;
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;
if (itr->second.m_flags & flag_delete_key)
keys.push_back(itr->first);
}
for (std::vector<const char*>::iterator itr = keys.begin(), last = keys.end(); itr != last; itr++)
delete [] *itr;
}
CommandMap::iterator
CommandMap::insert(key_type key, int flags, const char* parm, const char* doc) {
CommandMap::insert(const key_type& key, int flags, const char* parm, const char* doc) {
iterator itr = base_type::find(key);
if (itr != base_type::end())
@@ -81,25 +65,11 @@ CommandMap::insert(key_type key, int flags, const char* parm, const char* doc) {
// TODO: This is not honoring the public_xmlrpc flags!!!
if (rpc::xmlrpc.is_valid() && (flags & flag_public_xmlrpc))
// if (rpc::xmlrpc.is_valid())
rpc::xmlrpc.insert_command(key, parm, doc);
rpc::xmlrpc.insert_command(key.c_str(), parm, doc);
return base_type::insert(itr, value_type(key, command_map_data_type(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.");
// itr = base_type::insert(itr, value_type(key, command_map_data_type(src.m_variable, src.m_flags | flag_dont_delete, src.m_parm, src.m_doc)));
// // We can assume all the slots are the same size.
// itr->second.m_anySlot = src.m_anySlot;
// }
void
CommandMap::erase(iterator itr) {
if (itr == end())
@@ -112,14 +82,11 @@ CommandMap::erase(iterator itr) {
// if (!(itr->second.m_flags & flag_dont_delete))
// delete itr->second.m_variable;
const char* key = itr->second.m_flags & flag_delete_key ? itr->first : NULL;
base_type::erase(itr);
delete [] key;
}
void
CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
CommandMap::create_redirect(const key_type& key_new, const key_type& key_dest, int flags) {
iterator new_itr = base_type::find(key_new);
iterator dest_itr = base_type::find(key_dest);
@@ -135,11 +102,11 @@ CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
dest_itr->second.m_flags |= flag_has_redirects;
flags |= dest_itr->second.m_flags & ~(flag_delete_key | flag_has_redirects | flag_public_xmlrpc);
flags |= dest_itr->second.m_flags & ~(flag_has_redirects | flag_public_xmlrpc);
// TODO: This is not honoring the public_xmlrpc flags!!!
if (rpc::xmlrpc.is_valid() && (flags & flag_public_xmlrpc))
rpc::xmlrpc.insert_command(key_new, dest_itr->second.m_parm, dest_itr->second.m_doc);
rpc::xmlrpc.insert_command(key_new.c_str(), dest_itr->second.m_parm, dest_itr->second.m_doc);
iterator itr = base_type::insert(base_type::end(),
value_type(key_new, command_map_data_type(flags,
@@ -152,7 +119,7 @@ CommandMap::create_redirect(key_type key_new, key_type key_dest, int flags) {
}
const CommandMap::mapped_type
CommandMap::call_catch(key_type key, target_type target, const mapped_type& args, const char* err) {
CommandMap::call_catch(const key_type& key, const target_type& target, const mapped_type& args, const char* err) {
try {
return call_command(key, args, target);
} catch (torrent::input_error& e) {
@@ -162,7 +129,7 @@ CommandMap::call_catch(key_type key, target_type target, const mapped_type& args
}
const CommandMap::mapped_type
CommandMap::call_command(key_type key, const mapped_type& arg, target_type target) {
CommandMap::call_command(const key_type& key, const mapped_type& arg, const target_type& target) {
iterator itr = base_type::find(key);
if (itr == base_type::end())
@@ -172,7 +139,7 @@ CommandMap::call_command(key_type key, const mapped_type& arg, target_type targe
}
const CommandMap::mapped_type
CommandMap::call_command(iterator itr, const mapped_type& arg, target_type target) {
CommandMap::call_command(iterator itr, const mapped_type& arg, const target_type& target) {
return itr->second.m_anySlot(&itr->second.m_variable, target, arg);
}
+17 -25
View File
@@ -46,10 +46,6 @@
namespace rpc {
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
@@ -73,9 +69,9 @@ struct command_map_data_type {
const char* m_doc;
};
class CommandMap : public std::map<const char*, command_map_data_type, command_map_comp> {
class CommandMap : public std::map<std::string, command_map_data_type> {
public:
typedef std::map<const char*, command_map_data_type, command_map_comp> base_type;
typedef std::map<std::string, command_map_data_type> base_type;
typedef torrent::Object mapped_type;
typedef mapped_type::value_type mapped_value_type;
@@ -90,7 +86,6 @@ public:
using base_type::find;
static const int flag_dont_delete = 0x1;
static const int flag_delete_key = 0x2;
static const int flag_public_xmlrpc = 0x4;
static const int flag_modifiable = 0x10;
static const int flag_is_redirect = 0x20;
@@ -101,39 +96,36 @@ public:
static const int flag_tracker_target = 0x400;
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()); }
bool has(const std::string& key) const { return base_type::find(key) != base_type::end(); }
bool is_modifiable(const_iterator itr) { return itr != end() && (itr->second.m_flags & flag_modifiable); }
iterator insert(key_type key, int flags, const char* parm, const char* doc);
iterator insert(const key_type& key, int flags, const char* parm, const char* doc);
template <typename T, typename Slot>
void
insert_slot(key_type key, Slot variable, command_base::any_slot targetSlot, int flags, const char* parm, const char* doc) {
insert_slot(const key_type& key, Slot variable, command_base::any_slot target_slot, int flags, const char* parm, const char* doc) {
iterator itr = insert(key, flags, parm, doc);
itr->second.m_variable.set_function<T>(variable);
itr->second.m_anySlot = targetSlot;
itr->second.m_anySlot = target_slot;
}
// void insert(key_type key, const command_map_data_type src);
void erase(iterator itr);
void create_redirect(key_type key_new, key_type key_dest, int flags);
void create_redirect(const key_type& key_new, const key_type& key_dest, int flags);
const mapped_type call(key_type key, const mapped_type& args = mapped_type());
const mapped_type call(key_type key, target_type target, const mapped_type& args = mapped_type()) { return call_command(key, args, target); }
const mapped_type call_catch(key_type key, target_type target, const mapped_type& args = mapped_type(), const char* err = "Command failed: ");
const mapped_type call(const key_type& key, const mapped_type& args = mapped_type());
const mapped_type call(const key_type& key, const target_type& target, const mapped_type& args = mapped_type()) { return call_command(key, args, target); }
const mapped_type call_catch(const key_type& key, const target_type& target, const mapped_type& args = mapped_type(), const char* err = "Command failed: ");
const mapped_type call_command (key_type key, const mapped_type& arg, target_type target = target_type((int)command_base::target_generic, NULL));
const mapped_type call_command (iterator itr, const mapped_type& arg, target_type target = target_type((int)command_base::target_generic, NULL));
const mapped_type call_command (const key_type& key, const mapped_type& arg, const target_type& target = target_type((int)command_base::target_generic, NULL));
const mapped_type call_command (iterator itr, const mapped_type& arg, const target_type& target = target_type((int)command_base::target_generic, NULL));
const mapped_type call_command_d(key_type key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_download, download)); }
const mapped_type call_command_p(key_type key, torrent::Peer* peer, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_peer, peer)); }
const mapped_type call_command_t(key_type key, torrent::Tracker* tracker, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_tracker, tracker)); }
const mapped_type call_command_f(key_type key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_file, file)); }
const mapped_type call_command_d(const key_type& key, core::Download* download, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_download, download)); }
const mapped_type call_command_p(const key_type& key, torrent::Peer* peer, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_peer, peer)); }
const mapped_type call_command_t(const key_type& key, torrent::Tracker* tracker, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_tracker, tracker)); }
const mapped_type call_command_f(const key_type& key, torrent::File* file, const mapped_type& arg) { return call_command(key, arg, target_type((int)command_base::target_file, file)); }
private:
CommandMap(const CommandMap&);
@@ -175,7 +167,7 @@ create_object_list(const torrent::Object& o1, const torrent::Object& o2, const t
}
inline const CommandMap::mapped_type
CommandMap::call(key_type key, const mapped_type& args) {
CommandMap::call(const key_type& key, const mapped_type& args) {
return call_command(key, args, make_target());
}