* Moved Bencode stream functions to object_stream.h and the Bencode

class to object.h.

* torrent::download_add(...) now takes an torrent::Object*.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@653 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-03-20 21:03:42 +00:00
parent 86a3f868df
commit 17d66c4a2f
12 changed files with 83 additions and 69 deletions
+2 -2
View File
@@ -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)));
+1 -1
View File
@@ -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; }
+4 -4
View File
@@ -40,7 +40,7 @@
#include <sstream>
#include <stdexcept>
#include <rak/path.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#include <torrent/exceptions.h>
#include <torrent/rate.h>
@@ -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() ||
+14 -1
View File
@@ -37,8 +37,11 @@
#include "config.h"
#include <algorithm>
#include <iostream>
#include <sigc++/bind.h>
#include <torrent/exceptions.h>
#include <torrent/object.h>
#include <torrent/object_stream.h>
#include <torrent/torrent.h>
#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());
+3 -2
View File
@@ -43,10 +43,11 @@
#include <unistd.h>
#include <rak/path.h>
#include <rak/string_manip.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#include <torrent/exceptions.h>
#include <torrent/torrent.h>
#include <torrent/rate.h>
#include <torrent/object_stream.h>
#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())
+1 -1
View File
@@ -48,7 +48,7 @@
#include <rak/string_manip.h>
#include <sigc++/bind.h>
#include <sigc++/hide.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#include <torrent/connection_manager.h>
#include <torrent/exceptions.h>
+1 -1
View File
@@ -44,7 +44,7 @@
#include <rak/functional.h>
#include <rak/path.h>
#include <rak/string_manip.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#include <torrent/connection_manager.h>
#include <torrent/exceptions.h>
#include <torrent/torrent.h>
+3 -3
View File
@@ -40,7 +40,7 @@
#include <string>
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&);
+20 -20
View File
@@ -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.");
}
}
+30 -30
View File
@@ -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 <limits>
#include <inttypes.h>
#include <rak/functional_fun.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#include <torrent/exceptions.h>
#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 <typename Get, typename Set>
@@ -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;
};
}
+2 -2
View File
@@ -43,7 +43,7 @@
#include <rak/functional.h>
#include <rak/path.h>
#include <torrent/exceptions.h>
#include <torrent/bencode.h>
#include <torrent/object.h>
#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) {
+2 -2
View File
@@ -40,7 +40,7 @@
#include <map>
#include <string>
#include <iosfwd>
#include <torrent/bencode.h>
#include <torrent/object.h>
namespace utils {
@@ -49,7 +49,7 @@ class Variable;
class VariableMap : public std::map<std::string, Variable*> {
public:
typedef std::map<std::string, Variable*> 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;