diff --git a/src/core/download.cc b/src/core/download.cc index d52c9171..a12091ca 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -65,8 +65,8 @@ Download::Download(torrent::Download d) : rak::mem_fn(this, &Download::set_connection_current))); m_variables.insert("connection_leech", new utils::VariableAny(connection_type_to_string(torrent::Download::CONNECTION_LEECH))); m_variables.insert("connection_seed", new utils::VariableAny(connection_type_to_string(torrent::Download::CONNECTION_SEED))); - m_variables.insert("state", new utils::VariableBencode(&m_download.bencode(), "rtorrent", "state", torrent::Bencode::TYPE_STRING)); - m_variables.insert("tied_to_file", new utils::VariableBencode(&m_download.bencode(), "rtorrent", "tied_to_file", torrent::Bencode::TYPE_STRING)); + m_variables.insert("state", new utils::VariableObject(&m_download.bencode(), "rtorrent", "state", torrent::Object::TYPE_STRING)); + m_variables.insert("tied_to_file", new utils::VariableObject(&m_download.bencode(), "rtorrent", "tied_to_file", torrent::Object::TYPE_STRING)); m_variables.insert("directory", new utils::VariableSlotString<>(rak::mem_fn(&m_download, &torrent::Download::root_dir), rak::mem_fn(this, &Download::set_root_directory))); diff --git a/src/core/download.h b/src/core/download.h index 44cb6d09..76ec7e88 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -66,7 +66,7 @@ public: torrent::Download& get_download() { return m_download; } const torrent::Download& get_download() const { return m_download; } std::string get_hash() { return m_download.info_hash(); } - torrent::Bencode& get_bencode() { return m_download.bencode(); } + torrent::Object& get_bencode() { return m_download.bencode(); } const std::string& get_message() { return m_message; } diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index 8c09525f..1bcfcad9 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include @@ -149,7 +149,7 @@ DownloadFactory::receive_success() { return; } - torrent::Bencode& root = (*itr)->get_bencode(); + torrent::Object& root = (*itr)->get_bencode(); if (!m_session) { // We only allow session torrents to keep their @@ -160,9 +160,9 @@ DownloadFactory::receive_success() { if (!root.has_key("rtorrent") || !root.get_key("rtorrent").is_map()) - root.insert_key("rtorrent", torrent::Bencode(torrent::Bencode::TYPE_MAP)); + root.insert_key("rtorrent", torrent::Object(torrent::Object::TYPE_MAP)); - torrent::Bencode& rtorrent = root.get_key("rtorrent"); + torrent::Object& rtorrent = root.get_key("rtorrent"); if (!rtorrent.has_key("state") || !rtorrent.get_key("state").is_string() || diff --git a/src/core/download_list.cc b/src/core/download_list.cc index 7ca44a0d..70b2efdc 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -37,8 +37,11 @@ #include "config.h" #include +#include #include #include +#include +#include #include #include "rak/functional.h" @@ -63,9 +66,17 @@ struct download_list_call { DownloadList::iterator DownloadList::insert(std::istream* str, bool printLog) { + torrent::Object* object = new torrent::Object; + try { - torrent::Download d = torrent::download_add(str); + *str >> *object; + + // Catch, delete. + if (str->fail()) + throw torrent::input_error("Could not create download, the input is not a valid torrent."); + + torrent::Download d = torrent::download_add(object); iterator itr = Base::insert(end(), new Download(d)); @@ -75,6 +86,8 @@ DownloadList::insert(std::istream* str, bool printLog) { return itr; } catch (torrent::local_error& e) { + delete object; + if (printLog) control->core()->push_log(e.what()); diff --git a/src/core/download_store.cc b/src/core/download_store.cc index abd4f824..baf15085 100644 --- a/src/core/download_store.cc +++ b/src/core/download_store.cc @@ -43,10 +43,11 @@ #include #include #include -#include +#include #include #include #include +#include #include "download.h" #include "download_store.h" @@ -112,7 +113,7 @@ DownloadStore::save(Download* d) { // Test the new file, to ensure it is a valid bencode string. f.open((create_filename(d) + ".new").c_str(), std::ios::in); - torrent::Bencode tmp; + torrent::Object tmp; f >> tmp; if (!f.good()) diff --git a/src/core/manager.cc b/src/core/manager.cc index b9082808..e0b8f437 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -48,7 +48,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index fa900825..2fae6c04 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/utils/variable.h b/src/utils/variable.h index 959230b0..5ecc241f 100644 --- a/src/utils/variable.h +++ b/src/utils/variable.h @@ -40,7 +40,7 @@ #include namespace torrent { - class Bencode; + class Object; } namespace utils { @@ -50,8 +50,8 @@ public: Variable() {} virtual ~Variable() {} - virtual const torrent::Bencode& get() = 0; - virtual void set(const torrent::Bencode& arg) = 0; + virtual const torrent::Object& get() = 0; + virtual void set(const torrent::Object& arg) = 0; protected: Variable(const Variable&); diff --git a/src/utils/variable_generic.cc b/src/utils/variable_generic.cc index d7edf5a8..8ea2478c 100644 --- a/src/utils/variable_generic.cc +++ b/src/utils/variable_generic.cc @@ -45,40 +45,40 @@ namespace utils { VariableAny::~VariableAny() { } -const torrent::Bencode& +const torrent::Object& VariableAny::get() { return m_variable; } void -VariableAny::set(const torrent::Bencode& arg) { +VariableAny::set(const torrent::Object& arg) { m_variable = arg; } VariableValue::~VariableValue() { } -const torrent::Bencode& +const torrent::Object& VariableValue::get() { return m_variable; } void -VariableValue::set(const torrent::Bencode& arg) { +VariableValue::set(const torrent::Object& arg) { uint64_t value; const char* first; char* last; - switch (arg.get_type()) { - case torrent::Bencode::TYPE_NONE: + switch (arg.type()) { + case torrent::Object::TYPE_NONE: m_variable = (int64_t)0; break; - case torrent::Bencode::TYPE_VALUE: + case torrent::Object::TYPE_VALUE: m_variable = arg; break; - case torrent::Bencode::TYPE_STRING: + case torrent::Object::TYPE_STRING: first = arg.as_string().c_str(); value = strtoll(first, &last, 0); @@ -96,13 +96,13 @@ VariableValue::set(const torrent::Bencode& arg) { VariableBool::~VariableBool() { } -const torrent::Bencode& +const torrent::Object& VariableBool::get() { return m_variable; } void -VariableBool::set(const torrent::Bencode& arg) { +VariableBool::set(const torrent::Object& arg) { if (arg.is_value()) { m_variable = arg.as_value() ? (int64_t)1 : (int64_t)0; @@ -124,11 +124,11 @@ VariableBool::set(const torrent::Bencode& arg) { } } -VariableBencode::~VariableBencode() { +VariableObject::~VariableObject() { } -const torrent::Bencode& -VariableBencode::get() { +const torrent::Object& +VariableObject::get() { if (m_root.empty()) return m_bencode->get_key(m_key); else @@ -136,9 +136,9 @@ VariableBencode::get() { } void -VariableBencode::set(const torrent::Bencode& arg) { +VariableObject::set(const torrent::Object& arg) { // Consider removing if TYPE_NONE. - torrent::Bencode* root; + torrent::Object* root; if (m_root.empty()) root = m_bencode; @@ -146,20 +146,20 @@ VariableBencode::set(const torrent::Bencode& arg) { root = &m_bencode->get_key(m_root); switch (m_type) { - case torrent::Bencode::TYPE_NONE: + case torrent::Object::TYPE_NONE: root->insert_key(m_key, arg); break; - case torrent::Bencode::TYPE_STRING: - if (arg.get_type() == torrent::Bencode::TYPE_STRING) + case torrent::Object::TYPE_STRING: + if (arg.type() == torrent::Object::TYPE_STRING) root->insert_key(m_key, arg); else - throw torrent::input_error("VariableBencode could not convert to string."); + throw torrent::input_error("VariableObject could not convert to string."); break; default: - throw torrent::input_error("VariableBencode unsupported type restriction."); + throw torrent::input_error("VariableObject unsupported type restriction."); } } diff --git a/src/utils/variable_generic.h b/src/utils/variable_generic.h index b2b97881..f5e164a4 100644 --- a/src/utils/variable_generic.h +++ b/src/utils/variable_generic.h @@ -35,7 +35,7 @@ // 3185 Skoppum, NORWAY // Parts of this seems ugly in an attempt to avoid copying -// data. Propably need to rewrite torrent::Bencode. +// data. Propably need to rewrite torrent::Object. #ifndef RTORRENT_UTILS_VARIABLE_GENERIC_H #define RTORRENT_UTILS_VARIABLE_GENERIC_H @@ -45,7 +45,7 @@ #include #include #include -#include +#include #include #include "variable.h" @@ -54,15 +54,15 @@ namespace utils { class VariableAny : public Variable { public: - VariableAny(const torrent::Bencode& v = torrent::Bencode()) : + VariableAny(const torrent::Object& v = torrent::Object()) : m_variable(v) {} virtual ~VariableAny(); - virtual const torrent::Bencode& get(); - virtual void set(const torrent::Bencode& arg); + virtual const torrent::Object& get(); + virtual void set(const torrent::Object& arg); private: - torrent::Bencode m_variable; + torrent::Object m_variable; }; class VariableValue : public Variable { @@ -70,42 +70,42 @@ public: VariableValue(int64_t v) : m_variable(v) {} virtual ~VariableValue(); - virtual const torrent::Bencode& get(); - virtual void set(const torrent::Bencode& arg); + virtual const torrent::Object& get(); + virtual void set(const torrent::Object& arg); private: - torrent::Bencode m_variable; + torrent::Object m_variable; }; class VariableBool : public Variable { public: VariableBool(bool state) : m_variable(state ? (int64_t)1 : (int64_t)0) {} - VariableBool(const torrent::Bencode& v = torrent::Bencode((int64_t)0)) { set(v); } + VariableBool(const torrent::Object& v = torrent::Object((int64_t)0)) { set(v); } virtual ~VariableBool(); - virtual const torrent::Bencode& get(); - virtual void set(const torrent::Bencode& arg); + virtual const torrent::Object& get(); + virtual void set(const torrent::Object& arg); private: - torrent::Bencode m_variable; + torrent::Object m_variable; }; -class VariableBencode : public Variable { +class VariableObject : public Variable { public: - typedef torrent::Bencode::Type Type; + typedef torrent::Object::type_type Type; - VariableBencode(torrent::Bencode* b, + VariableObject(torrent::Object* b, const std::string& root, const std::string& key, - Type t = torrent::Bencode::TYPE_NONE) : + Type t = torrent::Object::TYPE_NONE) : m_bencode(b), m_root(root), m_key(key), m_type(t) {} - virtual ~VariableBencode(); + virtual ~VariableObject(); - virtual const torrent::Bencode& get(); - virtual void set(const torrent::Bencode& arg); + virtual const torrent::Object& get(); + virtual void set(const torrent::Object& arg); private: - torrent::Bencode* m_bencode; + torrent::Object* m_bencode; std::string m_root; std::string m_key; Type m_type; @@ -124,7 +124,7 @@ public: virtual ~VariableSlotString() {} - virtual const torrent::Bencode& get() { + virtual const torrent::Object& get() { m_cache = m_slotGet(); if (!m_cache.is_string()) @@ -133,12 +133,12 @@ public: return m_cache; } - virtual void set(const torrent::Bencode& arg) { - switch (arg.get_type()) { - case torrent::Bencode::TYPE_STRING: + virtual void set(const torrent::Object& arg) { + switch (arg.type()) { + case torrent::Object::TYPE_STRING: m_slotSet(arg.as_string()); break; - case torrent::Bencode::TYPE_NONE: + case torrent::Object::TYPE_NONE: m_slotSet(""); break; default: @@ -153,7 +153,7 @@ private: // Store the cache here to avoid unnessesary copying and such. This // should not result in any unresonable memory usage since few // strings will be very large. - torrent::Bencode m_cache; + torrent::Object m_cache; }; template @@ -176,7 +176,7 @@ public: virtual ~VariableSlotValue() {} - virtual const torrent::Bencode& get() { + virtual const torrent::Object& get() { m_cache = m_slotGet(); // Need this? @@ -186,7 +186,7 @@ public: return m_cache; } - virtual void set(const torrent::Bencode& arg) { + virtual void set(const torrent::Object& arg) { if (arg.is_string()) { Set v; @@ -213,7 +213,7 @@ private: // Store the cache here to avoid unnessesary copying and such. This // should not result in any unresonable memory usage since few // strings will be very large. - torrent::Bencode m_cache; + torrent::Object m_cache; }; } diff --git a/src/utils/variable_map.cc b/src/utils/variable_map.cc index d4fd33f8..2cc4467e 100644 --- a/src/utils/variable_map.cc +++ b/src/utils/variable_map.cc @@ -43,7 +43,7 @@ #include #include #include -#include +#include #include "variable.h" #include "variable_map.h" @@ -126,7 +126,7 @@ parse_unknown(std::string::const_iterator first, std::string::const_iterator las } std::string::const_iterator -parse_args(std::string::const_iterator first, std::string::const_iterator last, VariableMap::mapped_type::List* dest) { +parse_args(std::string::const_iterator first, std::string::const_iterator last, VariableMap::mapped_type::list_type* dest) { first = std::find_if(first, last, std::not1(variable_map_is_space())); while (first != last) { diff --git a/src/utils/variable_map.h b/src/utils/variable_map.h index 3b4beb62..3795b83e 100644 --- a/src/utils/variable_map.h +++ b/src/utils/variable_map.h @@ -40,7 +40,7 @@ #include #include #include -#include +#include namespace utils { @@ -49,7 +49,7 @@ class Variable; class VariableMap : public std::map { public: typedef std::map base_type; - typedef torrent::Bencode mapped_type; + typedef torrent::Object mapped_type; static const int max_size_key = 128; static const int max_size_opt = 1024;