mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-08-16 15:12:30 +00:00
* Continuing conversion to the new command infrastructure.
git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@896 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
@@ -40,6 +40,13 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
const torrent::Object
|
||||
CommandSlot::call_unknown(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandSlot* command = static_cast<CommandSlot*>(rawVariable);
|
||||
|
||||
return command->m_slot(rawArgs);
|
||||
}
|
||||
|
||||
const torrent::Object
|
||||
CommandSlot::call_list(Variable* rawVariable, const torrent::Object& rawArgs) {
|
||||
CommandSlot* command = static_cast<CommandSlot*>(rawVariable);
|
||||
|
||||
@@ -69,6 +69,8 @@ public:
|
||||
|
||||
void set_slot(slot_type::base_type* s) { m_slot.set(s); }
|
||||
|
||||
static const torrent::Object call_unknown(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
static const torrent::Object call_list(Variable* rawVariable, const torrent::Object& args);
|
||||
static const torrent::Object call_string(Variable* rawVariable, const torrent::Object& args);
|
||||
|
||||
@@ -134,6 +136,7 @@ private:
|
||||
|
||||
template <typename Return> object_void_fn_t<Return (*)(void), Return>* object_fn(Return (*func)(void)) { return new object_void_fn_t<Return (*)(void), Return>(func); }
|
||||
|
||||
template <typename Func> object_void_fn_t<Func>* object_void_fn(Func func) { return new object_void_fn_t<Func>(func); }
|
||||
template <typename Func> object_string_fn1_t<Func>* object_string_fn(Func func) { return new object_string_fn1_t<Func>(func); }
|
||||
|
||||
}
|
||||
|
||||
+51
-9
@@ -51,6 +51,15 @@ parse_skip_wspace(const char* first, const char* last) {
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_skip_wspace(const char* first) {
|
||||
// Assume iswspace('\0') == false.
|
||||
while (std::iswspace(*first))
|
||||
first++;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_string(const char* first, const char* last, std::string* dest) {
|
||||
if (first == last)
|
||||
@@ -99,6 +108,26 @@ parse_whole_string(const char* first, const char* last, std::string* dest) {
|
||||
|
||||
const char*
|
||||
parse_value(const char* src, int64_t* value, int base, int unit) {
|
||||
const char* last = parse_value_nothrow(src, value, base, unit);
|
||||
|
||||
if (last == src)
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
bool
|
||||
parse_whole_value_nothrow(const char* src, int64_t* value, int base, int unit) {
|
||||
const char* last = parse_value_nothrow(src, value, base, unit);
|
||||
|
||||
if (last == src || *parse_skip_wspace(last) != '\0')
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char*
|
||||
parse_value_nothrow(const char* src, int64_t* value, int base, int unit) {
|
||||
if (unit <= 0)
|
||||
throw torrent::input_error("Variable::string_to_value_unit(...) received unit <= 0.");
|
||||
|
||||
@@ -111,7 +140,7 @@ parse_value(const char* src, int64_t* value, int base, int unit) {
|
||||
if (strcasecmp(src, "true") == 0) { *value = 1; return src + strlen("true"); }
|
||||
if (strcasecmp(src, "false") == 0) { *value = 0; return src + strlen("false"); }
|
||||
|
||||
throw torrent::input_error("Could not convert string to value.");
|
||||
return src;
|
||||
}
|
||||
|
||||
switch (*last) {
|
||||
@@ -267,26 +296,39 @@ convert_list_to_command(torrent::Object::list_type::const_iterator first,
|
||||
|
||||
int64_t
|
||||
convert_to_value(const torrent::Object& src, int base, int unit) {
|
||||
int64_t value;
|
||||
|
||||
if (!convert_to_value_nothrow(src, &value, base, unit))
|
||||
throw torrent::input_error("Not convertible to a value.");
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
bool
|
||||
convert_to_value_nothrow(const torrent::Object& src, int64_t* value, int base, int unit) {
|
||||
const torrent::Object& unpacked = (src.is_list() && src.as_list().size() == 1) ? src.as_list().front() : src;
|
||||
|
||||
switch (unpacked.type()) {
|
||||
case torrent::Object::TYPE_VALUE:
|
||||
return unpacked.as_value();
|
||||
*value = unpacked.as_value();
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_STRING:
|
||||
int64_t tmp;
|
||||
|
||||
if (parse_skip_wspace(parse_value(unpacked.as_string().c_str(), &tmp, base, unit),
|
||||
if (parse_skip_wspace(parse_value(unpacked.as_string().c_str(), value, base, unit),
|
||||
unpacked.as_string().c_str() + unpacked.as_string().size()) != unpacked.as_string().c_str() + unpacked.as_string().size())
|
||||
throw torrent::input_error("Junk at end of value.");
|
||||
return false;
|
||||
|
||||
return tmp;
|
||||
break;
|
||||
|
||||
case torrent::Object::TYPE_NONE:
|
||||
return 0;
|
||||
*value = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw torrent::input_error("Not convertible to a value.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,12 +50,16 @@ inline bool parse_is_quote(const char c) { return c == '"'; }
|
||||
inline bool parse_is_escape(const char c) { return c == '\\'; }
|
||||
inline bool parse_is_seperator(const char c) { return c == ','; }
|
||||
|
||||
const char* parse_skip_wspace(const char* first);
|
||||
const char* parse_skip_wspace(const char* first, const char* last);
|
||||
|
||||
const char* parse_string(const char* first, const char* last, std::string* dest);
|
||||
const char* parse_whole_string(const char* first, const char* last, std::string* dest);
|
||||
|
||||
const char* parse_value(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
const char* parse_value_nothrow(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
bool parse_whole_value_nothrow(const char* src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
const char* parse_list(const char* first, const char* last, torrent::Object* dest);
|
||||
const char* parse_whole_list(const char* first, const char* last, torrent::Object* dest);
|
||||
@@ -65,5 +69,6 @@ std::string convert_list_to_string(torrent::Object::list_type::const_iterator fi
|
||||
std::string convert_list_to_command(torrent::Object::list_type::const_iterator first, torrent::Object::list_type::const_iterator last);
|
||||
|
||||
int64_t convert_to_value(const torrent::Object& src, int base = 0, int unit = 1);
|
||||
bool convert_to_value_nothrow(const torrent::Object& src, int64_t* value, int base = 0, int unit = 1);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user