Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create single-element maps for functions instead of using dict_keys #1341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/rpc/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,27 @@ parse_object(const char* first, const char* last, torrent::Object* dest, bool (*
if (depth > 3)
throw torrent::input_error("Max 3 parantheses per object allowed.");

*dest = torrent::Object::create_dict_key();
*dest = torrent::Object::create_map();
dest->set_flags(torrent::Object::flag_function << (depth - 1));

first = parse_string(first + 1, last, &dest->as_dict_key(), &parse_is_delim_func);
std::string key;
torrent::Object val;

first = parse_string(first + 1, last, &key, &parse_is_delim_func);
first = parse_skip_wspace(first, last);

if (first == last || !parse_is_delim_func(*first))
throw torrent::input_error("Could not find closing ')'.");

if (*first == ',') {
// This will always create a list even for single argument functions...
dest->as_dict_obj() = torrent::Object::create_list();
first = parse_list(first + 1, last, &dest->as_dict_obj(), &parse_is_delim_func);
val = torrent::Object::create_list();
first = parse_list(first + 1, last, &val, &parse_is_delim_func);
first = parse_skip_wspace(first, last);
}

dest->as_map().insert({key, val});

while (depth != 0 && first != last && *first == ')') {
first++;
depth--;
Expand Down
32 changes: 16 additions & 16 deletions src/rpc/parse_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,27 +253,27 @@ call_object(const torrent::Object& command, target_type target) {
}
case torrent::Object::TYPE_MAP:
{
if (command.is_dict_key()) {
// This can/should be optimized...
torrent::Object tmp_command = command;

// Unquote the root function object so 'parse_command_execute'
// doesn't end up calling it.
//
// TODO: Only call this if mask_function is set?
uint32_t flags = tmp_command.flags() & torrent::Object::mask_function;
tmp_command.unset_flags(torrent::Object::mask_function);
tmp_command.set_flags((flags >> 1) & torrent::Object::mask_function);

parse_command_execute(target, &tmp_command);
return commands.call_command(tmp_command.as_dict_key().c_str(), tmp_command.as_dict_obj(), target);
}

for (torrent::Object::map_const_iterator itr = command.as_map().begin(), last = command.as_map().end(); itr != last; itr++)
call_object(itr->second, target);

return torrent::Object();
}
case torrent::Object::TYPE_DICT_KEY:
{
// This can/should be optimized...
torrent::Object tmp_command = command;

// Unquote the root function object so 'parse_command_execute'
// doesn't end up calling it.
//
// TODO: Only call this if mask_function is set?
uint32_t flags = tmp_command.flags() & torrent::Object::mask_function;
tmp_command.unset_flags(torrent::Object::mask_function);
tmp_command.set_flags((flags >> 1) & torrent::Object::mask_function);

parse_command_execute(target, &tmp_command);
return commands.call_command(tmp_command.as_dict_key().c_str(), tmp_command.as_dict_obj(), target);
}
default:
return torrent::Object();
}
Expand Down
26 changes: 0 additions & 26 deletions src/rpc/xmlrpc_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,32 +351,6 @@ object_to_xmlrpc(xmlrpc_env* env, const torrent::Object& object) {
return result;
}

case torrent::Object::TYPE_DICT_KEY:
{
xmlrpc_value* result = xmlrpc_array_new(env);

xmlrpc_value* key_item = object_to_xmlrpc(env, object.as_dict_key());
xmlrpc_array_append_item(env, result, key_item);
xmlrpc_DECREF(key_item);

if (object.as_dict_obj().is_list()) {
for (torrent::Object::list_const_iterator
itr = object.as_dict_obj().as_list().begin(),
last = object.as_dict_obj().as_list().end();
itr != last; itr++) {
xmlrpc_value* item = object_to_xmlrpc(env, *itr);
xmlrpc_array_append_item(env, result, item);
xmlrpc_DECREF(item);
}
} else {
xmlrpc_value* arg_item = object_to_xmlrpc(env, object.as_dict_obj());
xmlrpc_array_append_item(env, result, arg_item);
xmlrpc_DECREF(arg_item);
}

return result;
}

default:
return xmlrpc_int_new(env, 0);
}
Expand Down
20 changes: 0 additions & 20 deletions src/rpc/xmlrpc_tinyxml2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,6 @@ print_object_xml(const torrent::Object& obj, tinyxml2::XMLPrinter* printer) {
}
printer->CloseElement(true);
break;
case torrent::Object::TYPE_DICT_KEY:
printer->OpenElement("array", true);

printer->OpenElement("value", true);
print_object_xml(obj.as_dict_key(), printer);
printer->CloseElement(true);

if (obj.as_dict_obj().is_list()) {
for (auto itr : obj.as_dict_obj().as_list()) {
printer->OpenElement("value", true);
print_object_xml(itr, printer);
printer->CloseElement(true);
}
} else {
printer->OpenElement("value", true);
print_object_xml(obj.as_dict_obj(), printer);
printer->CloseElement(true);
}
printer->CloseElement(true);
break;
default:
printer->OpenElement("i4", true);
printer->PushText(0);
Expand Down