* Moved hash and torrent size checking to initialize so it will be

caught when loading a torrent.

* Added VariableMap to core::Download and started moving various
settings.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@624 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-01-19 17:13:25 +00:00
parent e8cd37ea3a
commit 52636178d1
14 changed files with 242 additions and 114 deletions
+61
View File
@@ -53,4 +53,65 @@ VariableValue::set(const torrent::Bencode& arg) {
m_variable = arg;
}
VariableBool::~VariableBool() {
}
const torrent::Bencode&
VariableBool::get() {
return m_variable;
}
void
VariableBool::set(const torrent::Bencode& arg) {
if (arg.is_value()) {
m_variable = arg.as_value() ? (int64_t)1 : (int64_t)0;
} else if (arg.is_string()) {
if (arg.as_string() == "yes" ||
arg.as_string() == "true")
m_variable = (int64_t)1;
else if (arg.as_string() == "no" ||
arg.as_string() == "false")
m_variable = (int64_t)0;
else
throw torrent::input_error("String does not parse as a boolean.");
} else {
throw torrent::input_error("Input is not a boolean.");
}
}
VariableBencode::~VariableBencode() {
}
const torrent::Bencode&
VariableBencode::get() {
return m_bencode->get_key(m_key);
}
void
VariableBencode::set(const torrent::Bencode& arg) {
// Consider removing if TYPE_NONE.
switch (m_type) {
case torrent::Bencode::TYPE_NONE:
m_bencode->insert_key(m_key, arg);
break;
case torrent::Bencode::TYPE_STRING:
if (arg.get_type() == torrent::Bencode::TYPE_STRING)
m_bencode->insert_key(m_key, arg);
else
throw torrent::input_error("VariableBencode could not convert to string.");
break;
default:
throw torrent::input_error("VariableBencode unsupported type restriction.");
}
}
}
+30
View File
@@ -61,6 +61,36 @@ private:
torrent::Bencode 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); }
virtual ~VariableBool();
virtual const torrent::Bencode& get();
virtual void set(const torrent::Bencode& arg);
private:
torrent::Bencode m_variable;
};
class VariableBencode : public Variable {
public:
typedef torrent::Bencode::Type Type;
VariableBencode(torrent::Bencode* b, const std::string& key, Type t = torrent::Bencode::TYPE_NONE) :
m_bencode(b), m_key(key), m_type(t) {}
virtual ~VariableBencode();
virtual const torrent::Bencode& get();
virtual void set(const torrent::Bencode& arg);
private:
torrent::Bencode* m_bencode;
std::string m_key;
Type m_type;
};
template <typename Get = std::string, typename Set = const std::string&>
class VariableSlotString : public Variable {
public:
+7 -7
View File
@@ -60,7 +60,7 @@ VariableMap::insert(const std::string& key, Variable* v) {
base_type::insert(itr, value_type(key, v));
}
const torrent::Bencode&
const VariableMap::mapped_type&
VariableMap::get(const std::string& key) {
iterator itr = base_type::find(key);
@@ -71,7 +71,7 @@ VariableMap::get(const std::string& key) {
}
void
VariableMap::set(const std::string& key, const torrent::Bencode& arg) {
VariableMap::set(const std::string& key, const mapped_type& arg) {
iterator itr = base_type::find(key);
// Later, allow the user to create new variables. Have a slot to
@@ -94,7 +94,7 @@ parse_name(std::string::const_iterator first, std::string::const_iterator last,
}
std::string::const_iterator
parse_unknown(std::string::const_iterator first, std::string::const_iterator last, torrent::Bencode* dest) {
parse_unknown(std::string::const_iterator first, std::string::const_iterator last, VariableMap::mapped_type* dest) {
if (*first == '"') {
std::string::const_iterator next = std::find_if(++first, last, std::bind2nd(std::equal_to<char>(), '"'));
@@ -116,11 +116,11 @@ 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, torrent::Bencode::List* dest) {
parse_args(std::string::const_iterator first, std::string::const_iterator last, VariableMap::mapped_type::List* dest) {
first = std::find_if(first, last, std::not1(std::ptr_fun(&std::isspace)));
while (first != last) {
dest->push_back(torrent::Bencode());
dest->push_back(VariableMap::mapped_type());
first = parse_unknown(first, last, &dest->back());
first = std::find_if(first, last, std::not1(std::ptr_fun(&std::isspace)));
@@ -148,11 +148,11 @@ VariableMap::process_command(const std::string& command) {
if (pos == command.end() || *pos != '=')
throw torrent::input_error("Could not find '='.");
torrent::Bencode args(torrent::Bencode::TYPE_LIST);
mapped_type args(mapped_type::TYPE_LIST);
parse_args(pos + 1, command.end(), &args.as_list());
if (args.as_list().empty())
set(key, torrent::Bencode());
set(key, mapped_type());
else if (++args.as_list().begin() == args.as_list().end())
set(key, *args.as_list().begin());
+15 -3
View File
@@ -48,6 +48,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;
using base_type::iterator;
using base_type::value_type;
@@ -59,10 +60,11 @@ public:
// Consider taking char* start and finish instead of std::string to
// avoid copying. Or make a view class.
const torrent::Bencode& get(const std::string& key);
const mapped_type& get(const std::string& key);
std::string get_string(const std::string& key);
void set(const std::string& key, const torrent::Bencode& arg);
void set_string(const std::string& key, const std::string& arg) { set(key, torrent::Bencode(arg)); }
void set(const std::string& key, const mapped_type& arg);
void set_string(const std::string& key, const std::string& arg) { set(key, mapped_type(arg)); }
// Temporary.
void process_command(const std::string& command);
@@ -72,6 +74,16 @@ private:
void operator = (const VariableMap&);
};
inline std::string
VariableMap::get_string(const std::string& key) {
const mapped_type& v = get(key);
if (v.get_type() == mapped_type::TYPE_NONE)
return std::string();
else
return v.as_string();
}
}
#endif